KEMBAR78
RMI Notes | PDF | Client–Server Model | Computer Networking
0% found this document useful (0 votes)
18 views10 pages

RMI Notes

Remote Method Invocation (RMI) is a Java API that enables remote communication between applications running in different Java Virtual Machines (JVMs) through the use of stub and skeleton objects. The RMI architecture consists of four layers: Application Layer, Proxy Layer, Remote Reference Layer, and Transport Layer, which facilitate the connection, method invocation, and data transmission between client and server applications. A distributed application can be created using RMI by implementing remote interfaces and managing remote object references via a naming server or registry.

Uploaded by

sreeee437
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

RMI Notes

Remote Method Invocation (RMI) is a Java API that enables remote communication between applications running in different Java Virtual Machines (JVMs) through the use of stub and skeleton objects. The RMI architecture consists of four layers: Application Layer, Proxy Layer, Remote Reference Layer, and Transport Layer, which facilitate the connection, method invocation, and data transmission between client and server applications. A distributed application can be created using RMI by implementing remote interfaces and managing remote object references via a naming server or registry.

Uploaded by

sreeee437
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

RMI (Remote Method Invocation)

The RMI (Remote Method Invocation) is an API that provides a mechanism to


create distributed application in java. The RMI allows an object to invoke methods
on an object running in another JVM.
The RMI provides remote communication between the applications using two
objects stub and skeleton.
Understanding stub and skeleton
RMI uses stub and skeleton object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM.
Let's understand the stub and skeleton objects:
stub
The stub is an object, acts as a gateway for the client side. All the outgoing
requests are routed through it. It resides at the client side and represents the
remote object. When the caller invokes method on the stub object, it does the
following tasks:
1. It initiates a connection with remote Virtual Machine (JVM),
2. It writes and transmits (marshals) the parameters to the remote Virtual
Machine (JVM),
3. It waits for the result
4. It reads (unmarshals) the return value or exception, and
5. It finally, returns the value to the caller.
skeleton
The skeleton is an object, acts as a gateway for the server side object. All the
incoming requests are routed through it. When the skeleton receives the
incoming request, it does the following tasks:
1. It reads the parameter for the remote method
2. It invokes the method on the actual remote object, and
3. It writes and transmits (marshals) the result to the caller.
In the Java 2 SDK, an stub protocol was introduced that eliminates the need for
skeletons.
Understanding requirements for the distributed applications
If any application performs these tasks, it can be distributed application.
.
1. The application need to locate the remote method
2. It need to provide the communication with the remote objects, and
3. The application need to load the class definitions for the objects.
The RMI application have all these features, so it is called the distributed application.

Remote Method Invocation (RMI) is a mechanism that allows an object running in one java virtual
machine (JVM) to invoke methods on an object running in another java virtual machine (JVM). It
provides remote communication between java programs. Objects with methods that can be invoked
across JVMs are called remote objects. The remote object is called the server object.
Architecture of RMI
In an RMI application, we write two programs, a server program (resides on the server) and a client
program (resides on the client).
Inside the server program, a remote object is created and reference of that object is made available for
the client (using the registry).
The client program requests the remote objects on the server and tries to invoke its methods.
The RMI architecture consists of four layers:
1. Application Layer: This layer is the actual systems i.e. client and server which are involved in
communication. The java program on the client side communicates with the java program on the server-
side.
2. Proxy Layer: This layer contains the client stub and server skeleton objects.
- Stub is an object that resides on the client machine and it acts as a proxy for the remote object. It is like
a gateway for the client program. When the client calls on the stub object, the stub forwards this
request to a remote object (Skeleton) via RMI infrastructure which is then executed on the server.
- The server object which resides in a server machine is known as Skeleton. Stub communicates with
server application with the help of an intermediate Skeleton object. The responsibility of the skeleton
object is to send parameters to method implementation and send the return values back to the client.

3. Remote Reference Layer: This layer is responsible to maintain the session during the method call. i.e.
It manages the references made by the client to the remote server object. This layer is also responsible
for handling duplicated objects. The invocation semantics of the RMI connection is defined and
supported by this layer.
4. Transport Layer: The transport layer is responsible for setting up communication between the two
machines. This layer uses standard TCP/IP protocol for connection. The actual transportation of data is
performed through this layer.

Architectural Overview
The RMI system consists of three layers:
 The stub/skeleton layer -- client-side stubs (proxies) and server-side skeletons
 The remote reference layer -- remote reference behavior (such as invocation to a single object
or to a replicated object)
 The transport layer -- connection set up and management and remote object tracking
The application layer sits on top of the RMI system. The relationship between the layers is shown in the
following figure.

A remote method invocation from a client to a remote server object travels down
through the layers of the RMI system to the client-side transport, then up through the
server-side transport to the server.

A client invoking a method on a remote server object actually makes use of a stub or
proxy for the remote object as a conduit to the remote object. A client-held reference
to a remote object is a reference to a local stub. This stub is an implementation of the
remote interfaces of the remote object and forwards invocation requests to that server
object via the remote reference layer. Stubs are generated using the rmic compiler.

The remote reference layer is responsible for carrying out the semantics of the
invocation. For example, the remote reference layer is responsible for determining
whether the server is a single object or is a replicated object requiring
communications with multiple locations. Each remote object implementation chooses
its own remote reference semantics--whether the server is a single object or is a
replicated object requiring communications with its replicas.

Also handled by the remote reference layer are the reference semantics for the server.
The remote reference layer, for example, abstracts the different ways of referring to
objects that are implemented in (a) servers that are always running on some machine,
and (b) servers that are run only when some method invocation is made on them
(activation). At the layers above the remote reference layer, these differences are not
seen.
The transport layer is responsible for connection setup, connection management, and
keeping track of and dispatching to remote objects (the targets of remote calls)
residing in the transport's address space.

In order to dispatch to a remote object, the transport forwards the remote call up to the
remote reference layer. The remote reference layer handles any server-side behavior
that needs to occur before handing off the request to the server-side skeleton. The
skeleton for a remote object makes an up call to the remote object implementation
which carries out the actual method call.

The return value of a call is sent back through the skeleton, remote reference layer,
and transport on the server side, and then up through the transport, remote reference
layer, and stub on the client side.

RMI Architecture
The RMI architecture consists of four layers. These layers perform the specific
functions like establishing a connection, transmitting the objects.

The layers are as follows:

1. Application Layer
 It is the actual implementation of the client server application. The high
level calls are made here in order to access and export the remote objects.
The remote method can be accessed by the client through an interface that
inherits from java.rmi.Remote interface. Once this method described in the
remote interface is implemented the object is exported. The remote object
is implicitly exported only if the object inherits from
java.rmi.server.UnicastRemoteObject class. The application will then
register itself with a naming server or registry. It is used by the client to
obtain a reference of the remote objects.
2. Proxy Layer
 It is also known as the Stub and Skeleton Layer. They are simply class files
that represent the client and server side of a remote object. Stub is placed
on the client side and Skeleton is placed on the server side.
Responsibility of Stub class
 It works with the JVM and the RMI system on the client machine to serialize
the arguments to the remote method call and it sends the information to
the server machine.
 It receives any result from the remote method and returns it to the client.
Responsibility of the Skeleton class
 It receives the remote method call and any of the associated arguments. It
works with the JVM and RMI system on the server machine to deserialize
any of the arguments for this remote method call.
 Any return value is received from the method call and works with the JVM
and the RMI system on the server machine to serialize the return value and
sends the information back to the client application.
3. Remote Reference Layer

It is effectively between the Stub and Skeleton classes and the transport layer
which handles the actual communication protocol. The transmission of the
parameter or object through the network it should be in the form of a stream.

This layer is used for the following reasons:


 It handles the replicated objects. This feature is incorporated in the RMI
system so the replicated objects allow simple dispatch to many programs
that are exporting substantially the same remote objects.
 It establishes the persistence and strategies for the recovery of the lost
connections.
4. Transport Layer

The responsibilities of this layer are:


 It manages the connection.
 It sets up the connection to the remote machine.
 The connections are monitored to make sure that the remote machines are
live.
 A stream is created which is accessed by the remote reference layer to send
and receive data.
 It is also responsible for handling the actual machine to machine
communication.

public Server() {}
public static void main(String args[]) {
try {
ImplExample obj = new ImplExample();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server extends ImplExample {


public Server() {}
public static void main(String args[]) {
try {
ImplExample obj = new ImplExample();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}

import java.rmi.Remote;
import java.rmi.RemoteException;

interface Hello extends Remote {


void printMsg() throws RemoteException;
}
public class ImplExample implements Hello {
public void printMsg() {
System.out.println("This is an example RMI program");
}
}

1. create a folder rmi


2. within that rmi store below 3 files
3.open one cmd prompt
rmi>javac *.java
rmi>start rmiregistry
rmi>java Server
Server Ready
4.open another cmd prompt
rmi>java Client
see the first cmd prompt
that will display the message
This is an example RMI program

You might also like