ADVANCED JAVA PROGRAMMING - UNIT II
RMI - OVERVIEW
Remote method invocation (RMI) is the action of invoking a method of a remote interface on a
remote object. Most importantly, a method invocation on a remote object has the same syntax as
a method invocation on a local object.
As we have seen, Java sockets, combined with serialization makes is it easy to send objects
across the network, however, sockets require the programmer to encode and decode the
messages that are being exchanged. As the application facilities grow, so do the complexities in
communication.
RMI is a full architecture for distributed computing. It provides a mechanism for distributing
objects as services, where a remote service request looks similar to a local one. The object is not
passed to and from the client/server, rather it is fixed in the one place on the server. The Virtual
Machine that is responsible for the object declares it exportable and makes it available to an
object server that can call on it when a request is received.
The remote client obtains a reference to the object and calls methods on it. To obtain this
reference to the object, the client must know the name of the object, where it is and also what
methods it has. Once this information is known, RMI takes care of the passing of information
back and forth, tasks such as: object serialization, object transport, exception handling
(Exceptions may occur when network connections are broken, one of the client/server pair may
crash etc..) and security management.
To use a remote object, we first must have a reference to it. An application can register its remote
objects with RMI's simple naming facility, the rmiregistry, or the application can pass and
return remote object references as part of its normal operation The only concise way to do this is
to have lookup information available (such as the method names) to the client, generally prior to
run-time. This is facilitated through the use of a pair of classes, termed skeletons and stubs, that
are derived directly from the remote object.
The client calls the remote object by using this lookup information, which is coded into the
client. If the lookup succeeds then the RMI Server could return the remote object's stub, which
acts as a stand-in for the remote object's methods. A call to any of the stub's methods, is sent
from the stub to the skeleton reference, that resides on the server-side. The skeleton in effect
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 1
ADVANCED JAVA PROGRAMMING - UNIT II
calls the method on the server-side and routes the remote object's response back through to the
stub.
The stub for a remote object acts as a client's local representative or proxy for the remote object.
The caller invokes a method on the local stub which is responsible for carrying out the method
call on the remote object. In RMI, a stub for a remote object implements the same set of remote
interfaces that a remote object implements. When a stub's method is invoked, it:
initiates a connection with the remote JVM containing the remote object,
marshals (writes and transmits) the parameters to the remote JVM,
waits for the result of the method invocation,
unmarshals (reads) the return value or exception returned, and,
returns the value to the caller.
In the remote JVM, each remote object may have a corresponding skeleton (in Java 2 platform-
only environments, skeletons are not required). The skeleton is responsible for dispatching the
call to the actual remote object implementation. When a skeleton receives an incoming method
invocation it does the following:
unmarshals (reads) the parameters for the remote method,
invokes the method on the actual remote object implementation, and
marshals (writes and transmits) the result (return value or exception) to the caller.
Figure 13.1. Skeleton and stub communication
This communication takes place over the RMI remote reference layer (which relies on the
TCP/IP transport layer).
Figure 13.2, “RMI Distributed Application” illustrates an RMI distributed application that uses
the registry to obtain references to a remote object. The server calls the registry to associate a
name with a remote object. The client looks up the remote object by its name in the server's
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 2
ADVANCED JAVA PROGRAMMING - UNIT II
registry and then invokes a method on it. The illustration also shows that the RMI system uses an
existing web server to load bytecodes of classes written in the Java programming language, from
server to client and from client to server, for objects when needed. RMI can load class bytecodes
using any URL protocol (e.g., HTTP, FTP, file, etc.) that is supported by the Java platform.
Figure 13.2. RMI Distributed Application
Note: Non-remote arguments to, and results from, a remote method invocation are passed by
copy rather than by reference. This is because references to objects are only useful within a
single virtual machine.
DISTRIBUTED APPLICATION ARCHITECTURE
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 (e.g. 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.
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 3
ADVANCED JAVA PROGRAMMING - UNIT II
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.
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 4
ADVANCED JAVA PROGRAMMING - UNIT II
The transport is responsible for connection set-up, 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 be
done 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.
The Stub/Skeleton Layer
The stub/skeleton layer is the interface between the application layer and the rest of the RMI
system. This layer does not deal with specifics of any transport, but transmits data to the remote
reference layer via the abstraction of marshal streams. Marshal streams employ a mechanism
called object serialization which enables Java objects to be transmitted between address spaces.
Objects transmitted using the object serialization system are passed by copy to the remote
address space, unless they are remote objects, in which case they are passed by reference.
A stub for a remote object is the client-side proxy for the remote object. Such a stub implements
all the interfaces that are supported by the remote object implementation. A client-side stub is
responsible for:
Initiating a call to the remote object (by calling the remote reference layer).
Marshaling arguments to a marshal stream (obtained from the remote reference layer).
Informing the remote reference layer that the call should be invoked.
Unmarshaling the return value or exception from a marshal stream.
Informing the remote reference layer that the call is complete.
A skeleton for a remote object is a server-side entity that contains a method which dispatches
calls to the actual remote object implementation. The skeleton is responsible for:
Unmarshaling arguments from the marshal stream.
Making the up-call to the actual remote object implementation.
Marshaling the return value of the call or an exception (if one occurred) onto the marshal stream.
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 5
ADVANCED JAVA PROGRAMMING - UNIT II
The appropriate stub and skeleton classes are determined at run time and are dynamically loaded
as needed, as described in Dynamic Class Loading. Stubs and skeletons are generated using the
rmic compiler.
The Remote Reference Layer
The remote reference layer deals with the lower level transport interface. This layer is also
responsible for carrying out a specific remote reference protocol which is independent of the
client stubs and server skeletons.
Each remote object implementation chooses its own remote reference subclass that operates on
its behalf. Various invocation protocols can be carried out at this layer, for example:
Unicast point-to-point invocation.
Invocation to replicated object groups.
Support for a specific replication strategy.
Support for a persistent reference to the remote object (enabling activation of the remote object).
Reconnection strategies (if remote object becomes inaccessible).
The remote reference layer has two cooperating components: the client-side and the server-side
components. The client-side component contains information specific to the remote server (or
servers, if the remote reference is to a replicated object) and communicates via the transport to
the server-side component. During each method invocation, the client and server-side
components perform the specific remote reference semantics. For example, if a remote object is
part of a replicated object, the client-side component can forward the invocation to each replica
rather than just a single remote object.
In a corresponding manner, the server-side component implements the specific remote reference
semantics prior to delivering a remote method invocation to the skeleton. This component, for
example, could handle ensuring atomic multicast delivery by communicating with other servers
in the replica group.
The remote reference layer transmits data to the transport layer via the abstraction of a stream-
oriented connection. The transport takes care of the implementation details of connections.
Although connections present a streams-based interface, a connectionless transport may be
implemented beneath the abstraction.
The Transport Layer
In general, the transport layer of the RMI system is responsible for:
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 6
ADVANCED JAVA PROGRAMMING - UNIT II
Setting up connections to remote address spaces.
Managing connections.
Monitoring connection "liveness."
Listening for incoming calls.
Maintaining a table of remote objects that reside in the address space.
Setting up a connection for an incoming call.
Locating the dispatcher for the target of the remote call and passing the connection to this
dispatcher.
The concrete representation of a remote object reference consists of an endpoint and an object
identifier. This representation is called a live reference. Given a live reference for a remote
object, a transport can use the endpoint to set up a connection to the address space in which the
remote object resides. On the server side, the transport uses the object identifier to look up the
target of the remote call.
The transport for the RMI system consists of four basic abstractions:
An endpoint is the abstraction used to denote an address space or Java virtual machine. In the
implementation, an endpoint can be mapped to its transport. That is, given an endpoint, a specific
transport instance can be obtained.
A channel is the abstraction for a conduit between two address spaces. As such, it is responsible
for managing connections between the local address space and the remote address space for
which it is a channel.
A connection is the abstraction for transferring data (performing input/output).
The transport abstraction manages channels. Each channel is a virtual connection between two
address spaces. Within a transport, only one channel exists per pair of address spaces, the local
address space and a remote address space. Given an endpoint to a remote address space, a
transport sets up a channel to that address space. The transport abstraction is also responsible for
accepting calls on incoming connections to the address space, setting up a connection object for
the call, and dispatching to higher layers in the system.
A transport defines what the concrete representation of an endpoint is, so multiple transport
implementations may exist. The design and implementation also supports multiple transports per
address space, so both TCP and UDP can be supported in the same virtual machine.
Java RMI
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 7
ADVANCED JAVA PROGRAMMING - UNIT II
Remote method invocation(RMI) allow a java object to invoke method on an object running on
another machine. RMI provide remote communication between java program. RMI is used for
building distributed application.
Concept of RMI application
A RMI application can be divided into two part,Client program and Server program.
A Server program creates some remote object, make their references available for the client to
invoke method on it. A Client program make request for remote objects on server and invoke
method on them. Stub and Skeleton are two important object used for communication with
remote object.
Stub
In RMI, a stub is an object that is used as a Gateway for the client-side. All the outgoing request
are sent through it. When a client invokes the method on the stub object following things are
performed internally:
A connection is established using Remote Virtual Machine.
It then transmits the parameters to the Remote Virtual Machine. This is also known as Marshals
After the 2nd step, it then waits for the output.
Now it reads the value or exception which is come as an output.
At last, it returns the value to the client.
Skeleton
In RMI, a skeleton is an object that is used as a Gateway for the server-side.All the incoming
request are sent through it. When a Server invokes the method on the skeleton object following
things are performed internally:
All the Parameters are read for the remote method.
The method is invoked on the remote object.
It then writes and transmits the parameters for the result. This is also known as Marshals.
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 8
ADVANCED JAVA PROGRAMMING - UNIT II
Stub and Skeleton
Stub act as a gateway for Client program. It resides on Client side and communicate
with Skeleton object. It establish the connection between remote object and transmit request to
it.
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 9
ADVANCED JAVA PROGRAMMING - UNIT II
Skeleton object resides on server program. It is responsible for passing request from Stub to
remote object.
Creating a Simple RMI application involves following steps
Define a remote interface.
Implementing remote interface.
create and start remote application
create and start client application
Define a remote interface
A remote interface specifies the methods that can be invoked remotely by a client. Clients
program communicate to remote interfaces, not to classes implementing it. To be a remote
interface, a interface must extend the Remote interface of java.rmi package.
import java.rmi.*;
public interface AddServerInterface extends Remote
{
public int sum(int a,int b);
}
Copy
Implementation of remote interface
For implementation of remote interface, a class must either extend UnicastRemoteObject or use
exportObject() method of UnicastRemoteObject class.
import java.rmi.*;
import java.rmi.server.*;
public class Adder extends UnicastRemoteObject implements AddServerInterface
{
Adder()throws RemoteException{
super();
}
public int sum(int a,int b)
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 10
ADVANCED JAVA PROGRAMMING - UNIT II
{
return a+b;
}
}
Create AddServer and host rmi service
You need to create a server application and host rmi service Adder in it. This is done
using rebind() method of java.rmi.Naming class. rebind() method take two arguments, first
represent the name of the object reference and second argument is reference to instance
of Adder
import java.rmi.*;
import java.rmi.registry.*;
public class AddServer {
public static void main(String args[]) {
try {
AddServerInterface addService=new Adder();
Naming.rebind("AddService",addService); //addService object is hosted
with name AddService
}
catch(Exception e) {
System.out.println(e);
}
}
}
Create client application
Client application contains a java program that invokes the lookup() method of
the Naming class. This method accepts one argument, the rmi URL and returns a reference to an
object of type AddServerInterface. All remote method invocation is done on this object.
import java.rmi.*;
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 11
ADVANCED JAVA PROGRAMMING - UNIT II
public class Client {
public static void main(String args[]) {
try{
AddServerInterface st =
(AddServerInterface)Naming.lookup("rmi://"+args[0]+"/AddService");
System.out.println(st.sum(25,8));
}
catch(Exception e) {
System.out.println(e);
}
}
}
Steps to run this RMI application
Save all the above java file into a directory and name it as "rmi"
compile all the java files
javac *.java
Start RMI registry
start rmiregistry
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 12
ADVANCED JAVA PROGRAMMING - UNIT II
Run Server file
java AddServer
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 13
ADVANCED JAVA PROGRAMMING - UNIT II
Run Client file in another command prompt abd pass local host port number at run time
java Client 127.0.0.1
Example:
Program: Power.java
import java.rmi.*;
public interface Power extends Remote
{
public int power1()throwsRemoteException;
}
Copy
Program: PowerRemote.java
import java.rmi.*;
import java.rmi.server.*;
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 14
ADVANCED JAVA PROGRAMMING - UNIT II
import java.util.Scanner;
public class PowerRemote extends UnicastRemoteObject implements Power
{
PowerRemote()throws RemoteException
{
super();
}
public int power1(int z)
{
int z;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base number ::");
int x = sc.nextInt();
System.out.println("Enter the exponent number ::");
int y = sc.nextInt();
z=y^x;
System.out.println(z);
}
}
Copy
MyServer.java
import java.rmi.*;
import java.rmi.registry.*;
public class MyServer
{
public static void main(String args[])
{
try
{
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 15
ADVANCED JAVA PROGRAMMING - UNIT II
Power stub=new PowerRemote();
Naming.rebind("rmi://localhost:1995/shristee",stub);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Copy
MyClient.java
import java.rmi.*;
public class MyClient
{
public static void main(String args[])
{
try
{
Power stub=(Power)Naming.lookup("rmi://localhost:1995/shristee");
System.out.println(stub.power1());
}
catch(Exception e){}
}
}
Copy
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 16
ADVANCED JAVA PROGRAMMING - UNIT II
DEPARTMENT OF COMPUTER SCIENCE – RASC – R.SUGANYA Page 17