How does a client that needs a remote object locate that object on a distant server? More precisely, how does it get a remote reference to the object? Clients find out what remote objects are available by querying the server's registry . A registry advertises the availability of the server's remote objects. Clients query the registry to find out what remote objects are available and to get remote references to those objects. You've already seen one: the java.rmi.Naming class for interfacing with registries.
The Registry interface and the LocateRegistry class allow clients to retrieve remote objects on a server by name . A RegistryImpl is a subclass of RemoteObject , which links names to particular RemoteObject objects. Clients use the methods of the LocateRegistry class to retrieve the RegistryImpl for a specific host and port.
18.5.1 The Registry Interface
The java.rmi.registry.Registry interface has five public methods: bind( ) , to bind a name to a specific remote object; list( ) , to list all the names bound in the registry; lookup( ) , to find a specific remote object given its URL; rebind( ) , to bind a name to a different remote object; and unbind( ) , to remove a name from the registry. All of these behave exactly as previously described in the java.rmi.Naming class, which implements this interface. Other classes that implement this interface may use a different scheme for mapping names to particular objects, but the methods still have the same meaning and signatures.
Besides these five methods, the Registry interface also has one field, Registry.REGISTRY_PORT , the default port on which the registry listens. Its value is 1099.
18.5.2 The LocateRegistry Class
The java.rmi.registry.LocateRegistry class lets the client find the registry in the first place. This is achieved with five overloaded versions of the static LocateRegistry.getRegistry() method:
public static Registry getRegistry( ) throws RemoteException public static Registry getRegistry(int port) throws RemoteException public static Registry getRegistry(String host) throws RemoteException public static Registry getRegistry(String host, int port) throws RemoteException public static Registry getRegistry(String host, int port, // Java 1.2 RMIClientSocketFactory factory) throws RemoteException
Each of these methods returns a Registry object that can be used to get remote objects by name. LocateRegistry.getRegistry( ) returns a stub for the Registry running on the local host on the default port, 1,099. LocateRegistry.getRegistry(int port) returns a stub for the Registry running on the local host on the specified port. LocateRegistry.getRegistry(String host) returns a stub for the Registry for the specified host on the default port, 1,099. LocateRegistry.getRegistry(String host , int port) returns a stub for the Registry on the specified host on the specified port. Finally, LocateRegistry.getRegistry(String host , int port , RMIClientSocketFactory factory) returns a stub to the registry running on the specified host and port, which will be contacted using sockets created by the provided java.rmi.server.RMIClientSocketFactory object. If the host String is null , getRegistry( ) uses the local host; if the port argument is negative, it uses the default port. Each of these methods can throw an arbitrary RemoteException .
For example, a remote object that wanted to make itself available to clients might do this:
Registry r = LocateRegistry.getRegistry( ); r.bind("My Name", this);
A remote client that wished to invoke this remote object might then say:
Registry r = LocateRegistry.getRegistry("thehost.site.com"); RemoteObjectInterface tro = (RemoteObjectInterface) r.lookup("MyName"); tro.invokeRemoteMethod( );
The final two methods in the LocateRegistry class are the overloaded LocateRegistry.createRegistry( ) methods. These create a registry and start it listening on the specified port. As usual, each can throw a RemoteException . Their signatures are:
public static Registry createRegistry(int port) throws RemoteException public static Registry createRegistry(int port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf) // Java 1.2 throws RemoteException