Chapter 6
Remote Method Invocations
(RMI)
1
Introduction
• RMI 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.
RMI uses object serialization for the data exchange
between JVM1 and JVM2.
RMI uses stub and skeleton object for
communication with the remote object.
2
Cont…
A remote object is an object whose method can be
invoked from another JVM.
Stub
• 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.
3
Cont…
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.
5) It finally, returns the value to the caller.
4
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.
5
Cont..
Any RMI application consists of an RMI server, a
client, and the registry (a naming service).
These three components could run on three different
JVMs running on different networked computers.
The RMI server
creates Java objects that implement business logic,
registers them with the naming service, and
waits for remote clients to invoke methods on them.
A client application
gets a reference to a remote server object or objects from
the registry and then invokes methods on this remote
object
6
RMI Architecture
1. Start registry
ks up
o
l i e nt lo me
3. C r by na RMI Registry
e
serv
S e r ve r found,
4. 2. Register server
t u b r e c eived with registry
s
RMI client
5. Client calls stub method ,
RMI Server
which communicates
with method on server
7
The Remote Interface
A remote interface is defined by extending the
Remote interface that is provided in the
[Link] package.
declares methods that clients can invoke from a
remote virtual machine .
The remote interface must satisfy the following
conditions:
It must extend the interface Remote.
Each remote method declaration in the
remote interface must include the exception
8
class RemoteException
• superclass of all RMI exceptions . When a remote
method invocation fails, the exception
RemoteException is thrown. Communication
failure, protocol errors and failure during
marshalling or unmarshalling of parameters or
return values are some reasons for RMI failure.
Class UnicastRemoteObject
• Create remote objects and export them
• i.e. the classes make the remote objects available to
remote clients.
9
Developing applications with rmi
Writing distributed RMI applications involves the
following steps:
1) Declaring the remote interface.
2) Implementing the remote interface.
3) Writing a Java client that connects to the remote
server and calls remote methods.
4) Starting the registry and registering the RMI
server with it.
5) Starting the server and the client applications.
10
1. Defining remote interfaces
The Java classes that you deploy on the server side have to
implement remote interfaces, which declare business
method(s) to be invoked remotely by RMI clients
Following are the rules for creating remote interfaces
The remote interface must declare public methods to
allow clients to perform remote method invocation.
The remote interface must extend [Link].
Each method must declare [Link].
Public interface RMIinterface extends Remote
{
Public desplay(String name) throws RemoteException;
}
11
2. Implementing remote interfaces
• Although the remote interface just declares the methods,
you need to create a class that will run on the server side
and provide implementation for those methods.
• The easiest way to export an RMI server instance is by
extending it from [Link]
public class Server extends UnicastRemoteObject
implements RMIInterface{
Public double add(double ,double b) throws
RemoteException
{
return a+b;
}}
12
3. Registering remote objects
• To make a remote object available to clients, you need to
bind it to some name in a registry, a naming service that
knows where exactly in the network your RMI server is
running. This will allow Java clients to look up the object
on the host machine by name
• There are two methods in the class [Link] that
can bind an object in the registry.
• The method bind() binds an RMI server to a name. It
throws alreadyBoundException if the binding already
exists.
• The method rebind() replaces any preexisting binding with
the new one.
13
Cont..
public static void main(String args[]) throws
RemoteException{
try {
Registry r= [Link](1099);
[Link](“myserver", new Server());
[Link]("the server is running");
}
catch(Exception ex ){
[Link](ex);
} }
14
4. Writing rmi clients
• Writing rmi clients program, running anywhere on the
Internet, performs a lookup in the registry on the host
machine and obtains a reference to the remote object
Class client(){
double a=12,b=88;
RMIInterface db= (RMIInterface)
[Link]("rmi://[Link]:1099/myserver");
try {
double c = [Link](a, b);
[Link](“”+c)
} catch (Exception ex) {
[Link]();
} } 15
5. Finding remote objects
• RMI clients ̀find remote services by using a
naming or directory service.
• A naming service runs on a known host and port
number.
• RMI server can start its own registry that offers
naming services for RMI clients.
• The behavior of the registry is defined by the
interface [Link]
16
Cont..
• When the client wants to invoke methods on a
remote bject it obtains a reference to that object by
looking up the name.
• The lookup returns to the client a remote reference
• The method lookup() takes the object name’s URL
like
rmi://<host_name>[:<name_service_port>]/<servi
ce_name>
17
Thank You
18