Sunday, May 30, 2010

Workshop 13 : Java Remote Method Invocation


Using RMI in Java uses the Java Virtual Machine (JVM) to share objects through facilities for activating and managing object instances, and is also used with Enterprise Java Beans technology.  RMI allows classes to be downloaded from HTTP servers and can be used as an alternative to CORBA or DCOM.


 
HelloWorld in RMI
Why you would consider doing the Hello World program in RMI or CORBA is almost beyond belief, until you recognise that it is a good way for us to see a sample application.

Table 1:  The HelloWorld application as a Remote Method Invocation.

HelloWorldImpl <>
Implements the methods of the interface
Extends UnicastRemoteObject
HelloWorld <>
The interface is what the Client will see, methods.  Declared here are the ones the client sees
Extends java.rmi.Remote
java.rmi.Remote
Extending this tells the JVM that the object will be available to the RMI mechanism

UnicastRemoteObject
Provides methods that allow an object to be available to incoming calls (export)

The simple steps to exporting a remote object – the server

1.             Compile the interface with the RMIC tool.
2.             Create and bind an object to the rmiregistry


try{
  HelloWorld yoWorld = new HelloWorldImpl();
  Naming.rebind(exportName, yoWorld);
}catch(Exception e){
 e.printStackTrace();
}

3.            Create a client that looks up the object and then invokes its methods.


try {
  String exportName = "//localhost/yoHello";
  HelloWorld world = (HelloWorld) Naming.lookup(exportName);
  System.out.println(world.sayHello("bob smith"));
}catch(Exception e){
  e.printStackTrace();
}

Starting and running HelloWorld on the local machine

1.             Start the rmiregistry
C:\rmiregistry

2.             Start the server
C:\start java -Djava.security.policy=java.policy HelloWorldImpl

3.             Start the client
C:\java -Djava.security.policy=java.policy FindWorld

FindHello.java

import java.rmi.*;
import java.rmi.server.*;
import java.io.*;


public class FindHello{

public static void main(String[] args) {
System.out.println(System.getSecurityManager());
try {
String exportName = "//localhost/yoHello";
HelloWorld world = (HelloWorld)
Naming.lookup(exportName);
System.out.println(world.sayHello("bob smith"));
}catch(Exception e){
e.printStackTrace();
}
}// main
}// class

HelloWorld.java

import java.rmi.*;
public interface HelloWorld extends Remote{
public String sayHello(String yourName) throws RemoteException;

}

HelloWorldImpl.java

import java.rmi.*;
import java.rmi.server.*;
import java.util.Date;

public class HelloWorldImpl extends UnicastRemoteObject
implements HelloWorld{

public HelloWorldImpl() throws RemoteException{
super();
}

public String sayHello(String yourName){
return "Hello "+yourName+" the Time on this machine is "+
new Date().toString();
}

public static void main(String[] args) throws Exception{

System.out.println(System.getSecurityManager());


String exportName = "//localhost/yoHello";
try{
HelloWorld yoWorld = new HelloWorldImpl();
Naming.rebind(exportName, yoWorld);
}catch(Exception e){
e.printStackTrace();
}
}// main


}// class


Java.policy

grant {
    permission java.net.SocketPermission "*:1091",
        "connect,accept";
    permission java.net.SocketPermission "*:80", "connect";
};


0 comments:

Post a Comment