One of the limitations of the rmiregistry is that an RMI service can only register itself on the rmiregistry on the same host. Thus it seems, at first, that every host that has RMI services on it must have an rmiregistry. However, one can remove this limitation of rmiregistry by reimplementing its functionality. Call this reimplemented rmiregistry MyRegistry. It is then only necessary for MyRegistry to have a local rmiregistry. Other services can register with MyRegistry no matter what host they are on.
Here is how one starts MyRegistry:
rmiregistry -J-classpath -Jbin &will start the registry running in the background.
A service registers itself as follows:
A client obtains a remote object as follows:
There are also methods in MyRegistry for unregistering a service and for obtaining a list of all the names of registered services. In general, MyRegistry implements the java.rmi.registry.Registry interface.
The advantage of this approach is that clients only need to know a single URL in order to access any service. It allows services to be moved from one host to another without changing any of the client code. One can also have multiple copies of a service on different hosts, all identified by the same string. When a service is requested, MyRegistry can randomly select one of them in order to balance the load.
Some sample code is available in MyRegistry.java and ServiceGroup.java. Note that this code is only conceptual and has never been tested.
Ken Baclawski
The original for this document is at https://ken.baclawski.com/tutorial/rmiregistry.html.
© 1998, 2013 Kenneth Baclawski. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that redistributions and uses retain this copyright notice.