如何安全关闭RMI客户端? [英] How to close rmi client safely?

查看:81
本文介绍了如何安全关闭RMI客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用RMI协议关闭客户端和服务器之间的所有连接.

I want to close all connections between client and server with RMI protocol.

   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状态:

Some code to check rmi status of server:

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

This is not a client for rmi comunication. I just want to check server status using RMI protocol not with simple socket. Sometimes, the server is still running, but all requests blocked.

推荐答案

您无法关闭所有连接",因为对基础TCP机制的可见性为零.您可以在客户端中做的最好的事情就是允许所有RMI存根都被垃圾回收.无论如何,基本连接都会被集中起来并关闭.

You can't 'close all connections' because you have zero visibility of the underlying TCP mechanism. The best you can do in the client is allow all the RMI stubs to be garbage collected. The underlying connections are pooled and closed fairly aggressively anyway.

这篇关于如何安全关闭RMI客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆