如何安全关闭rmi客户端?

时间:2012-01-11 06:57:53

标签: java rmi

我想用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协议检查服务器状态而不是简单的套接字。 有时,服务器仍在运行,但所有请求都被阻止。

3 个答案:

答案 0 :(得分:1)

您无法“关闭所有连接”,因为您对底层TCP机制的可见性为零。您可以在客户端中做的最好的事情是允许所有RMI存根被垃圾收集。无论如何,基础连接都是合并并且非常积极地关闭。

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

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

相关问题