Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why RMI localhost client uses a no localhost ip to connect the RMI localhost server

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

  1. Run the jvm with -Djava.rmi.server.hostname=localhost argument
  2. Redefine RMIServerSocketFactory to return 127.0.0.1 everytime. (TCPEndpoint has 192.168.x.x ip and isLocal is always false)
  3. 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.

like image 542
RMI-thinker Avatar asked Nov 23 '10 16:11

RMI-thinker


1 Answers

java.rmi.server.hostname should do the trick. You did set it on server, right?

If java.rmi.server.hostname=localhost does not work, how about java.rmi.server.hostname=127.0.0.1 or java.rmi.server.hostname=::1?

like image 169
Neeme Praks Avatar answered Sep 28 '22 06:09

Neeme Praks