70 likes | 238 Vues
The RMI Registry. CS-328 Dick Steflik. The registry. A standalone RMI Naming Service run from the OS commandline The java.rmi.registry package defines the low level API for the registry and the registry interface and its methods. bind(), rebind(), list() and unbind()
E N D
The RMI Registry CS-328 Dick Steflik
The registry • A standalone RMI Naming Service • run from the OS commandline • The java.rmi.registry package • defines the low level API for the registry and the registry interface and its methods. • bind(), rebind(), list() and unbind() • Can be used to find or create a registry
LocateRegistry • Java.rmi.registry.LocateRegistry class • used to get a handle on a registry that can then be queried using registry interface methods Registry r = java.rmi.registry.LocateRegistry.getRegistry(“www.foo.com”); MyRemoteObject o = (MyRemoteObject)r.lookup(“MyClass”); - MyClass is not treated as a URL, it is the name of the that the object being queried - getRegistry returns a reference to an instance of a registry running on the remote system
Example Registry reg; MyRemoteObject ro; try { ro = new MyRemoteObject(); // get a handle on the local registry server reg = java.rmi.registry.LocateRegistry.getRegistry(); // bind the object to the registry reg.bind(“MyObject”, ro); } catch ( RemoteException e ) {out.println(“Couldn’t locate registry”); } catch ( AccessException e ) { // registry didn’t allow access out.println(“rebind(), access was denied”); }
Implementing the registry interface • Can be started only on the machine that is hosting the application • should be used with ports above 1024 • use createRegistry method of java.rmi.registry.LocateRegistry class
Example Registry reg; // start up a registry object try { reg = java.rmi.registry.LocateRegistry.createRegistry(5100); } catch (RemoteException e { out.ptintln(“Couldn’t create registry, try another port”); } // create an object and register it try { MyRemoteObject ro = new MyRemoteObject(); reg.rebind(“MyObject” , ro ); } catch (RemoteException e) { out.println(“error creating remote object or binding to registry”); } catch (AccessException ax) { out.println(“ operation rebind, not allowed”); }
Example (cont.) // print a list of all objects bound to the registry String list[ ]; try { // get a list of the object names list = reg.list(); } catch (RemoteException rx) { out.println(“list method failed”);} catch (AccessException ax) { out.println(“Access denied”); } // print the list for (int j=0 ; j < list.length; j++) out.println( list[j]);