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

CLIENT SERVER

STUB SKELETON

REMOTE REMOTE
REFERENCE REFERENCE
LAYER LAYER

TRANSPORT LAYER

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
another.

SEED Infotech Pvt. Ltd. 7
stub

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
object.
The stub packages the parameters used in
remote method as a block of bytes .

SEED Infotech Pvt. Ltd. 8
skeleton

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
references.
It can be handled by using classes and methods of java.rmi
package.
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.lang.Object

java.rmi.server.RemoteServer

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

SEED Infotech Pvt. Ltd. 13
Remote Method Invocation(Remote Object)

Lookup.java
Import java.rmi.*;

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

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

LookupServer.java

Import java.io.*;
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)
Save.addElements(s);
Fr.close();
}
}
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();
System.setSecurityManager(security);
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
java.rmi.server.RemoteStub
Implements Lookup.java.rmi.Remote {…}
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 Lookup.java, LookupServer.java and
LookupClient.java, compile as usual in Java :
javac Lookup.java
javac LookupServer.java
javac LookupClient.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