Friday, November 25, 2005

Java RMI connection problems

The standard RMI implementation is simple and easy to use. But it has a design flaw which make many developers confusing:

The server insists on setting a host address in the communication protocol when a client opens a connection. This address is used then for all following calls of the client to the server.

This result in following problem:

The server does not know how the client reached him. The RMI registry just waits on its ports on the network interface and handles the connection then. In detail it handshakes a new port with the client. Unfortunately it overrides the host where the client did come from as well. This is a contrast behavior to standard socket connections where the port is changed as well, but the host address keeps to what the client specified. The server chooses straightly one of its local ip addresses or the address/name specified by the "java.rmi.server.hostname" property.

This leads a lot of coders in a trap. They are wondering why the connection can be established to the remote server, but every consecutive remote call fails. In 99,9% the server has more than one NIC or the server assigns the localhost device to the connection.

The only way to get around this is to specify the same name of the server in each client and set this as the servers hostname in the "java.rmi.server.hostname" property. This name can be resolved to even different IP addresses in each client depending in how the server will be reached.