We are using a client/server RMI communication in the same computer (so all ip should be localhost).
We start the registry (using the default port 1099)
registry = LocateRegistry.createRegistry(port);
and export some objects to the RMI registry
Naming.rebind("//" + "localhost" + ":" + port + "/" + name, object);
From another process we retrieve some objects (remember everything runs in localhost)
MyRemoteObject ro = (MyRemoteObject) Naming.lookup("//" + "localhost" + ":" + port + "/" + name);
The issue happen when you run the app starting with local area network working and in the middle of the process you disable the network connection. If you run the app and the LAN is working no issue popup, and if you run the app and the LAN is not working no issue popup. It just happens when you change the LAN while you are running the app.
The exception thrown while executing Naming.lookup() method is this one:
java.lang.RuntimeException: java.rmi.ConnectIOException: Exception creating connection to: 192.168.x.x; nested exception is:
java.net.NoRouteToHostException: No route to host: connect
Debugging a little bit I found out that the
RemoteObject ($Proxy0) -> RemoteObjectInvocationHandler -> UnicastRef2 -> LiveRef -> TCPEndpoint
had the ip of the host (e.g.: 192.168.x.x) instead of "localhost" or 127.0.0.1 (what it would be what I wanted). And the isLocal boolean of liveRef object is always false.
I don't know if it is clear enough. Sorry!!!
Do you have any suggestions?
My Tries:
I tried this solutions
- Run the jvm with -Djava.rmi.server.hostname=localhost argument
- Redefine RMIServerSocketFactory to return 127.0.0.1 everytime. (TCPEndpoint has 192.168.x.x ip and isLocal is always false)
- Call rebind and lookup with no host in the URI. This is supposed to mean localhost.
but none of these have worked.
Any suggestion will be welcome.