Remote Method Invokation (RMI) in Java

# Remote Method Invokation (RMI) in Java #

  • What is RMI ?
  1.  RMI is an API that provide an mechanism to create distributed application in java.
  2. RMI allow an object to invoke the method of an object  running on another JVM(Java virtual Machine).
  3. RMI provide Remote Communication to the application using two special objects which are stub and skeleton .


  • what is stub ?
  1. stub is an object act as gateway for client side application.
  2. All outgoing request are routed by stub.
  3. Following are the tasks of stub.  

    •  To establish the connection with the remote JVM.
    • To write and trasmit the parameters to the remote object. (Marshalling) 
    • stub wait for the result.
    • To read the return value from remote object.(Unmarshalling)
    • To send the result to its caller.
 

  • what is skeleton ?
  1. skeleton is an object act as gateway for server side application.
  2. All incoming request are routed by stub.
  3. Following are the tasks of skeleton.  

    • To read the parameters from caller
    • Invoke the method
    • To write and transmit the result to its caller .

  • Steps to create remote application
  1. Create Remote Interface 
  2. Create Class for implementing Remote Interface.
  3. Compile the class and interface and create stub and skeleton object using rmi tools.
  4. Start the registry service using rmiregistry.
  5. Create Class Server and Start server.
  6. Create Class Client and Start Client.

# Practical Implementation of RMI #

      • Create Remote Interface
code :
     import java.rmi.Remote;
public interface AddI extends Remote
{
    public int add(int a,int b) throws Exception;
}

      

  • Create Class for implementing Remote Interface.
code :
import java.rmi.server.*;
public class AddC extends UnicastRemoteObject implements AddI
{
      public AddC() throws Exception
     {
        super();
     }
      public int add(int a,int b)
     {
         return a+b;
     }
  • Create Server Class
Code :

import java.rmi.*; public class Server { public static void main(String a[]) throws Exception { AddC obj = new AddC(); Naming.rebind("ADD",obj); // Register the obj and create caption System.out.println("Server started"); } }

  • Create Client Class
Code:
import java.rmi.*;
public class Client
{
public static void main(String a[]) throws Exception
{
AddI obj = (AddI)Naming.lookup("ADD");
int n = obj.add(4,5);
System.out.println("Addition is : "+n);
}
}
  • Compile All Class and Interface
Note : you can compile them one by one or at onces if they are in one folder by
using command " javac *.java ".


  • Create Stub.
  1. open one terminal and locate it to folder where classes are store .
  2. Execute command "rmic  AddC ". 
  3. " AddC_Stub.class " file were created.
 
  • Start Registry.

  1. Execute command " start rmiregistry ".     

                 
  • Start Server.
    1. open new terminal and Execute command " java Sever ".

  • Start Client.
    1. open new terminal and Execute command " java Client ".
    2. you will get output " Addition is : 9 ".

THE END



Comments

Post a Comment