Usage
Listings Three and Four illustrate the server and client side usage of our remote RMI registry (also see Figure 2).
Listing Three is a complete example for the definition of a simple RMI server object. It comprises the remote interface Hello, the implementation class HelloImpl, as well as the class HelloSetup. The latter class contains the code for startupsetting the code base from where RMI clients can download server object's code, instantiating and exporting the RMI server object, locating the remote registry, and (re)binding the server object with the remote RMI registry. The use of our remote RMI registry is completely transparent to the RMI server; no modifications have been made to the standard RMI code. With the standard RMI registry, however, the call registry.rebind("hello", stub); would have thrown an AccessException. With our custom RMI Registry, RMI servers are allowed to invoke bind, rebind, and unbind methods of a remote RMI Registry.
Listing Four shows a sample RMI client that uses the hello RMI server object. Exactly as with the standard RMI architecture, the client locates the RMI Registry, looks up the hello server object, and then invokes methods at the server object.
// Remote interface public interface Hello extends Remote { String getHello() throws RemoteException; } // Implementation class public class HelloImplimplements Hello { public String getHello() throws RemoteException { return "hello, world" ; } } // Startup of RMI serverobject, including registration of // the instantiated server object with remote RMI registry public class HelloSetup { public static void main (String[] args) throws UnknownHostException,RemoteException, MalformedURLException,NotBoundException, InterruptedException,AlreadyBoundException { System.setProperty ("java.rmi.server.codebase", "http://192.168.2.32/hello.jar"); Hello stub = (Hello) UnicastRemoteObject. exportObject (new HelloImpl(),0); //registration with remote RMI-Registry is now possible Registry registry = LocateRegistry.getRegistry ("192.168.2.31"); registry.rebind ("hello",stub); System.out.println ("Successfully registered."); } }
public class HelloClient { public static void main (String[] args) throws RemoteException,NotBoundException { Registry registry = LocateRegistry.getRegistry ("192.168.2.31"); Hello server = (Hello) registry.lookup ("hello"); System.out.println (server.getHello ()); } }
Conclusion
A remote RMI registry is useful in many distribution scenarios. A completely transparent custom registry implementation needs to resolve several interesting technical obstacles, such as how to export a remote object with a given ObjID, or how to check the validity of an existing binding. The complete source code as well as the executable jar file can be found at the RRR project homepage (www-home.htwg-konstanz.de/~haase/hp/RRR.html).