RMI Registry Naming Service


The RMI registry is the naming service used by RMI clients and servers. The purpose of a naming service is to bind a name to an object. Binding a name enables you to look up the name to retrieve a reference to the object. The server binds a name to the remote object when registering. The client looks up the name to retrieve a reference to the registered object. WebLogic programmers have access to two RMI naming services: the default Java 2 Standard Edition, J2SE, RMI registry and the WebLogic RMI registry. The WebLogic naming service is a drop-in replacement for the J2SE registry and therefore provides the same API. Only the import statements need to be changed. A full description of both naming services is provided in the following sections.

RMI Registry API

The API used by the client and server is provided by static methods in the Naming class. The methods are bind() , rebind() , unbind() , and lookup() which all pass the registered name as a URL in the first parameter. The format of the URL is

 
 //  host  :  port  /  Name  

If the well-known port 1099 is being used, you can leave off the : port field. If the host is localhost and the port is 1099, you need only specify the Name field. Some examples are

 
 //localhost:1099/MyObjectName //myhostname/MyObjectName //myhostname:2002/MyObjectName MyObjectName 

The RMI clients and servers communicate with the RMI registry through static methods in the Naming class. The remote object uses the bind() method to register a name with the remote object. The name may already be in use, which causes the AlreadyBoundException to be thrown. If you receive this message, you have three choices:

  • Bind a different name

  • Use the rebind() method to force the binding

  • Handle it as an error condition

The RMI client uses the Naming.lookup() method, passing the URL string as a parameter. The return value is an object that must be downcast to the remote interface that you were expecting to receive. The lookup() method uses the following three exceptions to indicate error conditions:

  • java.net.MalformedURLException ” The URL string is not syntactically correct.

  • java.rmi.NotBoundException ” The name is not bound to a remote object.

  • java.rmi.RemoteException ” The requested remote object is not available.

A complete RMI example is provided at the end of this chapter. Listing 12.1 is a snipet of code showing the usage of the Naming.lookup() method:

Listing 12.1 Example RMI lookup()
 try { // locate the remote object in the rmiregistry IHello hello = (IHello)Naming.lookup( "Hello" ); // use the remote object System.out.println( hello.getGreeting() ); } catch( MalformedURLException x ) { System.err.println( "URL is invalid:" + x.getMessage() ); } catch( NotBoundException x ) { System.err.println( "Name is not bound" + x.getMessage() ); } catch( RemoteException x ) { System.err.println( "Remote object is not available" + x.getMessage() ); } } 

Java 2 Standard Edition RMI Registry

The naming service executable for the J2SE RMI is rmiregistry . The RMI registry must be running for RMI clients to look up the remote interface that they want to use. The rmiregistry can be launched in two ways: either as a standalone process or within the remote object.

To run rmiregistry in the background on a Unix system, type

 
 rmiregistry & 

To run rmiregistry from a Windows command prompt, type

 
 start /min rmiregistry 

This command starts rmiregistry using the default port 1099. The command-line argument allows you to set the port to a different number. For example, if you want to run rmiregistry on port 2002, type the following:

 
 rmiregistry 2002 & 

As a convenience, the remote object can create an rmiregistry within its own process space by using the java.rmi.registry.LocateRegistry.createRegistry() method. The parameter is a port number. Again, the well-known port for rmiregistry is 1099. You can use 1099 or any other port that is available. This method throws RemoteException if an rmiregistry is already running on the given port. In most cases, this error is not fatal. Simply use the rmiregistry that is running. Refer to the sample RMI application shown later in this chapter in Listings 12.2 through 12.4 for sample code of a remote object and an RMI client. The code snippet to programmatically start the RMI registry is shown in Listing 12.2.

Listing 12.2 The RMI Registry Can Be Started Programmatically by the RMI Server
 // start the rmiregistry on the default port // check if rmiregistry is already running try { LocateRegistry.createRegistry(1099); } catch( RemoteException x ) { System.out.println( "Using rmiregistry that is already running" ); } 

WebLogic RMI Registry

WebLogic RMI registry replaces the standard RMI registry process. WebLogic RMI runs inside the WebLogic Server. No additional processes are necessary. The WebLogic RMI registry supports a protocol field in the bound URL. In addition to the standard rmi:// client protocol, http:// and https :// can be used as well.

The WebLogic RMI registry is integrated with the WebLogic JNDI. You can therefore bypass the RMI registry and use JNDI directly. This allows you to register the RMI services with the enterprise directory in the same manner that Enterprise Java Beans are registered.



BEA WebLogic Platform 7
BEA WebLogic Platform 7
ISBN: 0789727129
EAN: 2147483647
Year: 2003
Pages: 360

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net