I want to set the host address of remote object in rmi. Is it possible setting this ip addres in stub ?
NOT: I know that I can make a trick using ThreadLocalRmiClientSocket factory but I don't want to use it.
I want to set the host address of remote object in rmi. Is it possible setting this ip addres in stub ?
NOT: I know that I can make a trick using ThreadLocalRmiClientSocket factory but I don't want to use it.
Inside remote stub, you need to set a new value for the protected RemoteRef ref field. All remote communications go through this object. As the field is protected, you only can do this simply in your own class you derive from the RMI stub.
Unfortunately RemoteRef is an interface without public implementation. You can look into existing OpenJDK implementation. From there it is seen there is an implementation sun.rmi.server.UnicastRef
that requires a LiveRef
and that one requires Endpoint
. Endpoint
is already relatively easy enough class to understand. You may need to duplicate the functionality of these classes in your code.
To be sure stub class actually exists, generate it with rmic, maybe even use -keepgenerated
so you could check in the source code that field does exists - who knows the details of particular implementation.
In general, this is no way an elegant and easy solution but it should work.
Extension: As all this seems rather complex to do, I propose instead to obtain a new valid stub that points to the updated location of the remote service and steal the value of the ref
field from it. This seems relatively easy to do and may be reasonable if the current stub is tangled in some data structures so that you cannot easily replace it. The stub itself does not care about host, port, object id or watsoever as long as remote side keeps implementing the same remote contract - only its RemoteRef
does. Stub relies on RemoteRef.invoke.
You need to set the system property 'java.rmi.server.hostname' appropriately in the server JVM before exporting any remote objects. Then that value is embedded into the stub when it is created on export.