Simple entry point for single RMI service?

断了今生、忘了曾经 提交于 2019-12-22 14:12:34

问题


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)?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/11399408/simple-entry-point-for-single-rmi-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!