Now that you have a basic understanding of RMI, you are ready to write simple RMI applications. The steps in developing an RMI application are shown in Figure 36.4.
1. | Define a server object interface that serves as the contract between the server and its clients , as shown in the following outline: public interface ServerInterface extends Remote { public void service1(...) throws RemoteException; // Other methods } A server object interface must extend the java.rmi.Remote interface. |
2. | Define a class that implements the server object interface, as shown in the following outline: public class ServerInterfaceImpl extends UnicastRemoteObject implements ServerInterface { public void service1(...) throws RemoteException { // Implement it } // Implement other methods } The server implementation class must extend the java.rmi.server.UnicastRemoteObject class. The UnicastRemoteObject class provides support for point-to-point active object references using TCP streams. |
3. | Create a server object from the server implementation class and register it with an RMI registry: ServerInterface server = new ServerInterfaceImpl(...); Registry registry = LocateRegistry.getRegistry(); registry.rebind( "RemoteObjectName" , obj); |
4. | Develop a client that locates a remote object and invokes its methods, as shown in the following outline: Registry registry = LocateRegistry.getRegistry(host); ServerInterface server = (ServerInterfaceImpl) registry.lookup( "RemoteObjectName" ); server.service1(...); The example that follows demonstrates the development of an RMI application through these steps. |
This example creates a client that retrieves student scores from an RMI server. The client, shown in Figure 36.5, displays the score for the specified name .
1. | Create a server interface named StudentServerInterface in Listing 36.1. The interface tells the client how to invoke the server's findScore method to retrieve a student score. Listing 36.1. StudentServerInterface.java
| |
2. | Create a server implementation named StudentServerInterfaceImpl (Listing 36.2) that implements StudentServerInterface . The findScore method returns the score for a specified student. It returns “1 if the score is not found. Listing 36.2. StudentServerInterfaceImpl.java(This item is displayed on pages 1248 - 1249 in the print version)
StudentServerInterfaceImpl implements the findScore method (lines 22 “34) defined in StudentServerInterface . For simplicity, three students, John, Michael, and Michelle, and their corresponding scores are stored in an instance of java.util.HashMap named scores . HashMap is a concrete class of the Map interface in the Java Collections Framework, which makes it possible to search and retrieve a value using a key. Both values and keys are of Object type. The findScore method returns the score if the name is in the hash map, and returns -1 if the name is not found (line 27). | |
3. | Create a server object from the server implementation and register it with the RMI server (Listing 36.3). Listing 36.3. RegisterWithRMIServer.java
| |
4. | Create a client as an applet named StudentServerInterfaceClient in Listing 36.4. The client locates the server object from the RMI registry and uses it to find the scores. Listing 36.4. StudentServerInterfaceClient.java(This item is displayed on pages 1250 - 1251 in the print version)
The initializeRMI() method treats standalone applications differently from applets. The host name should be the name where the applet is downloaded. It can be obtained using the Applet 's getCodeBase().getHost() . For standalone applications, the host name should be specified explicitly. The lookup(String name) method (line 63) returns the remote object with the specified name. Once a remote object is found, it can be used just like a local object. The stub and the skeleton are used behind the scenes to make the remote method invocation work. | |
5. | Follow the steps below to run this example.
|
Note
You must start rmiregistry from the directory where you will run the RMI server. Otherwise, you will receive the error ClassNotFoundException on StudentServerInterfaceImpl_Stub . |
Note
Server, registry, and client can be on three different machines. If you run the client and the server on separate machines, you need to place StudentServerInterface on both machines. If you deploy the client as an applet, place all client files on the registry host. |
Caution
If you modify the remote object implementation class, you need to restart the server class to reload the object to the RMI registry. In some old versions of rmiregistry, you may have to restart rmiregistry. |