Database object is not working with a working RMI package

北城以北 提交于 2019-12-12 02:36:47

问题


I have 2 projects. One works fine in ever department. I downloaded and modified it to better understand it. The 2nd one is a project in development phase.

Now, both these projects have almost exactly the same RMI package, which works fine in the first project, but not in the 2nd.

My test classes in each package are essentially identical as well.

The main difference is what objects there are attempting to access, which are both interfaces in a database package.

Now, the database package in the 2nd project otherwise works absolutely fine, it just wont work with the RMI.

In short:

  • database package works fine
  • RMI package works fine
  • RMI package and database together does not work fine.

Here is my DBInterface

public interface DB extends Remote {

public String[] read(int recNo) throws RecordNotFoundException;

public void update(int recNo, String[] data, long lockCookie)
throws RecordNotFoundException, SecurityException, IOException;

public void delete(int recNo, long lockCookie)
throws RecordNotFoundException, SecurityException, IOException;

public int[] find(String[] criteria);

public int create(String[] data) throws DuplicateKeyException, IOException;

public long lock(int recNo) throws RecordNotFoundException;

public void unlock(int recNo, long cookie)
throws RecordNotFoundException, SecurityException;
}

and here is my RMIInterface

public interface RMIInterface extends Remote{

     public DB getClient() throws RemoteException; 
}

My RMIImplementation

public class RMIImplementation extends UnicastRemoteObject
    implements RMIInterface {  

private static String dbLocation = null;

private DB a;

public RMIImplementation() throws RemoteException{
}

public RMIImplementation(String dbLocation) throws RemoteException{
    System.out.println(dbLocation);
    this.dbLocation = dbLocation;
}

public static DB getRemote(String hostname, String port)
        throws RemoteException {
    String url = "rmi://" + hostname + ":" + port + "/DvdMediator";
    try {
        RMIInterface factory
                = (RMIInterface) Naming.lookup(url);
         // at this point factory equals Proxy[RMIInterface,................etc
        // i want the return to equal Proxy[DB,..............etc
return (DB) factory.getClient(); 
    } catch (NotBoundException e) {

        throw new RemoteException("Dvd Mediator not registered: ", e);
        }

      catch (java.net.MalformedURLException e) {
        throw new RemoteException("cannot connect to " + hostname, e);
    }
}

public DB getClient() throws RemoteException {
      try {
    a = new ContractorDatabase(dbLocation);

    }
    catch(Exception e){
        System.out.println("NewClass exception: " + e.toString());
    }
    return a;
}

And my the RMI registry

public class RegDvdDatabase {

private RegDvdDatabase() {
}

public static void register()
        throws RemoteException {
    register(".", java.rmi.registry.Registry.REGISTRY_PORT);
}

public static void register(String dbLocation, int rmiPort)
        throws RemoteException {

    Registry r = java.rmi.registry.LocateRegistry.createRegistry(rmiPort);

    r.rebind("DvdMediator", new RMIImplementation(dbLocation));
}

}

Getting these two to work together throws a

Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be     cast to sjdproject.remote.RMIImplementation

Can u please help me find the database issue that prevents it from working.


回答1:


You must cast it to the remote interface.

EDIT The Registry reference r in your server code must be static. I can't see any good reason for locating the client lookup code inside the implementation class. That class should only exist at the server, not the client.




回答2:


If you dont have a debugger, I would suggest using reflection on the provided object and see which interfaces it implements. It appears to be a proxy object, so must implement some interfaces.

 for(Class clazz : factory.getClass().getInterfaces()) {
   System.out.println(clazz.getSimpleName());
 }

My suspicion with multiple deployments is of course the jvm version and the classpath. Can you verify that they match?



来源:https://stackoverflow.com/questions/21526047/database-object-is-not-working-with-a-working-rmi-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!