Saturday, February 19, 2011

RMI for FUN … 

This tutorial is not for an experienced individual who needs to know the pros and cons in RMI. This I write for anyone who likes to get a quick brief look up in to RMI. Let me explain in simple English what it does as this I write for a beginner who is just as I am ….

What is actually RMI (Remote Method Invocation)
We use RMI to invoke methods of an object that resides in another memory heap (another machine) through a network …. Just think how cool that would be.
And Oh yes we are just going to write a simple code for a simple scenario and see for ourselves …

What I want to do (scenario) :
Think that we need to monitor the current state (wind speed, wind direction and many more other data) of each wind turbine in a wind mill farm…. In every turbine, we have an application that maintains its own state and we from another location needs to monitor the state of each turbine REMOTELY...


Ok here's the code that how I do this... (will discuss after showing the artifacts)

Class diagram that depicts the server side



At the Wind turbine side (server side) ...

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

public interface RemoteWindMill extends Remote{

    public String getStatus() throws RemoteException;

}

-------------------------------------------------------------------------------


import java.rmi.registry.Registry;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;

public class WindMillImpl implements RemoteWindMill {

    public WindMillImpl() throws RemoteException {
    }

    public String getStatus() {

        return " Wind Turbine no 001 : "
                + " \n location = Puttlam Srilanka"
                + "\n Time = " + new Date()
                + "\n Coordinates = 06'08''46'''N 81'06''47'''E"
                + "\n Model = M1500"
                + "\n Hub Height = 46m (151 ft)"
                + "\n Rotor Diameter = 43m (141 ft)"
                + "\n Wind Speed = 40kmph "
                + "\n Wind Dirrection = west to east ";


    }

    public static void main(String[] args) {

        try {
            WindMillImpl mill = new WindMillImpl();
            RemoteWindMill stub = (RemoteWindMill) UnicastRemoteObject.exportObject(mill,0);
            Registry registry = LocateRegistry.getRegistry();
            registry.rebind("RemoteWindMill", stub);

        } catch (Exception e) {
        }

    }
}

-------------------------------------------------------------------------------

At the monitoring side (client side) ...


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

public class WindMillMonitor {

    public static void main(String[] args) {
        new WindMillMonitor().monitor();

    }

    public void monitor() {

        try {
            System.out.println("Trying to call the remote method");
            Registry registry = LocateRegistry.getRegistry();
            RemoteWindMill mill = (RemoteWindMill) registry.lookup("RemoteWindMill");
            String s = mill.getStatus();
            System.out.println(s);

        } catch (Exception e) {
        }

    }
}

------------------------------------------------------------------------------- 

You can download the source code from the below link :
http://www.sendspace.com/file/a9z3sq

-------------------------------------------------------------------------------


In order to run the system..


step1: start rmiregistry in serverside

 step2: run WindMillImpl in serverside












step3: run WindMillMonitor in client side 



What we are really doing here is we are creating something called a stub which adheres to the interface of the RemoteWindMill and assign it in a registry. This stub is the one that is responsible to commuinicate through wires remotely and we don’t need to care about its content because UnicastRemoteObject.exportObject() will create and assign the stub in a registry for us.  This registry is almost like the white pages of a telephone directory where we find the address of a residence that having the phone number we search for.    Ok here’s a small diagram that depicts what I was talking…



In the diagram,

  1. WindMill monitor will check for the stub in the RMI registry which resides in another VM. LocateRegistry.getRegistry(String host, int port) in the WindMillMonitor class is responsible for this. Here we don’t specify the IP address of the WindMillImpl hosting machine or the port which WindMillIImpl will be listening as the arguments of the method because this application was written to run on the same machine just for you to get an idea how this RMI really works.
  2. Then by invoking registry.lookup("RemoteWindMill") it will search for the RemoteWindMill stub that has already been assigned to the RMI registry in the other VM. And a copy of  that stub is sent to the client side VM. The client sees a stub in it’s VM as almost like a WindMillImpl object because stub adheres to the RemoteWindMill interface.
  3. So that the client will directly be talking to the stub thinking it’s the RemoteWindMillImpl object. But under the covers it’s the stub secretly calling the real RemoteWindMillImpl object residing in another VM via a cable.

Resources: 

      No comments:

      Post a Comment