0

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.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
emin
  • 742
  • 9
  • 21
  • For what purpose? The resulting stub has only a 1 in 65536 chance of working, even assuming that a remote object implementing the same remote interface has been exported via the new host:port. – user207421 Jan 29 '13 at 05:21
  • hi, EJP I am getting remote server object. Then I am creating a client and registering this client. like this server.register(client); in above case when i am pinging server everything is going right. But when I ping client from server it is not working. Because client has two ethernet card and in client object invalid host adress embedded. So pinging invalid host address fails. – emin Jan 30 '13 at 13:58

2 Answers2

1

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.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • Precisely none of this is necessary. See my answer. – user207421 Jan 27 '13 at 22:09
  • It is, see my reply. @EJP solution will only works for stubs that represent local objects. – Audrius Meškauskas Jan 28 '13 at 06:20
  • That is completely incorrect. It works for all stubs exported from the JVM where you set that system property, and wherever they may end up, whether local or remote. You've done a good job of analyzing the code but you don't appear to have nay grasp of the wider issues. Your answer is neither necessary nor sufficient, and it will not work except in the very unlikely conditions I have enumerated below my answer. Clearly you haven't tried it. – user207421 Jan 28 '13 at 09:09
-2

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    He is asking how to change address of the *remote* object *in a stub* - the one that represents object on another machine, which host name should now be different. I assume, he wants to change this hostname after he knows the object has migrated to other known location. You explain how to change the host name of the local server. – Audrius Meškauskas Jan 28 '13 at 06:17
  • @AudriusMeškauskas In that case the question doesn't make any sense. Changing the endpoint address directly in the stub will only work if (a) there is a remote object in a JVM at that host:port that (b) implements the same remote interface *and* (c) that was exported with the identical objectID, over which you have no control. Not very likely. – user207421 Jan 28 '13 at 09:08
  • @EJB I assume this is a kind of failover and there is a fully identical service running at that new location. – Audrius Meškauskas Jan 28 '13 at 09:54
  • @AudriusMeškauskas With the same objectID? If he can figure out how to do that, surely he can figure out the stuff with Endpoints as well? And if that's the actual problem, the OP should have said so. There are proper solutions to that, that don't involve rewriting stubs in undocumented ways. – user207421 Jan 28 '13 at 11:13
  • hi, Audrius is right I am asking to change the embedded ip address. Setting property changes only server object host address. But I need to know how to change embedded address of an object that is not server object. – emin Jan 30 '13 at 13:45