简体   繁体   中英

Pass Remote object in method to RMI server?

I have an RMI client that connects to some RMI server just to let it know it can use this new client.

Can I pass directly some Remote object so that:

serverRemoteObject.registerClient(theClientRemoteObjectTheServerShouldUse);

will actually give the server some object he can use without connecting to my client? The following question says it is possible, but no real example was given:

Is it possible to use RMI bidirectional between two classes?

Andrew

Yes, you can. This is how exactly callbacks work in case of RMI. You send across an object to the server and when the server invokes a method on your object, it would be executed in the "client" JVM as opposed to on the server. Look into UnicastRemoteObject.export method for export any object which implements the Remote interface as a remote object which can be passed to your server.

interface UpdateListener extends Remote {

  public void handleUpdate(Object update) throws RemoteException;

}

class UpdateListenerImpl implements UpdateListener {

  public void handleUpdate(Object update) throws RemoteException {
  // do something
  }

}

//somewhere in your client code
final UpdateListener listener = new UpdateListenerImpl();
UnicastRemoteObject.export(listener);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM