Using RMI over IIOP

Remote Method Invocation (RMI) is a Java-specific distributed-object system that allows you to create and use remote objects. For example, a Java client program running on one host can obtain a reference to an RMI service running on another host. Once the client program has the remote reference, it can invoke the methods of the remote object as if it were a local object.

As with CORBA applications, you write remote interfaces for an object and generate stubs and skeletons. Also like CORBA, RMI allows a client and a remote object to communicate through client stubs and server skeletons. The stub exposes the methods of the remote object, and the client makes requests against the stub. These requests are forwarded to the server and passed through the server skeleton to the remote object. Unlike CORBA, Java over RMI uses Java interfaces and not IDL files to define and use the remote service.

The original form of RMI used a proprietary protocol, Java Remote Method Protocol (JRMP), to allow objects to communicate. To support interoperability with CORBA, RMI was augmented to use IIOP as well as JRMP; EJB components use RMI-IIOP for client/server communication. The next example application will use RMI over IIOP to show how easy it is to write CORBA-compatible client and server classes.

The RMI over IIOP example allows a remote user to enter a name; the local server prefixes this name with hello and returns the string to the client. At a high-level, the process to achieve this is quite straightforward:

1.
Create an interface for the remote object.

2.
Write a Java server to implement the interface.

3.
Use rmic to generate IIOP stubs and skeletons.

4.
Use the CORBA naming service to advertise and look up the server.

5.
Write a client to look up the service name and call methods on the remote server object.

Listing 19.2 shows the code for the interface for the remote object. As you can see, the interface is quite straightforward, but there are a couple of points to note. The first is that the interface must extend java.rmi.Remote. The second is that all methods that the interface declares must throw a RemoteException.

Listing 19.2. HelloUser.java
import javax.rmi. ProtableRemoteObject;
import java.rmi.Remote;

public interface HelloUser extends Remote {
    public String sayHello(String s) throws RemoteException;
}

You must now create the implementation of this interface. Naturally, this implementation class must implement the HelloUser interface, but it must also extend javax.rmi.PortableRemoteObject. This class provides the server object with much of the basic functionality needed to support CORBA integration. Additionally, all of the class constructors must throw a RemoteException. The example server is shown in Listing 19.3.

Listing 19.3. RMI over IIOP Server HelloUserImpl.java
import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;
import javax.naming.*;
import javax.rmi.*;

public class HelloUserImpl extends PortableRemoteObject implements HelloUser {

    // Constructer msut throw RemoteException
    public HelloUserImpl() throws RemoteException {
    }

    public String sayHello(String name) throws RemoteException {
        System.out.println("sayHello "+name);
        return "Hello "+name;
    }
    public static void main(String args[]) {
        try {
            HelloUserImpl hui = new HelloUserImpl();

            Context ctx = new InitialContext();
            ctx.rebind("HelloUser",hui);
            System.out.println("Registered");
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

RMI over IIOP uses JNDI (see Day 3,”Naming and Directory Services”) to register the service name. The naming service implementation must be a CORBA naming service. A suitable CORBA Object Naming (COSNaming) Service is supplied with the J2SE; you don't need a J2EE server to work with RMI-IIOP as it is part of the JRE.

Before you can run the server example, you must use the rmic compiler supplied with the J2SE SDK to generate the RMI over IIOP skeleton and stub classes. The rmic compiler takes the server class name as a parameter and requires the –iiop switch for generating RMI over IIOP stub files. Following the directory layout scheme of the case study code you will need to enter the following command (from within the Day19/examples directory):

rmic –iiop –classpath classes –d classes HelloUserImpl

The command creates the class files HelloUserImpl_Tie (the skeleton) used by the server, and HelloUserImpl_Stub (the stub) used by the client. You have now generated all the files you require to run the server.

To run the hello service example, you must start the COSNaming service by entering the command:

tnameserv –ORBInitialPort 1050

This will tie up the current command line window so you will need to open a second window for the HelloUserImpl server.

The tnameserv application is provided with the JRE and by default runs the name service on port 900. As port 900 is a privileged port it is common practice to supply the command line argument –ORBInitialPort 1050 to run the service on the non-privileged port 1050.

As discussed on Day 3 you will need to set the JNDI properties for your HelloUserImpl server. The jndi.properties file supplied in the Day 19 examples directory contains suitable values for use with the JRE COSNaming service running on port 1050.

NOTE

If port 1050 on your system is used by another application choose, any free port above 1024 for your service. You will need to update the jndi.properties file in the Day19/examples directory to reflect the changed port number.


Once the COSNaming service is running, you can start the example HelloUserImpl server. Windows users will need to enter

java –classpath .;classes HelloUserImpl

Solaris/Linux users should enter

java –classpath .:classes HelloUserImpl

This will tie up the current command line window so you will need to open another window for the HelloUser client.

The client code is very simple; it uses JNDI to look up the server object example and then calls the required business methods. A simple client is shown in Listing 19.4.

Listing 19.4. HelloUserClient.java
import java.rmi.*;
import javax.naming.*;
import javax.rmi.*;

public class HelloUserClient {
    public static void main(String args[]) {
        if (args.length>1) {
            System.err.println("Usage: java HelloUserClient");
        }
        try {
            Context ctx = new InitialContext();
            Object obj = ctx.lookup("HelloUser");
            HelloUser hu = (HelloUser)PortableRemoteObject.narrow(obj,HelloUser.class);
            String reply = hu.sayHello(args.length==1?args[0]:"unknown");
            System.out.println (reply);
        }
        catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

This client is functionally no different from the EJB clients you have seen in examples from previous days in this book. You can now run the client program from the command line.

Windows users should enter

java –classpath .;classes HelloUserClient <name>
					

Solaris/Linux users should enter

java –classpath .:classes HelloUserClient <name>
					

The RMI-IIOP stub files previously generated using the rmic compiler must be in your client's CLASSPATH.

As you run the client you will see a diagnostic message displayed on the server's window as well as the reply message displayed in the client's window.

If you would rather use the supplied asant build files to run this example from the Day19/examples directory, enter the following asant commands to generate the stub files:

asant rmic

To start the COSNaming service, type

asant tnameserv

In a separate window start the HelloUserImpl server:

asant HelloUserImpl

In yet another window run the HelloUserClient application:

asant HelloUserImpl

Finally, to shut down the COSNaming service and your HelloUserImpl server, simply type Control-C in the appropriate window, or just close the window.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.74.56