Understanding Remote Method Invocation (RMI)

RMI stands for Remote Method Invocation. A program running on a computer some where on the Internet can invoke a method that is resident in a program running somewhere else on the internet. To make this happen several things need to be done and all of them are built into the java.rmi package.

RMI allows you to build distributed client – server applications. It must be noted that the client and server objects are java objects that reside on machines running Java Virtual Machine (JVM). To develop applications using RMI two matched programs needs to be developed.

A server application will be written which will make available some of its methods for Remote Invocation. A client application will be written that will be able to connect to the server application and use its methods. This is not a security breach since the applet or application can only call methods in the server that have been registered for Runnable Invocation.

Steps to develop RMI

Creation of a package

Create a directory that has the name of the package you would like to use.

Finally, you have to define an interface so that the server program can implement it. In this example the interface is a simple one with a single method. Type in the following script and save it as Ourinterface.java.

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">package rmidemo;
public interface Ourinterface extends java.rmi.Remote {
public String toAppend(String str) throws java.rmi.RemoteException; } 

 

The above interface defines the single method toAppend(). It takes a String object as an argument and supplies a String as its return value.

READ  Understanding Visual Studio 2005

Implementation of the server program

You have to implement the above Interface and its functions. This is the server side program of the rmi and should reside on the server:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">package rmidemo; import java.rmi.*; 
import java.rmi.server.UnicastRemoteObject; 
public class OurServerString extends UnicastRemoteObject implements Ourinterface {
public OurServerString() throws RemoteException { 
super(); 
}
public String toAppend(String str) throws RemoteException {
return(str + "ppp"); }
public static void main(String args[]) {
System.setSecurityManager(new RMISecurityManager());
try {
OurServerString ss = new OurServerString();
Naming.rebind("Ourinterface",ss);
System.out.println("Interface successfully bound in registry");
}catch(Exception e) {
System.out.println("Stringtag" +e); } } } 

 

Following points should have to be noted regarding the above piece of code

(1) Our class extends UnicastRemoteObject class of the java.rmi package, so that it will be capable of receiving remote method calls
(2) It also implements the interface Ourinterface
(3) Body of the method is defined
(4) The program is to be executed on the server so it must have a main() method
(5) The RMISecurity Manager must be defined without that no rim is possible
(6) Object of the class is created inside the main()
(7) This object has to register itself with the Naming registry. When a remote request arrives at this computer there is a search in the registry for the name. If a match is found, the relevant method will be called

Compilation

Compile the interface and the server program by using the command javac. After compilation, generate the Stub and Skeleton by using rmic. The caller of the method will need a stub. The method being called will need a skeleton.

READ  Learn HTML in Malayalam - Part 1

These Stub and Skeleton wraps and unwraps the data’s while transmitting from client to server and viceversa. This process is termed as Marshalling and UnMarshalling. These are generated by using the command rmic. As a next step, you have to start the registry by using the command: 

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">Start rmiregistry 
&nbsp;

Starting the server and Finishing up with client Applet or Application

Once the server program is ready, you can implement the same in an Applet or Application. Here in this example, I will use an Applet to demonstrate the rmi program.

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">package rmidemo; 
import java.rim.*;
import java.awt.*; 
import java.applet.*;
public class StringApplet extends Applet {
String s;
Public void init() {
try {
Ourinterface tf = (Ourinterface)Naming.lookup( "//" +
GetCodeBase().getHost() + "/Ourinterface");
s = tf.toAppend("The String value");
} catch(Exception e) { System.out.println(e);
e.printStackTrace();
}
} //end of the init() method. 
public void paint(Graphics g) {
g.drawString(s.25.30);
}

One Response to "Understanding Remote Method Invocation (RMI)"

  1. swapnilRT   September 3, 2010 at 3:19 am

    Nice explinations….
    please include more info in page

    Reply

Leave a Reply

Your email address will not be published.