2

I have several services which export an RMI interface.

They used to offer this by creating their own registry (with LocateRegistry.createRegistry) and binding it there. However, that became impossible when the services were moved to run as separate applications in the same VM (Tomcat), because for some reason only one registry can be present there.

I worked around this by using a central registry for all the services. Even then, I'm not really interested in the multi-object registry role of a registry, just its entry point facilities. A central registry, however, introduces more complexity (e.g. it must be started first, it must have the interfaces of services it registers).

Is there a way to bring back the situation where each service indepently offers an entry point to its RMI interface, while having them run in the same VM (which is a hosting detail, not part of the design)?

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301

3 Answers3

0

I'd forgotten that I asked a similar question already (for a different reason, reducing code, before I moved services together in 1 VM), where the first answer suggests a way to circumvent the registry:

Use UnicastRemoteObject, serialize the stub obtained when you export the object, and use a shared file, or a socket, or sneakernet, to make the stub available to clients.

Community
  • 1
  • 1
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
0

You can't have more than one Registry per JVM, because the Registry has a fixed RMI object ID. Just adjust all your servers to start like this:

static Registry registry;
// ...
try
{
  registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
}
catch (...) // whatever the exception is, probably ExportException
{
  registry = LocateRegistry.locateRegistry(Registry.REGISTRY_PORT);
}
registry.rebind(...); // etc

Then whichever one of them starts first will start the Registry and the others will use it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I did that, for a while, but that's even more complex and error prone. The whole architecture then depends on whichever service happens to start first. If it stops/restarts, all the other ones need to rebind there, and are unavailable while service 1 restarts. Also, it brings a hosting detail (one VM) into the code. – Bart van Heukelom Jul 10 '12 at 07:45
  • @BartvanHeukelom No it doesn't. It doesn't matter which RMI server starts first. If they all do as above, the one that starts first will create the registry and the others will locate it. I'm not aware what 'hosting detail' is being brought into play when a hostname isn't even mentioned. – user207421 Jul 10 '12 at 10:15
  • Indeed, it doesn't matter *which* starts first, but whatever service it is, the whole system then depends on it, and it can't be stopped or restarted. By hosting detail I mean that the fact that the applications share a VM is a matter of hosting/server setup, not relevant to their design. – Bart van Heukelom Jul 10 '12 at 10:20
  • @BartvanHeukelom So now you are back with running it as a separate process. You can't have it both ways. – user207421 Jul 10 '12 at 12:47
  • Well, I can, by using option 3 from your answer to the other question. But I'm wondering whether the client can maybe synthesize the entire remote stub, which would be even easier. – Bart van Heukelom Jul 10 '12 at 12:50
  • @BartvanHeukelom Well that's yet another question. The answer is 'yes', because locateRegistry() does exactly that, but you have to export the object with a fixed objectID, which is non-trivial, uses Sun APIs, etc. Have a look at `locateRegistry()` and the source code of `RegistryImpl` if you want to follow that path. – user207421 Jul 11 '12 at 00:59
0

If you're still interested in this problem a year later....

It is possible to start multiple registries within the same JVM. Just call LocateRegistry.getRegistry with distinct ports. You have to have well-known ports for each service, but I think you're doing that already if you've implemented option 3 from this answer to the other question.

Long ago there was a bug that prevented multiple registries from coexisting in the same JVM, but this was fixed in JDK 5. There may be something in Tomcat that prevents multiple RMI registries from running. Or, it could be that the version of Tomcat you were using was on top of a very old JDK.

Community
  • 1
  • 1
Stuart Marks
  • 127,867
  • 37
  • 205
  • 259