RemoteMethod Invocation (RMI) using Java


To illustrate the concept of remote control invocation by performing the display message "Hello World"


  • Create a remote interface extending the java.rmi remote interface each remote method interfaces throws a java.rmi remote exception.
  • Define a class that implements that interface of the remote object extending the class java rmi unicust remote object.
  • create a server program that register all remote server objects naming rebind() method is used to bind (ie) to assign name to the remote server object.
  • Compile all the four java file using javac.
  • Run the stub compile (rmic compiler) using command rmic filename.
  • Start rmi registry.
  • Run the server program.
  • Run the client program in seperate command prompt using the command applet viewer filename client java.  
Program


Hello.java

// Comment
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}


HelloImpl.java
// Comment
import java.rmi.*;
import java.rmi.server.*;
public class HelloImpl extends UnicastRemoteObject
implements Hello {
public HelloImpl() throws RemoteException {
}
public String sayHello() throws RemoteException{
return "Hello World!";
}
}


Helloserver.java

// Comment
import java.rmi.*;
import java.rmi.registry.*;
public class Helloserver
{
public static void main(String args[]) {
try {
HelloImpl obj=new HelloImpl();
Registry reg=LocateRegistry.createRegistry(5000);
reg.rebind("Helloserver",obj);
System.out.println("Server Starts........");
String ab[]=reg.list();
System.out.println("The Remote Object Available Are:");
for(int i=0;i<ab.length;i++)
System.out.println(ab[i]);
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
}


Helloclient.java

// Comment
import java.rmi.*;
public class Helloclient
{
public static void main(String args[])
{
String str="";
Hello obj=null;
try {
obj=(Hello)Naming.lookup("rmi://localhost:5000/Helloserver");
System.out.println(str);
}
catch(Exception e)
{
System.out.println("Exception:"+e);
}
}
}
Ouput Window 

raja P Web Developer

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...