The RMI Registry

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:

  1. The host that will be running MyRegistry must have an rmiregistry running.
  2. The rmiregistry either must be running in the same directory as the MyRegistry compiled class files or must include the location of these class files in its classpath. For example, if the compiled class files are in the bin directory, then
      rmiregistry -J-classpath -Jbin &
    
    will start the registry running in the background.
  3. When MyRegistry starts, it registers itself with its local rmiregistry.
  4. MyRegistry has methods that are essentially identical to those supported by rmiregistry.

A service registers itself as follows:

  1. It connects to the rmiregistry of the MyRegistry host to obtain a remote handle for MyRegistry.
  2. It registers itself with MyRegistry.
  3. Note that there is no restriction on the string used for identifying the service with MyRegistry.

A client obtains a remote object as follows:

  1. It connects to the rmiregistry of the MyRegistry host to obtain a remote handle for MyRegistry.
  2. It requests the remote handle for a service by giving MyRegistry the identifier of the service. This request will succeed only if the service previously registered itself with MyRegistry using this identifier.

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
342 WVH
College of Computer and Information Science
Northeastern University
360 Huntington Avenue
Boston, MA 02115
ken@baclawski.com
(617) 373-4631 / Fax: (617) 373-5121

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.