I'm trying to create an RMI factory but I keep getting the following error:
java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to remote.pkg.ContractDataFactory
I've never worked with the RMI protocol before so has been a lot of guess work to get to this state but now I'm completely lost as to why $Proxy0 can't correctly be cast?
All the reading and other examples I've looked are not trying to implement via a factory pattern so I'm not sure that is where my issue is?
The main entry point to the application:
public static void main(String[] args) throws Exception {
try {
LocateRegistry.createRegistry(1099);
Naming.rebind("FactoryTest", new ContractDataImpl(""));
} catch (RemoteException e) {
e.printStackTrace();
}
Thread a = new FactoryTest("A");
a.start();
Thread.sleep(1000);
Thread b = new FactoryTest("B");
b.start();
a.join();
b.join();
System.exit(0);
}
Here is the run method from the FactoryTest class that I'm running
public void run() {
try {
System.out.println("Getting a remote handle to a factory. " + this.hashCode());
ContractDataFactory factory = (ContractDataFactory) Naming.lookup("FactoryTest");
ContractDataRemote worker = factory.getClient();
} catch (Exception e) {
System.err.println(e);
e.printStackTrace();
}
}
This creates an instance of ContractDataFactory which looks like:
interface ContractDataFactory extends Remote {
public ContractDataRemote getClient() throws RemoteException;
}
and ContractDataRemote looks like:
public interface ContractDataRemote extends Remote, DB {
}
DB is simply another interface with all the actual methods required to do the work.
Hopefully this is enough information to help me solve this?
Cheers.
Edit: Included the contents of ContractDataFactoryImpl
class ContractDataFactoryImpl extends UnicastRemoteObject implements ContractDataFactory {
private static final long serialVersionUID = 1337;
private static String dbLocation = null;
public ContractDataFactoryImpl(String dbLocation) throws RemoteException {
ContractDataFactoryImpl.dbLocation = dbLocation;
}
@Override
public ContractDataRemote getClient() throws RemoteException {
return new ContractDataImpl(dbLocation);
}
}
Naming.rebind("FactoryTest", new ContractDataImpl(""));
That should be
Naming.rebind("FactoryTest", new ContractDataFactoryImpl(""));