You are on page 1of 17

RMI (Remote Method Invocation)

Distributed System requires information running in different


Address spaces, Virtually on different computers. And these
Distributed System require a medium of communication.

The basic communication mechanism in Java is implemented


Through sockets, which involves a great deal of socket
Programming.

However, sockets require the client and server


To engage in an application level protocol for information
Exchange, and to design such protocols could result in a
Cumbersome task and can be error prone.
An alternative to Sockets in order to manage the Distributed
Object Systems require

Remote Method Invocation or RMI

In such systems a local surrogate(stub) object that is


A substitution object, manages the invocation on a remote
Object.
Cb
Ca
Cb Cb
Stub Skeleton

Client Server
Remote object.

In the java distributed object model, a remote object is one


Whose methods can be invoked from another java virtual
Machine.

An object of this type is described by one or more remote


Interfaces, which are java interfaces that declare the methods
Of the remote object.

RMI model is simple easy to use and fits well in the


Communication.
RMI Applications

So RMI means invoking a method defined in a different


Address space and implementing the java language to give an
Extensive support of its features.

So a client and server can communicate, and this does not


Involve sockets directly.

Such applications are sometimes referred as distributed


Object application.
A typical server application

-Creates a number of remote objects.


-Binds the registry to associate a name with a remote object
-Makes reference to the accessible remote object.
-Waits for clients to invoke methods on the remote objects.

A typical client application

-looks up the remote object by its name in the server registry


And then invokes a method on it.
-gets a remote reference to one or more objects in the server.
-Invokes methods through the references thus received.
RMI applications should perform the following.

Locate remote objects.

To receive the references of the remote objects, an application


Can register its remote objects with RMI’s simple naming
Facility tool rmiregistry.

Communicate with remote objects.

It has the same syntax as a method invocation on a local object.


Load class bytecodes for objects that are passed as parameters
Or return values.

Since RMI allows a caller mechanism to pass pure Java objects


To remote objects, it provides the necessary mechanisms for
Loading an object’s code. And it also does the job of
Transmitting remote objects data over the network as part
Of its normal operation.
Stubs & Skeletons – Marshalling & Unmarshalling

RMI uses a standard mechanism for communicating with


Remote objects : stubs and skeletons.

STUBS :

A stub for a remote object acts as a clients local representative


Or proxy for the remote object. The caller invokes a method
On the local stub, which is responsible for carrying out the
Invocation of a method on the remote object.
When a stub is invoked, it does the following.

-Instantiates connection with the remote JVM that contains


The remote object.

-Marshals (I.e) writes and transmits, the parameters to the


Remote JVM.

-waits for the result from the method invocation.

-Unmarshals (I.e) reads, the return value or exception incurred.

-Returns the value to the caller program.


Skeletons : In the remote JVM, each remote object may have
A corresponding skeleton. The skeleton is responsible for
Dispatching the call to the actual remote object for
Implementation. When a skeleton receives an incoming method
Due to remote invocation it does the following.

-Unmarshals the parameters for the remote method.

-Invokes the method on the actual remote object implementaion

-Marshals the result which is nothing but the return value or


Exception to the caller.

Note : The rmic compiler, a special tool, generates stubs and


Skeletons.
The RMI Packages

- java.rmi
- java.rmi.server
- java.rmi.registry

The java.rmi Package

It comprises of the following important interfaces and classes.

- The Remote Interface


- The remoteException class
- The Naming class
The Remote Interface.

The java.rmi.Remote interface provides an identity to all


Remote interfaces so any object that is a remote object must
Directly or inderectly implement this interface.

The Naming class

This class provides method for storing and acquiring


References of remote objects in the remote object registry.

Methods - bind(), lookup(), rebind(), unbind(), list().


Naming.bind() – Binding a name for a remote object is nothing
But registering a name for a remote object that can be used
At a later time to look up that remote object.

Naming.bind(“rmi://localhost/remserver”,obj);

Naming.lookup() – Once a remote object is registered (that is


Bound with the registry) the callers on a remote or local host
Can look for the remote object by name through lookup()
Method. The lookup() method obtains the remote objects
Reference, and then invoke remote methods on the object.

Naming.lookup(“rmi://192.168.11.1100/remserver”);
The java.rmi.server package

This package contains interfaces and classes typically used to


Implement remote objects. Provides classes and interfaces
For supporting the Server side of RMI.

The UnicastRemoteObject class

This class defines a non-replicated remote object whose


References are authenticated only while the server process is
Alive.
import java.rmi.*;
public interface rinter extends Remote
{
public int add(int a, int b)throws RemoteException;
}
import java.rmi.*;
public class rclient
{
public static void main(String arg[])throws Exception
{
rinter obj=(rinter)Naming.lookup("rmi:///localhost/rserver");
System.out.println("the value is:"+obj.add(12,12));
}
}
import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*;
public class rserver extends UnicastRemoteObject implements rinter
{
public rserver() throws RemoteException
{
}
public int add(int a, int b)
{
int c=a+b;
return(c);
}
public static void main(String arg[])throws Exception
{
LocateRegistry.createRegistry(1099);
rserver obj=new rserver();
Naming.bind("rmi:///localhost/rserver",obj);
System.out.println("Server is ready for rmi application");
} }

You might also like