RMI Remote Method Invocation
RMI Remote Method Invocation
RMI Remote Method Invocation
Remote Method
Invocation
Building Distributed Application
• Programming where the code is spread
across geographically different locations and
accessed via the network.
• Java provides various ways to program over
the networks :
c. Socket programming
d. RMI (Remote Method Invocation
e. Java Messaging Service
Socket programming
• Good but it requires involved parties to
communicate via some application-level protocol
designing which can involve lot of coding and
could be error-prone
Request data
Response data
RMI
• RMI allows communication between two java
objects residing on different JVMs.
• This allows a java object residing on one
machine (client machine) to call a method on
another object located on the another
machine (server machine).
• The object on the server is called remote
object/server object and the method is called
remote method.
• RMI technology internally uses network
programming to achieve this.
RMI make this possible
server
client
warehouse.findProduct()
customer product warehouse
RemoteObject
No networking hassles!
How RMI makes it possible?
Like how we make things possible in real life!
remotely accessible.
2. RMI publishes all the remote services in a
way that clients can locate and access
the remote objects.
3. RMI makes it possible for the method
parameter to be shipped.
4. RMI makes it possible to ship the return
data too.
Processes that participate in RMI
• The Client invokes a method on a remote
object.
• The Server that owns the remote object
• The Object Registry is a name server that
relates objects with names.
Registering remote object
• The remote object can be registered in naming
and directory service using either
• Sun RMI library’s Bootstrap registry service
rmiregistry
• Or a other naming service that use JNDI.
• The server program registers server (remote)
objects with the bootstrap registry service. It
provides a name for the object using which the
client can lookup.
• The registry stores two objects as name-value
pair with respect to the server object Remote
Object Reference and Stub.
Remote Object Reference
• Associated with every remote object.
• Represents unique identifier of remote object in
the distributed system
• Internet address of the server +
port no. + time of creation + local
object no.+ name
• Client accesses the remote object by specifying
the name of the object along with the url. The
protocol used is ‘rmi’ and default port is 1099.
rmi://servername:port/objectname.
Stub
• When the client access the remote object by specifying
the name of the object along with the url
//host:port/name
• What is returned on lookup is something called a stub.
• A stub for a remote object acts as a client's local
representative or proxy for the remote object.
• Therefore, when client object attempts to make a call on
server object (remote object), it actually calls a method on
the stub that resides on the client machine
• Stubs are dynamically downloaded from the server m/c to
the client m/c.
• The stub has the same methods which remote object has.
The stub forwards the request via RMI framework to the
remote object.
Responsibility of a stub
• Stub takes care of sending the data to the
server object. The process of converting
data or object into a byte stream by the
stub is called parameter marshalling.
• Stub does the following tasks:
• Initiates a connection with remote JVM.
• Marshals the parameters and send to the
remote JVM.
• Waits for the reply from the remote
objects.
• Unmarshals the return value and returns
to the client program.
Skeleton/Server side process
• On the server side, the receiver object called
Skeleton performs the following actions for every
method call:
• It unmarshals the parameters.
• It locates the object to be called.
• It calls the desired method
• It gets the return data and marshals it.
• It sends the marshaled data to the client.
• In Java 1.x each remote object may have a
corresponding skeleton (from java 2 onwards,
skeletons are not required).
Parameters in RMI
• When a primitive data type is passed as a parameter
to a remote method, the RMI system passes it by
value.
• When an object is passed to a remote method it
sends the object itself, not its reference, between
JVMs. That is it is passed by value, not the reference
to the object. Similarly, when a remote method returns
an object, a copy of the whole object is returned to the
calling program.
• RMI uses Serialization to transform an object into a
linear format that can then be sent over the network
wire.
• There for the object parameters should be serializable
or it should be another remote object.
client server
return value
Remote interface
import java.rmi.*;
public interface Dict extends Remote{
public String getMeaning(String word) throws
RemoteException;
}
All the remote methods must throw this exception
Remote object class
• Must be a public class.
• Must implement Remote object’s interface.
• Must extends java.rmi.RemoteServer class which
is an abstract class that has some method for
basic communication between server and client
objects. java.rmi.server.UnicastRemoteObject is a
convenience class that extends RemoteServer
class and provides implementation for all the
methods. So invariably all the remote object
extends this class.
• Create a no-arg constructor that throws
RemoteException.
• Create remote objects and bind a name to it so
that the name and the remote object could be
registered in the registry.
You could do this in another class also
import java.rmi.*;
import java.rmi.server.*;
import java.util.Hashtable;
public class DictImpl extends
UnicastRemoteObject implements Dict{
Hashtable wordlist= new Hashtable();
public DictImpl() throws RemoteException{}
public String getMeaning(String word) throws
RemoteException{
return (String)wordlist.get(word);}
private void setWord(String word, String
meaning){
wordlist.put(word,meaning); }
public static void main(String str[]){
try{
DictImpl dic= new DictImpl();
dic.setWord("Fantasy","Dream");
dic.setWord("Earnest","sincere, honest");
java.rmi.Naming.rebind("EnglishDic",dic);
}catch(Exception e){
System.out.println(e);
}}
}
Generate stubs
• After creating remote object class, we need to
create stubs, so that client can download it. A
tool called rmic generates stubs
automatically.
rmic DictImpl
• Creates DictImpl_Stub.class
Write client class
public class DictClient {
public static void main(String str[]){
try{
String url="rmi://localhost/EnglishDic";
Dict dic =(Dict)java.rmi.Naming.lookup(url);
System.out.println(dic.getMeaning("Earnest"));
}catch(Exception e){
System.out.println(e.toString());
}
}}
Running the application
• Compile all source files.
javac *.java
2. Run rmic on the implementation class
rmic DictImpl
3. Start RMI registry
rmiregistry
4. Start the server
java DictImpl
5. Run the client
java -Djava.security.policy=client.policy
DictClient
Distributing RMI Classes
• For the server, the following classes must be available to
its class loader:
• Remote service interface definitions
• Remote service implementations
• Skeletons for the implementation classes (JDK 1.1
based servers only)
• Stubs for the implementation classes
• All other server classes
• For the client, the following classes must be available to
its class loader:
• Remote service interface definitions
• Stubs for the remote service implementation classes
• Server classes for objects used by the client (such as
return values)
• All other client classes
Automatic Distribution of Classes
• RMI can be done by loading of classes from FTP servers
and HTTP servers also.
• RMI supports this remote class loading through the
RMIClassLoader. If a client or server is running an RMI
system and it sees that it must load a class from a remote
location, it calls on the RMIClassLoader to do this work.
• For this to work, the server program must be run by
specifying certain properties like the code-base and
security policy.
• The property java.rmi.server.codebase is used to
specify a URL. This URL points to a file:, ftp:, or http:
location that supplies classes for objects that are sent this
JVM.
• When the property
java.rmi.server.useCodebaseOnly is set to true,
then the JVM will load classes from either a location
specified by the CLASSPATH environment variable or the
URL specified in this property .
RMI with Applet
import java.awt.Graphics;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class DictApp extends Applet {
Dict dic=null;
try{
String s=dic.getMeaning("Earnest");
g.drawString(s, 25, 50);
}catch(Exception e){
g.drawString(e.toString(),40,50);}
}
}
CORBA
• We looked at RMI using JRMP protocol
• Another way in which RMI can happen is using IIOP
(Internet Inter-ORB Protocol ) protocol of CORBA.
• What is CORBA?
• Common Object Request Broker Architecture : OMG's
open, vendor-independent architecture and
infrastructure that computer applications work together
over network.
• Using the standard protocol IIOP, a CORBA-based
program from any vendor, on almost any computer,
operating system, programming language, and
network, can interoperate with a CORBA-based
program from the same or another vendor, on almost
any other computer, operating system, programming
language, and network.
RMI-IIOP
• An RMI object using RMI/IIOP can communicate
with a remote CORBA object, regardless of the
implementation language of the CORBA object.
• Likewise, a CORBA object can interact with your
Java RMI objects directly.
• The RMI-IIOP tools and classes became a
standard part of the Java core environment as of
JDK 1.3.
• In order to convert your RMI objects to use IIOP,
implementation classes should extend the
javax.rmi.Portable-RemoteObject class,
rather than
java.rmi.server.UnicastRemoteObject.
Lookups
• The remote objects must be bound to names in the
CORBA Naming Service through the JNDI context.
• Remote objects are looked up using service through
the JNDI context.
• Object ref =
jndiContext.lookup("CabinHome");
CabinHomeRemote home = (CabinHomeRemote)
PortableRemoteObject.narrow(ref,
CabinHomeRemote.class);
• InitialContext is the starting context for performing
naming operations. All naming operations are relative
to a context. The initial context implements the
Context interface and provides the starting point for
resolution of names.
Activatable
• What we look at was the JDK 1.1 implementation which
provided only one way for clients to connect to remote
object a unicast, point-to-point connection.
• And this requires the remote service to be instantiated on
the server and exported to the RMI system.
• The Java 2 RMI supports activatable remote objects.
When a method call is made to the proxy for an
activatable object, RMI determines if the remote service
implementation object is dormant. If it is dormant, RMI will
instantiate the object and restore its state from a disk file.
Once an activatable object is in memory, it behaves just
like JDK 1.1 remote service implementation objects.
• The advantage is that for the system which have many
remote object classes, only a few of them can be active at
any one time. This save memory (and so gain
performance).