问题
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' ?
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();
}
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.
回答1:
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.
回答2:
Try unbind(String name) Removes the binding for the specified name in this registry.enter link description here
回答3:
UnicastRemoteObject.unexportObject(Remote obj, boolean force)
can help.
来源:https://stackoverflow.com/questions/8815342/how-to-close-rmi-client-safely