The sample RMI application you create in this section is a hit counter that keeps track of the number of times the remote object was called. It is compliant with the Java 2 Standard Edition RMI. The remote interface has only one method, getHitCount() . The remote interface is called IHitCounter , the remote object implementation is called HitCounter , and the RMI client is called HitCounterClient . Notice how the main() method in HitCounter registers the remote object with the rmiregistry and also how the HitCounterClient uses the Naming.lookup() method to locate the remote object. Creating a Java RMI application is a fairly straightforward process. The steps to create and execute the sample RMI application are as follows :
The remote interface defines the methods that are provided by the RMI server to the RMI client. Listing 12.3 shows the IHitCounter sample remote interface that defines the getHitCount() method. Note The methods in the remote interface are not required to throw RemoteException when WebLogic RMI is used. Listing 12.3 IHitCounter Remote Interface/** RMI Remote Interface for HitCounter */ // Imports import java.rmi.*; public interface IHitCounter extends Remote { // return number of times this method was called public int getHitCount() throws RemoteException; } The remote object provides the implementation for the remote interface. Listing 12.4 shows the HitCounter remote object that implements the IHitCounter remote interface. This example increments a hit counter and returns the value each time the getHitCount() method is called. The main() in HitCounter attempts to launch the RMI registry. If an exception is thrown, it is assumed that the RMI registry is already running and will use the running instance. The primary purpose of the main() is to bind the HitCounter remote object to the name "HitCounter" in the RMI registry. Listing 12.4 HitCounter Remote Object/** RMI Remote Object HitCounter */ // Imports import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; import java.net.*; public class HitCounter extends UnicastRemoteObject implements IHitCounter { // object attributes private int hitCount; /** Default constructor @throws RemoteException */ public HitCounter() throws RemoteException { hitCount = 0; } /** getHitCount @returns hitCount for this RMI Server @throws RemoteException */ public int getHitCount() throws RemoteException { // increment hitCount hitCount++; return hitCount; } /** main method @param args command line arguments */ public static void main( String[] args ) { // 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" ); } try { // create the remote object HitCounter hc = new HitCounter(); // register remote object with rmiregistry Naming.bind( "HitCounter", hc ); } catch( MalformedURLException x ) { System.err.println( x.toString() ) ; System.exit( -1 ); } catch( AlreadyBoundException x ) { System.err.println( x.toString() ); System.exit( -1 ); } catch( RemoteException x ) { System.err.println( x.toString() ); System.exit( -1 ); } System.out.println( "serving HitCounter" ); } } The HitCounterClient , shown in Listing 12.5 performs a lookup() operation on the RMI registry to find the HitCounter RMI server. The lookup() method returns an instance that implements the IHitCounter interface. The client calls methods defined by the IHitCounter interface to communicate with the HitCounter RMI server. Listing 12.5 HitCounterClient/** Client of RMI Remote Object HitCounter */ // Imports import java.rmi.*; public class HitCounterClient { /** main method @throws Exception */ public static void main( String[] args ) throws Exception { // locate the remote object in the rmiregistry IHitCounter ihc = (IHitCounter)Naming.lookup( "HitCounter" ); // use the remote object System.out.println( "count = " + ihc.getHitCount() ); } } ![]() |