【发布时间】:2012-02-07 13:50:52
【问题描述】:
我想用 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 协议而不是简单的套接字来检查服务器状态。 有时,服务器仍在运行,但所有请求都被阻止。
【问题讨论】: