Java 如何安全关闭rmi客户端?

Java 如何安全关闭rmi客户端?,java,rmi,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("hash

我想用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();
}
final ThreadLocal currentSocket=new ThreadLocal(){
受保护列表初始值(){
返回新的ArrayList();
}
};
RMISocketFactory.setSocketFactory(新的RMISocketFactory(){
公共套接字createSocket(字符串主机,int端口)引发IOException{
套接字=新的套接字(主机、端口);
socket.setKeepAlive(true);
插座。设置插座(300);
currentSocket.get().add(套接字);
返回插座;
}
公共服务器套接字createServerSocket(int端口)引发IOException{
返回新的ServerSocket(端口);
}
});
远程r=命名。查找(“rmi://192.168.105.38:9121/AccountRMIService");
if(rmiu存根的r实例){
rminvocationwrapper_存根=(rminvocationwrapper_存根)r;
System.out.println(“hashCode=“+stub.getRef().hashCode());
}
迭代器s=currentSocket.get().Iterator();
而(s.hasNext()){
s、 next().close();
s、 删除();
}
这不是rmi通信的客户端。我只想使用RMI协议而不是简单的套接字检查服务器状态。
有时,服务器仍在运行,但所有请求都被阻止。

UnicastRemoteObject。unexportObject(远程obj,布尔力)会有所帮助。

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

请尝试取消绑定(字符串名称)
删除此注册表中指定名称的绑定。

如何关闭客户端的所有连接?服务器仍在运行。这不会关闭任何东西,除了服务器端的侦听套接字,但它将阻止所有未来的客户端执行远程方法调用。这不是要求的。这将由服务器而不是客户端完成,并且不会关闭任何东西。