Monday, March 23, 2009

Simple RMI example

Here is a very simple RMI example. The writeup about RMI can be found in my previous post here.

Remote Interface

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

public interface Product extends Remote{

public String getDescription() throws RemoteException;

}



Implementation of the Remote Interface:

import java.rmi.server.*;
import java.rmi.*;
import working.testing.myrmi.remoteinterface.*;

public class ProductImpl implements Product {

private String type;

public ProductImpl(String type)throws RemoteException{
this.type = type;
}

public String getDescription() throws RemoteException{
return ("The Product is " + type);
}
}



The Server

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import working.testing.myrmi.remoteinterface.*;

public class MyRMIServer {

public static void main(String[] args) {
String toaster = "toaster";
try {
/** If an RMI program does not install a
* security manager, RMI will not download
* classes (other than from the local class path)
* for objects received as arguments or
* return values of remote method invocations.
*/
if (System.getSecurityManager() == null) {
System.setSecurityManager
(new RMISecurityManager());
}
Product product = new ProductImpl(toaster);
/**
* Since the server dosent extend
* UnicastRemoteObject,
* First we need to export the
* object. Once the remote object has been
* successfully exported it is ready for
* invocations. However for clients to
* invoke the remote object, it needs to
* be bound to the registry with a name.
* If extending UnicastRemoteObject,
* We should do only the following
* Naming.rebind(toaster,product);
* because it automatically exports
* the remote object for us
*/
Product stub = (Product)UnicastRemoteObject.
exportObject(product,0);
Registry registry = LocateRegistry.createRegistry(5555);
registry.rebind(toaster,stub);
System.out.println("Waiting for clients");
} catch (Exception e) {
System.out.println("The Exception is " + e);
e.printStackTrace();
}

}

}



The client

import working.testing.myrmi.remoteinterface.*;
import java.rmi.*;
import java.rmi.registry.*;

public class MyRMIClient {

public static void main(String[] args) {
try {
/** If an RMI program does not install a
* security manager, RMI will not download
* classes (other than from the local class path)
* for objects received as arguments or
* return values of remote method invocations.
*/
if (System.getSecurityManager() == null) {
System.setSecurityManager
(new RMISecurityManager());
}

/**
* First we need to get a handle
* to the registry on the server machine and
* then we need to lookup for the remote
* object
* We can also do
* Product product =
* (Product)Naming.lookup
* ("rmi://host:1099/toaster");
* where host is the hostname where
* the server is running.
* host will be localhost if the server is
* running locally. Sometimes putting in
* localhost can cause
* some unnecessary exceptions, so just do
* lookup("rmi://:1099/toaster") if
* on localhost
* Also while getting registry if on localhost
* better to do just getRegistry() instead of
* getRegistry("localhost") to avoid similar
* exceptions as above.
*/
Registry registry = LocateRegistry.
getRegistry(5555);
Product product = (Product)registry.
lookup("toaster");
/*Product product = (Product)Naming.
lookup("rmi://:1099/toaster");*/
System.out.println(product.getDescription());
} catch (Exception e) {
System.out.println("The exception is " + e);
e.printStackTrace();
}
}
}

No comments:

Post a Comment