[英]Pass Remote object in method to RMI server?
我有一个连接到某个RMI服务器的RMI客户端,只是为了让它知道它可以使用这个新客户端。
我可以直接传递一些Remote对象,以便:
serverRemoteObject.registerClient(theClientRemoteObjectTheServerShouldUse);
实际上会给服务器一些他可以使用的对象,而无需连接到我的客户端? 以下问题说这是可能的,但没有给出真实的例子:
安德鲁
是的你可以。 这就是在RMI情况下回调的工作原理。 您将对象发送到服务器,当服务器调用对象上的方法时,它将在“客户端”JVM中执行,而不是在服务器上执行。 查看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.