You are on page 1of 23

Chapter -5

Remote Method Invocation (RMI)


Introduction to RMI
RMI stands for “Remote Method Invocation” means communicating the
object across the network.

RMI is a one type of structure or system that allows an object running in one
Java virtual machine (Client) to invoke methods on an object running in
another Java virtual machine (Server).

This object is called a Remote Object and such a system is also called RMI
Distributed Application.

RMI provides for remote communication between programs written in the


JAVA.

RMI allows a Java program on one machine to invoke a method on a remote


object.
Introduction to RMI
RMI
 Register method as remotely accessible
• Client can look up method and receive a reference
• Use reference to call method
• Syntax same as a normal method call
 Marshalling of data
• Can transfer objects as well
• Class ObjectOutputStream converts Serializable object into stream
of bytes
– Transmit across network
• Class ObjectInputStream reconstructs object
Marshalling Parameters
Creating a Distributed System with RMI
 How can we build a distributed system using Java?
 Use Java Remote Method Invocation (RMI) technology !

 Four major steps to design Distributed System in java

 Define remote interface

 Describes client/server communication

 Define server application to implement remote interface

 Same name as remote interface, ends with Impl

 Define client application that uses remote interface reference

 Interacts with server implementation

 Compile and execute server and client


General functioning diagram :
Layers and components of RMI

The complete RMI system has a FOUR layer/archticture

(1) Application Layer

(2) Proxy Layer

(3) Remote Reference Layer

(4) Transport Layer

Mainly the RMI application contains the THREE components,

(1) RMI Server

(2) RMI Client

(3) RMI Registry


RMI Architecture Diagram:
(1) RMI Server:
RMI Server contains objects whose methods are to be called remotely.

It creates remote objects and applies the reference to these objects in the
Registry, after that the Registry registers these objects who are going to be
called by client remotely.

(2) RMI Client:


The RMI Client gets the reference of one or more remote objects from
Registry with the help of object name.
Now, it can be invokes the methods on the remote object to access the
services of the objects as per the requirement of logic in RMI application.
Once the client gets the reference of remote object, the methods in the
remote object are invoked just like as the methods of a local object.
(3) RMI Registry:
In the Server side the reference of the object (which is invoked remotely) is
applied and after that this reference is set in the RMI registry.
When the Client call the method on this object, it’s not directly call but it
call by the reference which is already set in the Registry so first get the
object from this reference which is available at RMI Registry then after calls
the methods as per the requirement of logic in RMI application.

The RMI Registry is a naming service.


RMI server programs use this service to bind the remote java object with the
names.
Clients executing on local or remote machines retrieve the remote objects by
their name registered with the RMI registry and then execute methods on the
objects.
RMI creates a remote proxy for that object and sent it to clients.

An object proxy contains the reference to an object.


RMI Applications
RMI applications often comprise two separate programs, a server and a
client.

A typical server program creates some remote objects, makes references


to these objects accessible, and waits for clients to invoke methods on
these objects.

A typical client program obtains a remote reference to one or more remote


objects on a server and then invokes methods on them.

RMI provides the mechanism by which the server and the client
communicate and pass information back and forth.

Such an application is sometimes referred to as a distributed object


application.
Distributed object applications need to do the following:
Locate remote objects. Applications can use various
mechanisms to obtain references to remote objects.
For example, an application can register its remote objects with
RMI's simple naming facility, the RMI registry.
Alternatively, an application can pass and return remote object
references as part of other remote invocations.
Communicate with remote objects. Details of communication
between remote objects are handled by RMI.
To the programmer, remote communication looks similar to
regular Java method invocations.
Load class definitions for objects that are passed around.
Because RMI enables objects to be passed back and forth, it
provides mechanisms for loading an object's class definitions as
well as for transmitting an object's data.
Steps for Developing the RMI Application:
(1) Define the remote interface
(2) Define the class and implement the remote
interface(methods) in this class
(3) Define the Server side class
(4) Define the Client side class
(5) Compile source(java) files
(1) Define the remote interface:
 This is an interface in which we are declaring the
methods as per our logic and further these
methods will be called using RMI.
 Here we create a simple calculator and registration
application by RMI so that we need four methods
such as addition, subtraction, multiplication and
division as per logic for simple calculator that
registered in database.
 so create an interface name interface4&5.java and
declare these methods without body as per the
requirement of a simple calculator RMI
application.
import java.rmi.RemoteException;
import java.rmi.Remote;
public interface Interface5 extends Remote
{
public void delete(String a, String b) throws RemoteException;
public void sum(int x1, int x2, int x3, int x4, int x5) throws
RemoteException;
public void studentsregistretionform(String s1, String s2, String s3, String s4,
String s5) throws RemoteException;

The Remote interface serves to identify interfaces whose methods may be


invoked from a non-local virtual machine.
(2) Define the class and implement the remote interface(methods) in
this class:
The next step is to implement the interface so define a class server4 and
implements the interface4 so now in the class we must define the body of
those methods(insert and display ) as per the logic requirement in the
RMI application.
This class run on the remote server or database.
(3) Define the Server side class:
The server must bind its name to the registry by passing the reference
link with remote object name.
For that here we are going to use rebind method which has two
arguments:
The first parameter is a URL to a registry that includes the name of the
application .
The second parameter is an object name that is access remotely in
between the client and server.
This rebind method is a method of the Naming class which is available
in the java.rmi.* package.
The server name is specified in URL as a application name
(4) Define the Client side class:
To access an object remotely by client side that is already bind
at a server side by one reference URL we use the lookup method
which has one argument that is a same reference URL as already
applied at server side class.
This lookup method is a method of the Naming class which is
available in the java.rmi.* package.
The name specified in the URL must be exactly match the
name that the server has bound to the registry in server side class
and here the name is Client 4&6.
After getting an object we can call all the methods which
already declared in the interface 4&5 or already defined in the
class client 4&6 by this remote object.
Passing Parameters
When a client invokes a remote method with
parameters, passing the parameters is handled by the
stub and the skeleton.
The difference b/n local and remote object?
How does Java RMI do this?
Local object types: Cannot pass the object
reference! Instead, the stub serializes the object
parameter and sends it in a stream over a network.
Remote object types: The stub of the remote object
is passed. This is handled by RMI Callbacks.
RMI vs Socket Programming

How is Java RMI different from socket


programming?
RMI is a higher-level built API on top of sockets.
RMI enables you not only to pass data among
objects on different systems, but also to invoke
methods in a remote object.
RMI hides the details of socket programming, so that
the user can program at a higher-level of
abstraction.
The user doesn’t even have to worry about
multithreading.
 RMI applications are scalable and easy to
maintain.
 While clients and servers are tightly synchronized
in sockets, they are loosely coupled in RMI.
 While socket programming is primitive, RMI
focuses more on high-level programming.

You might also like