|
Short notes on RMI [ Tutorial ] |
|
|
Written by Veena Devi
|
This is a short tutorial on RMI
Remote Method Invocation
Examples of Use
• Database access
• Computations
• Any custom protocol
• Not for standard protocols (HTTP, FTP, etc.)


General RMI architecture
- The server must first bind its name to the registry
- The client lookup the server name in the registry to establish remote references.
- The Stub serializing the parameters to skeleton, the skeleton invoking the remote method and serializing the result back to the stub.

Stub and skeleton
- A client invokes a remote method, the call is first forwarded to stub.
- The stub is responsible for sending the remote call over to the server-side skeleton
- The stub opening a socket to the remote server, marshaling the object parameters and forwarding the data stream to the skeleton.
- A skeleton contains a method that receives the remote calls, unmarshals the parameters, and invokes the actual remote object implementation.

Steps to develop a RMI system
- Define the remote interface
- Develop the remote object (Server object) by implementing the remote interface.
- Develop the client program.
- Compile the Java source files.
- Generate the client stubs and server skeletons.
- Start the RMI registry.
- Start the remote server objects.
- Run the client
Step1: Defining remote interface
To create a RMI application, the first step is defining of a remote interface between the client and server objects.
/* InterfaceRMI.java */
import java.rmi.*;
public interface InterfaceRMI extends Remote{
int cube(int x) throws RemoteException;
}
Step2: Develop the server object
The server is a simple unicast remote server.
Create server by extending
java.rmi.server.UnicastRemoteObject
/*ServerRMI.java*/
import java.rmi.*;
import java.rmi.server.*;
public class ServerRMI extends UnicastRemoteObject implements InterfaceRMI {
public ServerRMI() throws RemoteException {
super();
}
...
The server class needs to implement the remote methods
public int cube(int x) throws RemoteException{
return x*x*x;
}
The server must bind its name to the registry, the client will look up the server
name.
Use java.rmi.Naming class to bind the server name to registry. In this example the name by which it is registered is “MyServer”.
...
public static void main (String a[]){
try {
ServerRMI remoteServer = new ServerRMI();
Naming.rebind("MyServer", remoteServer );
System.out.println("System is ready");
}catch(Exception e){
System.out.println(e);
}
}
}
Step 3: Develop the client program
In order for the client object to invoke methods on the server, it must first look
up the name of server object in the registry.
Use the java.rmi.Naming class to lookup the server name.
The server name is specified as URL in the from( rmi://host:port/name )
Default RMI port is 1099.
The name specified in the URL must exactly match the name with which the server object has bound to the registry. In this example, the name is "MyServer"
The remote method invocation is programmed using the remote interface reference name.function name
/*ClientRMI.java*/
import java.rmi.*;
public class ClientRMI {
public static void main(String a[])throws RemoteException{
try{
InterfaceRMI obj=(InterfaceRMI)
Naming.lookup("//localhost/MyServer");
int no=obj.cube(4);
System.out.println("The value is : " + no);
}
catch(Exception e){
System.out.println(e);
}
}
}
Step 4: compiling all the source files
Assume that the code being compiled and run from C:\ in the command prompt
Compile all the 3 sourcecode using
C:\ javac *.java
Step 5: generating stubs and skeleton
Now the stub and skeleton need to be generated using rmic (rmi compiler)
C:\ rmic ServerRMI
Step 6: starting the RMI registry
The rmiregistry must be started using
C:\ start rmiregistry
This step needs to be done before the server program starts as the server
object has to be registered
Step 7: start the server program
Once the Registry is started, the server can be started and will be able to store
itself in the Registry
Start the server program
C:\ java ServerRMI
Step 8: start the client program
The client program can now access the server program
C:\ java ClientRMI
For the client it seems as if the function call is on the same machine
|