0% found this document useful (0 votes)
10 views

Remote Method Invocation Sir Wale

Remote Method Invocation (RMI) is a Java API that enables the creation of distributed applications by allowing objects to invoke methods on remote objects in different JVMs. It utilizes stub and skeleton objects for communication, where the stub acts as a client-side gateway and the skeleton serves as a server-side gateway. RMI applications require the ability to locate remote methods, communicate with remote objects, and load class definitions, and they follow a structured process involving interface creation, implementation, and registry setup.

Uploaded by

darshanjain1905
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Remote Method Invocation Sir Wale

Remote Method Invocation (RMI) is a Java API that enables the creation of distributed applications by allowing objects to invoke methods on remote objects in different JVMs. It utilizes stub and skeleton objects for communication, where the stub acts as a client-side gateway and the skeleton serves as a server-side gateway. RMI applications require the ability to locate remote methods, communicate with remote objects, and load class definitions, and they follow a structured process involving interface creation, implementation, and registry setup.

Uploaded by

darshanjain1905
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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.


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.

Java RMI Example

The is given the 6 steps to write the RMI program.

1. Create the remote interface

2. Provide the implementation of the remote interface

3. Compile the implementation class and create the stub and skeleton objects
using the rmic tool

4. Start the registry service by rmiregistry tool

5. Create and start the remote application

6. Create and start the client application


RMI Example

In this example, we have followed all the 6 steps to create and run the rmi application.
The client application need only two files, remote interface and client application. In the
rmi application, both client and server interacts with the remote interface. The client
application invokes methods on the proxy object, RMI sends the request to the remote
JVM. The return value is sent back to the proxy object and then to the client application.

1) create the remote interface

For creating the remote interface, extend the Remote interface and declare the
RemoteException with all the methods of the remote interface. Here, we are creating a
remote interface that extends the Remote interface. There is only one method named
add() and it declares RemoteException.

import java.rmi.*;

public interface Adder extends Remote{

public int add(int x,int y)throws RemoteException;

2) Provide the implementation of the remote interface

Now provide the implementation of the remote interface. For providing the
implementation of the Remote interface, we need to
• Either extend the UnicastRemoteObject class,

• or use the exportObject() method of the UnicastRemoteObject class

In case, you extend the UnicastRemoteObject class, you must define a constructor that
declares RemoteException.

import java.rmi.*;

import java.rmi.server.*;

public class AdderRemote extends UnicastRemoteObject implements Adder{

AdderRemote()throws RemoteException{

super();

public int add(int x,int y){return x+y;}

3) create the stub and skeleton objects using the rmic tool.

Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool
invokes the RMI compiler and creates stub and skeleton objects.

rmic AdderRemote

4) Start the registry service by the rmiregistry tool

Now start the registry service by using the rmiregistry tool. If you don't specify the port
number, it uses a default port number. In this example, we are using the port number
5000.

rmiregistry 5000

5) Create and run the server application

Now rmi services need to be hosted in a server process. The Naming class provides
methods to get and store the remote object. The Naming class provides 5 methods.
In this example, we are binding the remote object by the name sahil

import java.rmi.*;

import java.rmi.registry.*;

public class MyServer{

public static void main(String args[]){

try{

Adder stub=new AdderRemote();

Naming.rebind("rmi://localhost:5000/sahil",stub);

}catch(Exception e){System.out.println(e);}

6) Create and run the client application

At the client we are getting the stub object by the lookup() method of the Naming class
and invoking the method on this object. In this example, we are running the server and
client applications, in the same machine so we are using localhost. If you want to
access the remote object from another machine, change the localhost to the host name
(or IP address) where the remote object is located.

import java.rmi.*;

public class MyClient{

public static void main(String args[]){

try{

Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");

System.out.println(stub.add(34,4));

}catch(Exception e){}

}
Architecture of an RMI Application

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.

Let us now discuss the components of this architecture.

• Transport Layer − This layer connects the client and the server. It manages the
existing connection and also sets up new connections.

• Stub − A stub is a representation (proxy) of the remote object at client. It resides


in the client system; it acts as a gateway for the client program.

• Skeleton − This is the object which resides on the server


side. stub communicates with this skeleton to pass request to the remote
object.

• RRL(Remote Reference Layer) − It is the layer which manages the references


made by the client to the remote object.
Marshalling and Unmarshalling

Whenever a client invokes a method that accepts parameters on a remote object, the
parameters are bundled into a message before being sent over the network. These
parameters may be of primitive type or objects. In case of primitive type, the parameters
are put together and a header is attached to it. In case the parameters are objects, then
they are serialized. This process is known as marshalling.

At the server side, the packed parameters are unbundled and then the required method
is invoked. This process is known as unmarshalling.

RMI Registry

RMI registry is a namespace on which all server objects are placed. Each time the
server creates an object, it registers this object with the RMIregistry
(using bind() or reBind() methods). These are registered using a unique name known
as bind name.

To invoke a remote object, the client needs a reference of that object. At that time, the
client fetches the object from the registry using its bind name (using lookup() method).
Goals of RMI

Following are the goals of RMI −

• To minimize the complexity of the application.

• To preserve type safety.

• Distributed garbage collection.

• Minimize the difference between working with local and remote objects.

You might also like