Monday, November 14, 2011

Remote Method Invocation(RMI) Example

Here, we will learn basics of RMI and will implement a small example

Remote Method Invocation Basics
As the name suggests,it is a system to call remote method,which may be on other JVM or other computer. It is built on top of Sockets. But,you don't have to deal with sockets and protocols directly. You can directly call a method on some other machine and it works,as if you have called it locally. To achieve this it passes the objects over the network(objects should be Serializable).

Drawback: RMI can only be used in java. Interaction with other language platform is not possible.

Requirements of RMI
Client Requirement
Let us suppose there is a server Object which is registered.
* Client Object have to find the Server Object(by looking it up in registry)
* Serialize the parameters and send
* Deserialize the response from the server

Server Requirement
* Server should be a remote object
* The remote object should be registered(rmiregistry is used for registration)

Example

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

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

Hello.java
import java.rmi.RemoteException;
public class Hello implements HelloInterface 
{

private String message; 
public Hello (String msg) throws RemoteException {
message = msg;
 }
public String sayHello() throws RemoteException {
 return message;  
 }
}
HelloServer.java
import java.net.MalformedURLException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

class HelloServer {

 public static void main(String[] argv)  throws RemoteException, MalformedURLException {
  
  try{
   Hello remoteObj=new Hello("This is test RMI");
    Remote obj = UnicastRemoteObject.exportObject(remoteObj, 9502);
          Registry r = LocateRegistry.createRegistry(9502);
          r.bind("RemoteHelloServer", obj);
   System.out.println("Hello Server is ready.");
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  
 }
}

Steps for compilation:
1)javac HelloInterface.java
2)javac Hello.java
3)javac HelloWorld.java
4)rmiregistry //In a separate command prompt
5)rmic -vcompat Hello //will generate Hello_Stub.class and Hello_Skel.class
6)java HelloWorld

After step 6,Server is up

Steps for client code:
* Copy the HelloInterface.java and Hello_Stub.class file in client area
* Write a client program HelloClient.java, which looks up in registry.

HelloClient.java
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

class HelloClient {
 public static void main(String[] args) {
  try {
   Registry registry;
  HelloInterface hello;
  registry=LocateRegistry.getRegistry(
                "localhost",(new Integer(9502)).intValue());
  String name = "RemoteHelloServer";
  
   hello = (HelloInterface) registry.lookup(name);
   System.out.println(hello.say());
  } catch (Exception e) {
   System.out.println("HelloClient exception: " + e);
  }
 }
}

Compile and run the client code.
After this it is easy to expose your methods with RMI.

1 comment: