RMI Application

Download Report

Transcript RMI Application

RMI
Varun Saini
Ying Chen
What is RMI?




RMI is the action of invoking a method of
a remote interface on a remote object.
It is used to develop applications that
communicate between virtual machines
RMI is a means of doing distributed
computing
RMI hides the underlying mechanism of
transporting method arguments and
return values across the network.
Example
Server returns sum of two numbers
Client calls the add() method of the
server and passes two numbers
We will need four files







AddServerIntf.java
AddServerImpl.java
AddServer.java
AddClient.java
Java Complete ReferenceSchildt Naughton
Remote Interface



a remote interface is an interface that
declares a set of methods that may be
invoked from a remote Java virtual
machine.
Must extend java.rmi.Remote
The interface java.rmi.Remote is a marker
interface that defines no methods


public interface Remote {}
All methods must throw RemoteException
AddServerIntf.java
import java.rmi.*;
public interface AddServerIntf extends Remote
{
double add (double d1, double d2)
throws RemoteException;
}
RMI Registry



Simple Name Repository
Server binds a name with an object
implementation
Client can query the registry for
checking the availability of a server
object
AddServerImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends
UnicastRemoteObject implements AddServerIntf
{
public AddServerImpl() throws RemoteException{}
public double add (double d1, double d2)
throws RemoteException
{return d1+d2;}
}
Parameter Passing

Passing by Copy



Local objects and exceptions are passed by
copy.
Use JAVA object serialization
Passing by Reference

Remote Object Passed by reference
AddServer.java
import java.rmi.*;
public class AddServer
{
public static void main(String[] args)
{ try{
AddServerImpl addServerImpl=new
AddServerImpl();
Naming.rebind("AddServer",addServerImpl);
} catch(Exception e){System.out.println(e);}
}
}
AddClient.java
import java.rmi.*;
public class AddClient
{
public static void main(String[] args)
{
try{
String addServerURL = "rmi://"+args[0]+"/AddServer";
AddServerIntf addServerIntf=
(AddServerIntf)Naming.lookup(addServerURL);
}
}
double d1=Double.valueOf(args[1]).doubleValue();
double d2=Double.valueOf(args[2]).doubleValue();
System.out.println("Sum= "+addServerIntf.add(d1,d2));
} catch(Exception e){System.out.println(e);}
Stubs and Skeletons

Stub



Skeleton


resides on client
Provides interface of the server
Resides on server
Generate stubs and skeletons

rmic AddServerImpl
RMI Sequence of Actions



The RMI Server creates an instance of the 'Server
Object' which extends UnicastRemoteObject
The constructor for UnicastRemoteObject "exports"
the Server Object. A TCP socket which is bound to
an arbitrary port number is created and a thread is
also created that listens for connections on that
socket.
The server registers the server object with the
registry.
RMI Sequence of Actions
A client obtains the stub by calling the
registry, which hands it the stub directly.
When the client issues a remote method
invocation to the server, the stub class




opens a socket to the server on the port
specified in the stub itself, and
Sends the RMI header information as
described in the RMI spec.
RMI Sequence of Actions



The stub class marshalls the arguments
On the server side, when a client connects to
the server socket, a new thread is forked to
deal with the incoming call.
The server calls the "dispatch" method of the
skeleton class, which calls the appropriate
method on the object and pushes the result
back down the wire
RMI Performance

RMI vs. Local call(200Mhz, NT, JDK no JIT)
RMI
(cross process)
1.6 ms/call


700ns/call
setup time: RMI 40% slower than CORBA
binding: RMI 200-1500ms slower than CORBA
CORBA vs. RMI

Pengwu97
local
Call time (no configuration available)


RMI
(cross machine)
1.8ms/call
1000 calls, one argument, JDK1.1.1
Resources




java.sun.com
http://gsraj.tripod.com/java/rmi_interna
ls.html
http://wwwcsag.ucsd.edu/individual/achien/cs491f97/reading.html[pengwu97]
Java Complete Reference-Schildt
Naughton