2012-01-11 134 views
0

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

回答

-1

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

+0

如何关闭客户端的所有连接?服务器仍在运行。 – imxylz 2012-01-11 07:06:27

+0

这将不会关闭任何东西,除了服务器端的侦听套接字,但它会阻止所有未来的客户端执行远程方法调用。不是要求什么。 – EJP 2015-03-04 00:39:08

1

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