How to execute RMI programs- Remote Method Invocation in Java

RMI stands for "Remote Method Invocation", the technology of Java.


  • RMI is nothing but communication between two JVMs loaded on two different systems.
  • The RMI Architecture is very simple involving a client program, a server program, a stub and skeleton.
In RMI, the client and server do not communicate directly; instead communicates through stub and skeleton (a special concept of RMI and this is how designers achieved distributed computing in Java). They are nothing but special programs generated by RMI compiler. They can be treated as proxies for the client and server. Stub program resides on client side and skeleton program resides on server. That is, the client sends a method call with appropriate parameters to stub. The stub in turn calls the skeleton on the server. The skeleton passes the stub request to the server to execute the remote method. The return value of the method is sent to the skeleton by the server. The skeleton, as you expect, sends back to the stub. Finally the return value reaches client through stub.

How to run a RMI program?


A typical RMI application includes four programs.
  1. Remote interface: Write an interface that should extend an interface called Remote from java.rmi package.
  2. Implementation program: Write a concrete class which extends UnicastRemoteObject and implements the above remote interface.
  3. Server Program: Write a server program to register the object with RMI registry.
  4. Client program: Write a client program to request for object and method invocation.
Refer the guidelines on how to write above file in java documentation. below i show you a small example on how to execute rmi programs.

Calculator program in RMI:

1-Remote interface:(calc.java):

import java.rmi.*;
import java.util.*;

public interface calc extends Remote
{

public abstract int add(int a, int b) throws RemoteException;
public abstract int sub(int a, int b) throws RemoteException;
public abstract int mul(int a, int b) throws RemoteException;
public abstract int div(int a, int b) throws RemoteException;

}

2-Implementation program (File name: calcimpl.java):
import java.rmi.*;
import java.rmi.server.*;  


public class calcimpl extends UnicastRemoteObject implements calc
{
  
  public calcimpl() throws RemoteException
  {
    super();
  }
  
  public int add(int a, int b) throws RemoteException
  {
 int sum=a+b;
 return sum;
 
  }
  public int mul(int c, int d) throws RemoteException
  {
 int mul=c*d;
 return mul;
 
  }
   public int sub(int c, int d) throws RemoteException
  {
 int sub=c-d;
 return sub;
 
  }
   public int div(int c, int d) throws RemoteException
  {
 int div=c/d;
 return div;
 
  }

}

3.server program(Filename: calcserver.java):

import java.rmi.*;
public class calcserver
{

public static void main(String args[]) throws Exception
 {
calc n1= new calcimpl();
Naming.rebind("abc", n1);  
System.out.println("Server is ready for remote invocations by client");
 }



}

4.client program(Filename:calcclient.java):



import java.rmi.*;
public class calcclient 
{
  public static void main(String args[]) throws Exception
  {
     calc i2 = (calc) Naming.lookup("abc");         
   
    int f = i2.add(5,3);  
    System.out.println("sum is" + f);
int g = i2.mul(7,8);
    System.out.println("mul is" + g);

int h = i2.sub(7,8);
    System.out.println("sub is" + h);
int i = i2.div(7,8);
    System.out.println("Div is" + i);

 }

}

How to execute the above  program:

follow below steps :

Step-1:

Compile all the 4 programs with javac command as usual as follows:

Javac calc*.java



This compiles all the 4 programs at a time and obtains 4 different .class files. Do not attempt to compile each program separately which sometimes says the remote interface is not available. To do so, set the classpath of JDK at the DOS prompt eventhough you set it in system environment variables.



Step-2:

Generate stub and skeleton by executing the below command:

rmic calcImpl


The above rmi compiler (rmic stands for rmi compiler), generates stub and skeletons and they are .class files as follows:
calcimpl_stub.class

Observe, the stub and skeleton are generated on implementation file. From JDK 1.2, skeleton is not created with the introduction dynamic stubs creation. Even without skeleton generation, still your program works.

Step-3:

start rmi registry as follows:

 start rmiregistry





The second window is rmi registry.
The above command gets a window, minimize it and it must be working at the background as long as client and server communicates (like Tomcat server running all through the Servlet communication). It is a service between client and server. RMI registry can treated as a middleware component between client and server. For this reason, RMI is known as middleware technology.

Step-4:

Now run server program by executing below commad

java InterestServer



Step-5:

Now open a new command prompt and execute the client program

java InterestClient

Comments

Popular posts from this blog

FC Barcelona vs Real Madrid -El Clasico Rivalry.

1NF,2NF, 3NF Normalization

Memory Management (Contiguous and Non-Contiguous ).