Wednesday, February 25, 2009

Hello World using Java RMI

Java RMI (Remote Method Invocation) is a variation of Remote Procedure Calls implemented out and out with Java Programming language and integrated seamlessly into Java API. Java RMI uses JNDI (Java Naming and Directory Interface) to host Java Objects on a Remote Server and allow them to be accessed via multiple clients thus providing a distributed usage of those hosted Java Objects. These hosted Java Objects actually reside in one JVM. But they are hosted in a registry so that they can be used from other JVMs too. once a Java Object of one JVM is bounded in a registry Processes from other JVMs can access it and call all the methods of that Java Object. Parameters can also be passed into those objects as well as returned values can also be captured.

Here we discuss about a simple hello world application using Java RMI.

 

Greetable.java

import java.rmi.*;

/**

 * @author Shazin Sadakath

 *

 **/

public interface Greetable extends Remote{

            public String sayHello() throws RemoteException;

}

Greeter.java

import java.rmi.registry.*;

import java.rmi.*;


/**

 * @author Shazin Sadakath

 *

 **/

public class Greeter implements Greetable{

             public String sayHello(){

                          String greeting = “Hello, World!”;

                         System.out.println(greeting);

                         return greeting;

             }

}

RMIServer.java

import java.rmi.registry.*;

import java.rmi.*;

import java.rmi.server.*;

/**

 * @author Shazin Sadakath

 *

 **/

public class RMIServer{

              public static void main(String[] args){

             try{

                          String ip = args[0];

                          int port = Integer.parseInt(args[1]);

                          Registry registry = LocateRegistry.getRegistry(ip,port);

                          Remote obj = UnicastRemoteObject.exportObject(new Greeter(),0);

                          registry.rebind(”Greeter”,obj);

                          System.out.println(”RMI Server Started”);

               }catch(ArrayIndexOutOfBoundsException aie){

                         usage();

               }catch(Exception re){

                         System.err.println(re.getMessage());

               }

}


private static void usage(){

                System.out.println(”Usage: java RMIServer ”);

}

}

RMIClient.java

import java.rmi.registry.*;

import java.rmi.*;

/**

 * @author Shazin Sadakath

 *

 **/

public class RMIClient{

              public static void main(String[] args){

              try{

                          String ip = args[0];

                          int port = Integer.parseInt(args[1]);

                          Registry registry = LocateRegistry.getRegistry(ip,port);

                          Greetable greeter = (Greetable) registry.lookup(”Greeter”);

                          System.out.println(greeter.sayHello());

              }catch(ArrayIndexOutOfBoundsException aie){

                         usage();

              }catch(Exception e){

                         System.err.println(e.getMessage());

              }

}


private static void usage(){

               System.out.println(”Usage: RMIClient ”);

}

}

Starting the RMI Registry in localhost

$ rmiregistry 9999

Starting the RMI Server in localhost

$ java RMIServer 127.0.0.1 9999

Starting the RMI Client in localhost

$ java RMIClient 127.0.0.1 9999

 

RMI provides fast and reliable distributed application capabilities. Thus has become a major part in network based applications.

No comments:

Post a Comment