Search code examples
javarmi

RMI Stubs: Force host-value on client side


We would like to access the same RMI-server from different hosts in our network (dev-pc via ssh-tunnel, jenkins-server via direct connection). The problem is that the RMI-host is known under different names on the different client hosts.

This is not a problem when we connect to the registry, because we can set the target host name like this:

Registry registry = LocateRegistry.getRegistry("hostname", 10099, new CustomSslRMIClientSocketFactory());

But when we lookup the remote object like below, it contains the wrong hostname.

HelloRemote hello = (HelloRemote) registry.lookup(HelloRemote.class.getSimpleName());

In the debugger I can observe that the host is like needed on the Registry object, but not on the Stub: debugger view on registry and stub

We get a connection timeout as soon as we call a method on the Stub. If I manually change the host value to localhost in the debugger the method invocation succeeds.

I'm aware that I can set java.rmi.server.hostname on the server side but then the connection from jenkins does not work anymore. The simplest solution would be that I force RMI to use the same host as for the registry for all Stubs retrieved from that registry. Is there a better way than replacing the host-value in the Stub via reflection?


Solution

  • Unfortunately RMI has a deeply built-in assumption that the server host has a single 'most public' IP address or hostname. This explains the java.rmi.server.hostname fiasco. If your system doesn't comply you are out of luck.