Remote Method Invocation

Learn Development of RMI applications

RMI allows programmer to write method which can be invoked remotely. When we say remote it means, accessing the service from different machine.

Before RMI there was a similar concept in "C", known as RPC(Remote Procedure Call). RPC call procedure in same manner as local function.


Remote Method Invocation is calling method in java from remote machine. Method which can be called remotely also known as Service .

RMI concept used in developing distributed application and client-server application. Programmer no need to write any networking code for client and server. Java automatically generate networking code for client and server both sides. Client side networking code generated by java known as stub and server side code generate by java known as skeleton .


Stub and Skeleton is the component which provide actual communication on network.



Whenever client will call remote method, that request will be sent to stub. If there is any argument passed in method those arguments has to be sent to server. Stub will convert those argument in binary stream which can be transmitted on network. This conversion convert argument from java notation to network data notion (Network data representation) and send it to server.

Conversion of data from java notation to network notation is known as marshelling. Marshelling in java achieved by serialization.
Server side skeleton receive the request and convert arguments from network notation to java's notation. This is also known as Un-marshelling and execute the method. Value returned by method sent to skeleton. Skeleton perform marshelling on return value and send it to client stub. Client stub unpack the value and send it to calling program.



How client would know which method is available on which machine?

You can look at this problem in different way. Suppose you went to a shopping mall and you want to go to particular shop in that shopping mall. You are going first time then one way you will check every shop.



Another way to find shop is to look for map. In map you will find your shop and then you will go to shop directly.
Same situation occurred when a client want to invoke a remote method. But client don't know anything about address of machine having that method.

Client can check that method on every machine, but if network is large then it would take much time.
Can't we have map kind of solution discussed above.

In RMI which machine providing which service(remote method) is stored in a database. This database is maintained on a server, known as registry server. Whenever client want to use any server( or want to call any remote method), client will contact registry and ask which machine having asked service. Registry will reply to client about address of system having method.

All client has to remember is address of registry server.

All client need to know is registry server. Whenever client want to invoke any method he will interact with registry server and ask about address of server offering that service.

rmi registry provide that address. After client getting this address he will contact server directly.


Searching in registry for a service known as Look up Operation.
Naming.lookup() method is used for searching a service. This method require a url which include protocol name, registry server ip, port and service name.
Syntax:

    
  rmi://<ip_address_registry>:<port>/<service_name>  

Default port for registry server is 1099.


If registry server is running on same machine then url will be



  rmi://127.0.0.1:1099/<service_name>  


When a new method is created in network then one entry has to be created in registry server. This operation also known as bind.

In a network there are many system. If client want to call a specific method remotely how woult he know on which machine that service(remote method) is available. One way is client has to remember, which method stored on which machine.


For small number of system above approach is ok, but if number of services is very huge above solution is not possible.



Why packing and unpacking?

During communication between two machines, if some data has to be exchanged then this data has to be converted into streams of binary to transmit over wire. This conversion during RMI is done in java by using serialization concept. At the receiving side this wired version of data need to be changed in original form which is known as unpacking.



Implementation of RMI

RMI application has two parts client side and server side.

  1. Create a interface and declare method which we want to call remotely
    First step requires creating an interface by implementing Remote interface. Remote interface available in java.rmi package. In interface all declared method must throw RemoteException.


    import java.rmi.*;
    public interface addInterface extends Remote
    {
     public int addition(int a,int b) throws RemoteException;
    }
      

  2. Create a class by implementing interface created in first step and also extends class UnicastRemoteObject.


    import java.rmi.*;
    import java.rmi.server.*;
    public class addClass extends UnicastRemoteObject implements addInterface
     {
       public addClass() throws RemoteException
        {
    	  super();
    	}
       public int addition(int a,int b) throws RemoteException
       {
         return (a+b);
       }
     } 
    

  3. Create an entry in registry server by bind operation
    In this step one entry has to be created in registry server.

    import java.rmi.*;
    import java.net.*;
    public class addServer 
     {
       public static void main(String arg[])
        {
    	 try
    	  {
    	  addInterface a=new addClass();
    	  Naming.rebind("addService",a);
    	  } catch(Exception e)
    	    {
    		 e.printStackTrace();
    		}
    	}
     }
     

  4. Call method remotely


    import java.rmi.*;
    import java.net.*;
    public class addClient 
     {
       public static void main(String arg[])
        {
    	 try
    	  {
    	  addInterface a=(addInterface)Naming.lookup("rmi://127.0.0.1/addService");
    	  System.out.println("Addition:"+a.addition(123,23));
    	  }
    	  catch(Exception e)
    	   {
    	   e.printStackTrace();
    	   }
    	}
     }  
     


how to run program

  • First of all compile all java programs.

    javac *.java
    

  • In our case client and server both running on same mahcine. that's why ip address is 127.0.0.1. In case of error set CLASSPATH to directory where all classes are stored.
    Now start registry server by writing following command in cmd.

    start rmiregistry


  • Now run Server application

    java addServer

  • Run client application

    java addClient