Sunday, February 6, 2011

RMI and Java SImple Example

Hello RMI,

HelloInterface
import java.rmi.*;
public interface HelloInterfaceextends Remote {
    public Stringsay() throws RemoteException;
}

Hello
import java.rmi.*;
import java.rmi.server.*;

public class Hello extendsUnicastRemoteObject implements HelloInterface {
  private String message;
  public Hello (String msg)throws RemoteException {
    message = msg;
  }
  public String say() throwsRemoteException {
    return message;
  }
}

Then >mic Hello

HelloServer
import java.rmi.Naming;

public class HelloServer
{
  public static void main(String[] argv)
  {
    try {
     Naming.rebind ("Hello", new Hello ("Hello,From Roseindia.net pvtltd!"));
     System.out.println ("Server is connected and ready for operation.");
    }
  catch (Exception e) {
     System.out.println ("Server not connected: " + e);
    }
  }
}

HelloClient
import java.rmi.Naming;

public class HelloClient
{
    public staticvoid main (String[] argv) {
    try {
      HelloInterfacehello =(HelloInterface) Naming.lookup ("//192.168.10.201/Hello");
     System.out.println (hello.say());
    }
  catch (Exception e){
   System.out.println ("HelloClient exception: " + e);}
  }
}


Place stub in client
Then >rmiregistry
        >javaHelloServer
        >javaHelloClient

No comments:

Post a Comment