|
Java.rmi
Remote Method Invocation (RMI):
Remote
Method Invocation (RMI) allows a Java object that executes on
one machine to invoke a method of a Java object that executes
on another machine.This is an important feature, because it allows
you to build distributed applications. While acomplete discussion
of RMI is outside the scope of this book, the following example
describes the basic principles involved.
RMI Architecture:

RMI Layers:

Step One: Compile the Source Code
Sint.java
This application uses four source files. The first file, Sint.java, defines the remote interface that is provided by the server.
It contains one method that accepts two double arguments and returns their sum. All remote interfaces must extend the Remote interface, which is part of java.rmi. Remote defines no members.
Its purpose is simply to indicate that an interface uses remote methods. All remote methods canthrow a RemoteException.
import java.rmi.*;
public interface Sint extends Remote {
boolean eqtest(Object a) throws RemoteException;
int add(int a,int b) throws RemoteException;
String dsp(Object str) throws RemoteException;
}
Sintimp.java
The
second source file, Sintimp.java, implements the remote interface.
The implementation of the add( ) method is straight forward. All
remote objects must extend UnicastRemoteObject, which provides
functionality that is needed to make objects available from remote
machines.
import java.rmi.*;
import java.rmi.server.*;
public class Sintimp extends UnicastRemoteObject implements Sint {
Sintimp()throws RemoteException {
}
public boolean eqtest(Object a) throws RemoteException {
return true ;
}
public int add(int a,int b) throws RemoteException {
return (a+b) ;
}
public String dsp(Object str) throws RemoteException {
String s = (String) str ;
return s.toUpperCase();
}
}
The third source file,
Srmi.java, contains the main program for the server machine. Its primary function is to update the RMI registry on that machine.
This is done by using the rebind( ) method of the Naming class (found in java.rmi). That method associates a name with an object reference.
The first argument to the rebind( )method is a string that names the server as "Srmi". Its second argument is a reference to an instance
of Srmi.
Srmi.java
The
third source file, Srmi.java, contains the main program for the
servermachine
import java.rmi.*;
import java.net.*;
public class Srmi {
public static void main(String[] args)throws Exception {
Sintimp rob = new Sintimp();
Naming.rebind("equal",rob);
System.out.println("Server is waiting for Client to Connect :");
}
}
Crmi.java
The
fourth source file, Crmi.java, implements the client side of this
distributed application.
import java.rmi.*;
public class Crmi {
public static void main(String[] args) {
try {
String url= "rmi://127.0.0.1/equal";
Sint in = (Sint) Naming.lookup(url);
System.out.println("The two Objects are : "+in.add(10,20));
System.out.println("The String in Upper case : "+in.dsp("This will displays in Upper Case :"));
}catch(Exception e) {
e.printStackTrace();
}
}
}
Step Two: Generate Stubs and Skeletons
Before you can use the client and server, you must generate the necessary stub. You may also need to generate a skeleton. In the context of RMI, a stub is a Java object that resides on the client machine. Its function is to present the same interfaces as the
remote server. Remote method calls initiated by the client are actually directed to the stub. The stub works with the other parts of the RMI system to formulate a request that is sent to the remote machine.
A remote method may accept arguments that are simple types or objects. In the latter case, the object may have references to other objects. All of this information must be sent to the remote machine. That is, an object passed as an argument to a remote method call must be serialized and sent to the remote machine.
A skeleton is a Java object that resides on the server machine. It works
with the other parts of the RMI system to receive requests, perform deserialization,and invoke the appropriate code on the server. Because many
readers will want to generate the skeleton, one is used by this example.
If a response must be returned to the client, the process works in reverse. Note
that the serialization and deserialization facilities are also used if objects are returned to a client.
To generate stubs and skeletons, you use a tool called the RMI compiler, which is invoked from the command line, as shown here:
rmic Sintimp
Step Three: Install Files on the Client and Server Machines
Copy Crmi.class, Sintimp_Stub.class, and Sint.class to adirectory on the client machine. Copy Sint.class,Sintimp.class, Sintimp_Skel.class, Sintimp_Stub.class, and Srmi.class to adirectory on the server machine.
Step Four: Start the RMI Registry on the Server Machine
The Java 2 SDK provides a program called rmiregistry, which executes on the server machine. It maps names to object references. First, check that the CLASSPATH environment variable includes
the directory in which your files are located. Then, start the RMI Registry from the command line, as shown here:
start rmiregistry

Step Five: Start the Server
The server code is started from the command line, as shown here:
java Srmi Recall that the Srmi code instantiates Sintimp and registers that object with the name "Srmi".
Step Six: Start the Client
The Crmi software requires three arguments: the name or IP address of the server machine and the two numbers that are to be summed together.
Crmi
Note:
For Server Side:
* javac Sint.java
* javac Sintimp.java
* javac Srmi.java
* rmic Sintimp
* start rmiregistry
* java Srmi
For Client Side:
* javac Crmi.java
* java Crmi
|