【问题标题】:How to close rmi client safely?如何安全关闭rmi客户端?
【发布时间】:2012-02-07 13:50:52
【问题描述】:

我想用 RMI 协议关闭客户端和服务器之间的所有连接。

   Remote r=  Naming.lookup("rmi://192.168.105.38:9121/AccountRMIService");
   if(r instanceof RmiInvocationWrapper_Stub) {
       RmiInvocationWrapper_Stub stub = (RmiInvocationWrapper_Stub)r;
       System.out.println("hashCode="+stub.getRef().hashCode());
   }
   System.out.println(r);
   //How to close the connection with 'Remote' ?

检查服务器 rmi 状态的一些代码:

final ThreadLocal<List<Socket>> currentSocket = new ThreadLocal<List<Socket>>() {

    protected List<Socket> initialValue() {
        return new ArrayList<Socket>();
    }
};
RMISocketFactory.setSocketFactory(new RMISocketFactory() {

    public Socket createSocket(String host, int port) throws IOException {
        Socket socket = new Socket(host, port);
        socket.setKeepAlive(true);
        socket.setSoTimeout(300);
        currentSocket.get().add(socket);
        return socket;
    }

    public ServerSocket createServerSocket(int port) throws IOException {
        return new ServerSocket(port);
    }
});
Remote r = Naming.lookup("rmi://192.168.105.38:9121/AccountRMIService");
if (r instanceof RmiInvocationWrapper_Stub) {
    RmiInvocationWrapper_Stub stub = (RmiInvocationWrapper_Stub) r;
    System.out.println("hashCode=" + stub.getRef().hashCode());
}
Iterator<Socket> s = currentSocket.get().iterator();
while(s.hasNext()) {
    s.next().close();
    s.remove();
}

这不是 rmi 通信的客户端。我只想使用 RMI 协议而不是简单的套接字来检查服务器状态。 有时,服务器仍在运行,但所有请求都被阻止。

【问题讨论】:

    标签: java rmi


    【解决方案1】:

    您不能“关闭所有连接”,因为您对底层 TCP 机制的可见性为零。您可以在客户端中做的最好的事情是允许对所有 RMI 存根进行垃圾收集。无论如何,底层连接都会被池化并相当积极地关闭。

    【讨论】:

      【解决方案2】:

      UnicastRemoteObject.unexportObject(Remote obj, boolean force) 可以帮忙。

      【讨论】:

      • 如何关闭客户端的所有连接?服务器仍在运行。
      • 这不会关闭任何东西,除了服务器端的监听套接字,但它会阻止所有未来的客户端执行远程方法调用。不是所要求的。
      【解决方案3】:

      尝试解除绑定(字符串名称) 删除此注册表中指定名称的绑定。enter link description here

      【讨论】:

      • 这将由服务器完成,而不是客户端,它不会关闭任何东西。
      猜你喜欢
      • 2010-09-14
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2013-12-09
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多