简体   繁体   English

如何安全关闭RMI客户端?

[英]How to close rmi client safely?

I want to close all connections between client and server with RMI protocol. 我想使用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' ?

Some code to check rmi status of server: 一些代码来检查服务器的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();
}

This is not a client for rmi comunication. 这不是rmi通讯的客户端。 I just want to check server status using RMI protocol not with simple socket. 我只想使用RMI协议而不是简单的套接字来检查服务器状态。 Sometimes, the server is still running, but all requests blocked. 有时,服务器仍在运行,但是所有请求都被阻止。

You can't 'close all connections' because you have zero visibility of the underlying TCP mechanism. 您无法“关闭所有连接”,因为您对基础TCP机制的可见性为零。 The best you can do in the client is allow all the RMI stubs to be garbage collected. 您可以在客户端中做的最好的事情就是允许所有RMI存根都被垃圾回收。 The underlying connections are pooled and closed fairly aggressively anyway. 无论如何,基础连接都被集中起来并主动关闭。

Try unbind(String name) Removes the binding for the specified name in this registry. 尝试unbind(String name)删除此注册表中指定名称的绑定。 enter link description here 在此处输入链接说明

UnicastRemoteObject.unexportObject(Remote obj, boolean force)可以提供帮助。

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

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