2012-01-11 135 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存根被垃圾收集。無論如何,底層連接都是集中並相當積極的關閉。