Tuesday 13 December 2016

RMI:(Remote Method Innovation)

By,
Santosh

The Remote Method Invocation (RMI) is an application programming interface that provides a functionality to create distributed application in java. The Remote Method Invocation (RMI) allows an object to invoke method running in another Java virtual machine.
The Remote Method Invocation (RMI) provides remote communication / interaction between the applications using two objects stub and skeleton.
stub and skeleton
Remote Method Invocation (RMI) uses stub and skeleton object for communication/interaction with the remote.

A remote object is an object whose method can be invoked from another java virtual machine. Brief details of stub and skeleton objects:
stub
It is a gateway for the client side. All outgoing requests are gone through it. It place at the client side and represents the remote object.  caller invokes method on the stub object, it gone through the following tasks:
1. Connection with remote Virtual Machine,
2. Transmits the veriables to the remote Virtual Machine,
3.  Waits for result
4.  Return value or exception, and
5.  Finally returns value to caller.
skeleton
it is a gateway for the server side object. All incoming requests are gone through it. receives the incoming request, it gone through the following tasks:
1. Reads parameter for the remote method
2. Invokes the method on the actual remote object
3. Writes and transmits the result to caller.
Requirements for the distributed applications
1. The application need to locate the remote method
2. It need to provide the communication with the remote objects, and
3. The application need to load the class definitions for the objects.
Java RMI
steps to write the RMI program.
Create the remote interface
Implementation for the remote interface
Compile the implementation class and create the stub and skeleton objects
Start the registry service by rmiregistry tool
Start the remote application
Start the client application
Create the remote interface
creating and extend the remote interface, then declare the RemoteException with all methods of remote interface. Here, we are going to creating a remote interface which extends the Remote interface. There is only one method div() and it declares RemoteException.
import java.rmi.*;
public interface Division extends Remote
{
    public int div(int a, int b)throws RemoteException;
}
Provide the implementation of the remote interface
Now provide the implementation of the remote interface. For providing the implementation of the Remote interface, we require
• One is extend the UnicastRemoteObject class,
• Second one is use the exportObject() method of the UnicastRemoteObject class
import java.rmi.*;
import java.rmi.server.*;
public class DivisionRemote extends UnicastRemoteObject implements Division
{
    DivisionRemote()throws RemoteException
    {
        super();
    }
    public int div(int a, int b)
    {  
        return a+b;
    }
}

No comments:

Post a Comment