Wednesday, August 26, 2015

RMI (Remote Method Invocation)

The RMI (Remote Method Invocation) is an API that help developer to create distributed application in java.

With the help of RMI developer can invoke methods on an object running in another JVM.

The RMI provides remote communication between the applications using two objects stub and skeleton.

Stub

The stub is an gateway object resides at the client side. All the outgoing requests are passed through it. When the caller invokes method on the stub object, it does the following tasks:

- Initiates a connection with remote Virtual Machine (JVM),
- Writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
- Reads (unmarshals) the return value or exception, and
- Finally, returns the value to the caller.


Skeleton

The skeleton is an gateway resides on the server side . All the incoming requests are passed through it. When the skeleton receives the incoming request, it does the following tasks:

-reads the parameter for the remote method that is send to it from stub.
-invokes the method on the actual remote object
-writes and transmits (marshals) the result to the caller.

Diagram



Following are the steps involved in RMI program

1- Create the remote interface
2- Provide the implementation of the remote interface
3- Compile the implementation class
4- Create the stub and skeleton objects using the rmic tool
5- Start the registry service by rmiregistry tool
6- Create and start the Server application
7- Create and start the client application

Lets try to have one smaller example


1- Create the remote interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Adder extends Remote{  
public int add(int x,int y)throws RemoteException;  
}  

2- Provide the implementation of the remote interface
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class AdderRemote extends UnicastRemoteObject implements Adder{  
AdderRemote()throws RemoteException{  
super();  
}  
public int add(int x,int y){return x+y;}  
}
3- Compile the implementation class
4- Create the stub and skeleton objects using the rmic tool




5- Create and start the Server application
import java.rmi.*;  
import java.rmi.registry.*;  
public class MyServer{  
public static void main(String args[]){  
try{  
Adder stub=new AdderRemote();  
Naming.rebind("rmi://localhost:5000/siddhu",stub);  
}catch(Exception e){System.out.println(e);}  
}  
}



6- Create and start the client application
import java.rmi.*;  
public class MyClient{  
public static void main(String args[]){  
try{  
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/siddhu");  
System.out.println(stub.add(34,4));  
}catch(Exception e){}  
}  
}




In this was we can write server class that has connection with the DB or calling 3rd Party application and by doing RMI we can allow Client residing on the same or different JVM can call that method to perform operation.

No comments: