Introduction to Remote Method Invocation (RMI) - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Introduction to Remote Method Invocation (RMI)

Share This
The RMI API provides a mechanism to create distributed applications in java. Using this API, the methods of a remote object can be called, where the remote object is running in a different JVM.

This communication is established between two applications, which are running in different machines, creating two objects known as stub and skeleton.

A stub object, which resides in the client system, acts as a gateway for the client-side. When a method of stub object is called, it initiates a connection and sends the parameters to the remote JVM. Then it waits until it receives the result and returns the value to the caller after receiving the result.

A skeleton acts as a gateway for the server-side object. After receiving an incoming request, it reads the parameters and invokes the actual remote method. Then it writes and transmits the result to the caller.

Nowadays, we do not use skeletons, as an introduction of stub protocol eliminated the need for skeletons in Java 2 SDK.

Example


In this context, we will discuss how to write a program using RMI API. Now, we will develop a program for a basic calculator and a client will call remote methods which are defined in the server program.

First, create a remote interface inheriting the Remote interface.

CalculatorIntf.java
import java.rmi.*; public interface CalculatorIntf extends Remote{ public int add(int num1,int num2)throws RemoteException; public int sub(int num1,int num2)throws RemoteException; public int mul(int num1,int num2)throws RemoteException; public int div(int num1,int num2)throws RemoteException; }

Now, define a class implementing the remote interface as follow.

Calculator.java
import java.rmi.*; import java.rmi.server.*; public class Calculator extends UnicastRemoteObject implements CalculatorIntf{ public int add(int num1, int num2){ return num1 + num2; } public int sub(int num1, int num2){ return num1 - num2; } public int mul(int num1, int num2){ return num1 * num2; } public int div(int num1, int num2){ return num1 / num2; } }

Now you have to create a stub object using the rmic tool.
#rmic Calculator
Start the registry service by the rmiregistry tool specifying a port number. Make sure that the port is free.

#rmiregistry 5000
At this moment this service needs to be hosted as a server process and this object needs to be bind by some name as shown below.
MyServer.java
import java.rmi.*; import java.rmi.registry.*; public class MyServer{ public static void main(String args[]){ try{ CalculatorIntf stub = new Calculator(); Naming.rebind("rmi://localhost:5000/cal", stub); } catch(Exception e){ System.out.println(e); } } }

Now write the following code for client application development.

MyClient.java
import java.rmi.*; public class MyClient{ public static void main(String args[]){ try{ CalculatorIntf stub = (CalculatorIntf) Naming.lookup("rmi://localhost:5000/cal"); System.out.println(stub.add(34,4)); } catch(Exception e){} } }

For running this rmi example,
1) compile all the java files
javac *.java
2) create stub and skeleton object by rmic tool
rmic Calculator
                
3) start rmi registry in one command prompt
rmiregistry 5000
4) start the server in another command prompt
  
java MyServer
                
5) start the client application in another command prompt
java MyClient

Happy Exploring!

1 comment: