简体   繁体   English

将方法中的远程对象传递给RMI服务器?

[英]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. 我有一个连接到某个RMI服务器的RMI客户端,只是为了让它知道它可以使用这个新客户端。

Can I pass directly some Remote object so that: 我可以直接传递一些Remote对象,以便:

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? 是否可以在两个类之间使用RMI双向?

Andrew 安德鲁

Yes, you can. 是的你可以。 This is how exactly callbacks work in case of RMI. 这就是在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. 您将对象发送到服务器,当服务器调用对象上的方法时,它将在“客户端”JVM中执行,而不是在服务器上执行。 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. 查看UnicastRemoteObject.export方法,将实现Remote接口的任何对象导出为可以传递给服务器的远程对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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