Basic Understanding of RMI : JaVa - is not MaVa


RMI logo


The Remote Method Invocation(RMI) is an API that provides a mechanism to create distributed application in Java. RMI allows a Java object to invoke method on an object running on another machine. RMI provides remote communication between java programs.

---Watch the Video to understand "Why we need RMI ?"----





Concept of RMI application

A RMI application can be divided into two parts,
1. Client  program
2. Server program.
Server program creates some remote object, make their references available for the client to invoke method on it. A Client program make request for remote objects on server and invoke method on them. Stub and Skeleton are two important objects used for communication with remote object.

Stub and Skeleton

components of RMI

Stub acts as a gateway for Client program. It resides on Client side and communicates with Skeleton object. It establishes the connection between remote object and transmit request to it. Skeleton object resides on the Server side.

Stub Operation:
  • Acts as proxy for remote object.
  • Marshall parameters (converting the data or the objects in-to a byte-stream).
  • Send request and parameters to server skeleton.
Skeleton operation:
  • UN-Marshall parameters(converting the byte-stream back to their original data or object).
  • Perform computation
  • Marshall method return.
  • Send return object to client stub
RMI Registry
RMI registry is a server where :
  • Servers can register their object.
  • Clients can find server objects and obtain a remote references. Using the remote reference we can then invoke the required method.
                             
Watch this video to understand basic Implementation of RMI




RMI IMPLEMENTATION



    RMI Architecture


    Creating simple RMI application involves following steps:

    • Define a remote interface.
    • Implementing remote interface.
    • create and start remote application
    • create and start client application

    Define a remote interface

    A remote interface specifies the methods that can be invoked remotely by a client. Clients program communicate to remote interfaces, not to classes implementing it. To be a remote interface, a interface must extend the Remote interface of java.rmi package.
    import java.rmi.*;
    public interface AddServerInterface extends Remote
    {
    public int sum(int a,int b);
    }
    

    Implementation of remote interface

    For implementation of remote interface, a class must either extend UnicastRemoteObject or use exportObject() method of UnicastRemoteObject class.
    import java.rmi.*;
    import java.rmi.server.*;
    public class Adder extends UnicastRemoteObject implements AddServerInterface
    {
    Adder()throws RemoteException{
    super();
    }
    public int sum(int a,int b) throws RemoteException
    {
    return a+b; 
    }
    }
    

    Create Server and host rmi service

    You need to create a server application and host rmi service Adder in it. This is done using rebind() method of java.rmi.Naming class. rebind() method takes two arguments, first represent the name of the object reference and second argument is reference to instance of Adder
    import java.rmi.*;
    import java.rmi.registry.*;
    public class Server{
    public static void main(String args[]){
    try{
    AddServerInterface addService=new Adder();
    Naming.rebind("Sum",addService); 
    //addService object is hosted with name Sum 
    
    }catch(Exception e){System.out.println(e);}
    }
    }
    

    Create client application

    Client application contains a java program that invokes the lookup() method of the Naming class. This method accepts one argument, the rmi URL and returns a reference to an object of type AddServerInterface. All remote method invocation is done on this object.
    import java.rmi.*;
    public class Client{
    public static void main(String args[]){
    try{
    AddServerInterface st=(AddServerInterface)Naming.lookup("rmi://localhost/Sum");
    System.out.println(st.Sum(25,8));
    }catch(Exception e){System.out.println(e);}
    }
    }

    Steps to run this RMI application

    • compile all the java files
      javac *.java
    • Start RMI registry
      start rmiregistry
    • Run Server file
      java Server
    • Run Client file in another command prompt pass localhost at run time
      java Client localhost


    Goals of RMI
    • To minimize the complexity of applications.
    • Minimize the difference between working with local and remote objects
    • Make writing reliable distributed applications as simple as possible



    Would you like learn Java and get Certified from Oracle ?



    Popular posts from this blog

    MongoDB - daTa ModeLLinG Concepts: SQL - Where all to UsE ?

    Comparing Bootstrap With Google's Material Design Lite : Bootstrap - ResponSive Design

    MongoDB overView with Installation_procEss: SQL - Where all to UsE ?