Sunday, October 16, 2016

RMI(Remote Method Invocation)

The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM.
The RMI provides remote communication between the applications using two objects stub and skeleton.

/*Program of RMI application to display a hello world string*/

1) Creating Interface:-

//HelloInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface HelloInterface extends Remote
{
    String greeting() throws RemoteException;
}

2) Implementation of Interface:-

//HelloImpl.java

import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;

public class HelloImpl extends UnicastRemoteObject implements HelloInterface

{
    public HelloImpl() throws RemoteException
{
    }

    public String greeting() throws RemoteException
 {
        return "greeting";
    }

}
  
3) Create Server:-

//HelloServer.java

import java.rmi.registry.LocateRegistry;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;

public class HelloServer
{

public static void main(String[]args)throws RemoteException,                                       MalformedURLException
  {
    HelloInterface hello = new HelloImpl();
    Naming.rebind("rmi://127.0.0.1:1099/HelloService", hello);
    System.out.println("server.RMI Server is ready.");
  }
}

4) Create Client:-

//HelloClient.java

import java.rmi.RemoteException;
import java.rmi.NotBoundException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.net.MalformedURLException;
import java.rmi.Naming;
public class HelloClient

{
public static void main(String[] args) throws RemoteException,                       MalformedURLException, NotBoundException
  {
   
HelloInterface hello =   HelloInterface)Naming.lookup("//127.0.0.1:1099/HelloService");
System.out.println(“Hello world…!”);
 
  }
}

Output:

Compile all the source files and start the rmiregistry:-


Rmiregistry start:-


Execute HelloClient.java on another cmd_window:-

Example No.2

/*Program of RMI application to display addition of two number*/

1) Creating Interface:-

// CalculatorInterface.java

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface CalculatorInterface extends Remote

{
  public long add(long a, long b)throws RemoteException;
}

2) Implementing Interface:-

// CalculatorImpl.java


import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class CalculatorImpl extends UnicastRemoteObject                                                       implements CalculatorInterface
{
  protected CalculatorImpl() throws RemoteException
{
  super();
}
 public long add(long a, long b)throws RemoteException
{
  return a+b;
}
}

3) Create Server:-

// CalculatorServer.java

import java.rmi.Naming;

public class CalculatorServer
{
  CalculatorServer()
{
try
{
CalculatorInterface cal=new CalculatorImpl();
Naming.rebind("rmi://127.0.0.1:1099/CalculatorService",cal);
}
catch(Exception e)
{
  //System.out.println(e);
e.printStackTrace();
}
}
public static void main(String args[])
{
  new CalculatorServer();
}
}

4) Create Client:-

// CalculatorClient.java

import java.rmi.Naming;

public class CalculatorClient
{
  public static void main(String args[])
{
  try
{
CalculatorInterface cal=(CalculatorInterface)Naming.lookup("//127.0.0.1:1099/CalculatorService");
System.out.println("addition:"+cal.add(10,20));
}
catch(Exception e)
{
  System.out.println(e);
}
}
}
  
Output:-
Compile all the source files and start the rmiregistry:-


Rmiregistry start:


Execute CalculatorClient.java on another cmd_window:-


/*Program of RMI application to display a Factorial of given number*/