6. Using Wrappers and Java RMI
Our main emphaisis has been on the construction and use of wrapper classes that utilize the cam.netapp package to provide the underlying communication link, however, it is also possible to use the same techniques to create wrapper classes that utilize the Java RMI package as the communications link. In particular, the application cam.codegen.CreateRMI takes an existing class and creates the appropriate wrapper class and wrapper interface so that it is in the ``server'' form required by the Java RMI interface.
Consider again our first example, the application whose components are contatined in Sample1/App.java and Sample1/Aclass.java. The creation of a distrubuted version that uses the Java RMI package requires
The application cam.codegen.CreateRMI creates the required server classes.
When applied to Aclass, the application creates the server wrapper class AclassRS.java
( (R)MI (S)erver) and the server interface AclassRI.java
((R)MI (I)nterface). As with all RMI server classes, the JDK
rmic utility program is applied to AclassRS
to create the stub and skeleton classes AclassRS_Stub and AclassRS_Skel.
The interface class, AclassRI.java, extends java.rmi.Remote and contains all the public methods of Aclass. The interface is not identical to that of Aclass, because each method throws java.rmi.RemoteExceptions (as required of any class that extends extends java.rmi.Remote). The server class AclassRS.java implements the AclassRI interface and obtains its functionality by invoking the appropriate methods of an Aclass instance that AclassRS contains. Also included in AclassRS is the requisite
main(...) invocation that sets the SecurityManager and registers
the class with the rmiregistry.
After this packaging of Aclass into a "server" form, the client appliction App must be modified to use a reference of type AclassRI. In addition to changing the data member declaration, the constructor requires modification and all AclassRI method calls must placed within a try-catch block The modificed code is presented inSamples6/App.java.
Prepretory to running the distributed application requires appropriately placing class components on the local and remote machine. The location of the files is given in the following table
|App.class (uses AclassRI)||Aclass.class|
rmiregistryon the remote machine, and then starts an instance of AclassRS (e.g. by running a statement of the form
java AclassRS). The client application App is then invoked and it communicates AclassRS on the remote machine.