AndrewBourgeois
AndrewBourgeois

Reputation: 2765

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

Upvotes: 2

Views: 4613

Answers (1)

Sanjay T. Sharma
Sanjay T. Sharma

Reputation: 23208

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);

Upvotes: 4

Related Questions