You are on page 1of 22

Introduction to RMI

SEED Infotech Pvt. Ltd. 1
Objectives of This Session

1. State the concept of Remote Method Invocation.

2. Describe the steps to be followed for writing a
simple RMI application.

3. Describe and demonstrate the sequence of
activities that happen in a typical RMI request-
response using stubs and skeletons.

SEED Infotech Pvt. Ltd. 2
Distributed Computing
Distributed computing refers to application design
paradigm in which the programs, the data they
process & the actual computations are spread
over a network.

SEED Infotech Pvt. Ltd. 3
Remote Method Invocation

Allows object to object communication between
different JVM’s.
This enables applications to call object methods
located remotely, sharing resources & processing
load across systems.
RMI is a distributed system that internally uses
sockets over TCP/IP.

SEED Infotech Pvt. Ltd. 4
Remote Method Invocation

The RMI architecture consists of three layers :
The Stubs / Skeleton
Remote Reference
Transport Layers

SEED Infotech Pvt. Ltd. 5
RMI Architecture





SEED Infotech Pvt. Ltd. 6
parameter marshalling

The process of encoding the parameters is called
parameter marshalling.
Its purpose: convert parameters into a format
suitable for transport from one virtual machine to

SEED Infotech Pvt. Ltd. 7

Client side proxy representing remote object.
Defines all interfaces that the remote object
implementation supports.
It looks like a local object on client side.
It also maintains a connection to server side
The stub packages the parameters used in
remote method as a block of bytes .

SEED Infotech Pvt. Ltd. 8

Is the server side construct that interfaces with the
server side RRL.
Skeleton receives method invocation requests from
the client side RRL.
Server side Skeleton then makes a call to the actual
RRL must unmarshall any arguments sent.
object implementation on the server side.

SEED Infotech Pvt. Ltd. 9
Remote reference layer

Deals with low level transport interface.
Responsible for providing a stream to the stub
and skeleton layers.

SEED Infotech Pvt. Ltd. 10
RMI Registry

A program that maintains the table of object name and
It can be handled by using classes and methods of java.rmi
Accessed by Server to insert the remote object entry.
Accessed by client to retrieve the remote object reference.

SEED Infotech Pvt. Ltd. 11
Steps to write RMI

Write a Remote interface.
Write Implementation of remote interface.
Write Server.
Compile (javac & rmic)
Run rmiregistry
Run server
Compile and run client

SEED Infotech Pvt. Ltd. 12
Remote Method Invocation



java.rmi.activation.Activatable java.rmi.server.UnicastRemoteObject

SEED Infotech Pvt. Ltd. 13
Remote Method Invocation(Remote Object)
Import java.rmi.*;

public interface Lookup extends Remote {
public String findInfo (String info) throws RemoteException;

SEED Infotech Pvt. Ltd. 14
Remote Method Invocation(Server)

Import java.util.*;
Import java.rmi.*;
Import java.rmi.server.*;

SEED Infotech Pvt. Ltd. 15
Remote Method Invocation

public class LookupServer extends UnicastRemoteObject
implements Lookup {
Private Vector save = new Vector();

Public LookupServer(String db) throws RemoteException{
try {
FileReader fr = new FileReader(db);
BufferedReader br = new BufferedReader(fr);
String s = null;
While ((s = br.readLine()) !=null)
SEED Infotech Pvt. Ltd. 16
Remote Method Invocation

public String findInfo(String info){
if(info == null) return null;
info = info.toLowerCase();
int n = save.size();
for (int I = 0; I < n; I++) {
String dbs = (String)save.elementAt(i);
If (dbs.toLowerCase().indexOf(info) !=-1)
return dbs;
return null;
SEED Infotech Pvt. Ltd. 17
Remote Method Invocation

public static void main(String args[]){
try {
RMISecurityManager security =
new RMISecurityManager();
String db = args[0];
LookupServer server =new LookupServer(db);
Naming.rebind(“LookupServer”, server);
System.err.println(“LookupServer ready…”);

SEED Infotech Pvt. Ltd. 18
Remote Method Invocation

Setting up a security manager
stub class is :
public class LookupServer_Stub extends
Implements {…}
skeleton class is :
public class lookupServer_Skel implements
java.rmi.server.Skeleton {…..}

SEED Infotech Pvt. Ltd. 19
Remote Method Invocation(Client)

try {
RMISecurityManager security =
new RMISecurityManager();
System.setSecurityManager (security);
String host = “localhost”;
String server = “LookupServer”;
String name = “rmi://” + host + “ “ + server;
Lookup look_obj = (Lookup)Naming.lookup(name);
String results = look_obj.findInfo(args[0]);

SEED Infotech Pvt. Ltd. 20
Compiling and running the Application

Compile the code
The three files, and, compile as usual in Java :

SEED Infotech Pvt. Ltd. 21
Compiling and running the Application

Run the RMI Compiler
After you compile the files, run the RMI compiler (rmic):
rmic LookupServer
To produce the Lookup_Server_Skel.class and
lookupServer_Stub.class files.

SEED Infotech Pvt. Ltd. 22