Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

An Overview of RMI Applications: Distributed Object Application

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

An Overview of RMI Applications

Java RMI is a mechanism to allow the invocation of methods that reside on different
Java Virtual Machines (JVMs). The JVMs may be on different machines or they
could be on the same machine. In either case, the method runs in a different address
space than the calling process.

Java RMI is an object-oriented remote procedure call mechanism.

∙​ 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​.

There are three entities involved in running a program that uses RMI:

Client​: this is the program that you write to access remote methods

Server​: this is the program that you write to implement the remote methods - clients
connect to the server and request that a method be executed. The remote methods to
the client are local methods to the server.

Object registry​: this is a program that you use.


The object registry runs on a known port (1099 by default)
A server, upon starting, registers its objects with a textual name with the object
registry.
A client, before performing invoking a remote method, must first contact the
object registry to obtain access to the remote object.

To Implemet RMI In Java One can Create Following Programs:


1)​ ​Remote Interface​: This program contains all the abstract methods which called by
clinet remotely.This program use interface instead of class which extends the
java.rmi.Remote​ interface which contains all remote features. ​Any method that can be
remotely invoked in Java/RMI may throw a ​java.rmi.RemoteException​.
java.rmi.RemoteException​ is the superclass of many more RMI specific exception
classes.
Program: serverinterface.java

import java.rmi.*;
public interface serverinterface extends Remote
{
int getAmount(int qty) throws RemoteException;

}
2) ​Impementing Remote interface​:in second program all abstract method which
declared in interfaces are implemented in class. All methods which implement in class
must throws java.rmi.RemoteException.
This class must extends UnicastRemoteObject class to acquire the properties and
behavior of Remote Object. The UnicastRemoteObject subclass exports the remote
object to make it available for servicing incoming RMI calls.
Program: serverclass.java

import java.rmi.*;
import java.rmi.server.*;
public class serverclass extends UnicastRemoteObject implements serverinterface
{ private int rate;
public serverclass(int r) throws RemoteException
{ rate=r;}
public int getAmount(int qty)throws RemoteException
{ return(rate*qty);
}
}
3) Client Registry: This program is use to register client by binding their names. To
binding name one can use Naming.rebind() method, each client name bind with
RemoteObjects which is further search by client machine using method Naming.lookup()
which returns reference of Remote object to client machine to call remote methods.
Program:clientregistry.java

import java.rmi.*;
public class clientregistry
{ public static void main(String arg[])
{ try
{ serverclass objsony=new serverclass(9000);
serverclass objlg=new serverclass(8999);
Naming.rebind("SONY",objsony);
Naming.rebind("LG",objlg);
​​ }
catch(Exception e)
{System.out.println(e); } }}

4)​ ​Client Program: This program Calls the Remote Method.


Program:client.java
import java.rmi.*;
public class client
{ public static void main(String arg[])
{ try
{ String serurl="rmi://192.168.1.2/SONY";
serverinterface i=(serverinterface)Naming.lookup(serurl);
int amt=i.getAmount(4);
System.out.println("Amount is:"+amt); }
catch(Exception e)
{System.out.println(e); } }}

To run the above program follow the following instruction.


∙​ ​Compile All Programs
∙​ ​Create a Stub using rmi compiler
rmic serverclass
this will create stub class name serverclass_stub.class which is further
use to establish communication between client and server.
∙​ ​Start RMI server
rmiregistry
∙​ ​Register the clients on server
Java clientregistry
∙​ ​Run client program to invoke server methods
Java client
How remoting works
To invoke a remote method, the client makes a call to the client proxy. The client side proxy
packs the call parameters into a request message and invokes a wire protocol JRMP(in
Java/RMI) to ship the message to the server. At the server side, the wire protocol delivers
the message to the server side stub. The server side stub then unpacks the message and
calls the actual method on the object Java/RMI, the client stub is called the ​stub​ or ​proxy
and the server stub is called ​skeleton​.

Application of RMI:-
RMI applications are often comprised of two separate programs: a server and a
client. A typical server application creates some remote objects, makes references
to them accessible, and waits for clients to invoke methods on these remote
objects. A typical client application gets a remote reference to one or more remote
objects in the 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
∙​ ​Locate remote objects:​ Applications can use one of two mechanisms to
obtain references to remote objects. 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.
∙​ ​Communicate with remote objects​: Details of communication between
remote objects are handled by RMI; to the programmer, remote
communication looks like a standard Java method invocation.
∙​ ​Load class bytecodes for objects that are passed around​: Because RMI
allows a caller to pass objects to remote objects, RMI provides the necessary
mechanisms for loading an object's code, as well as for transmitting its data.
The following illustration depicts an RMI distributed application that uses the
registry to obtain a reference to a remote object. The server calls the registry to
associate (or bind) a name with a remote object. The client looks up the remote
object by its name in the server's registry and then invokes a method on it. The
illustration also shows that the RMI system uses an existing Web server to load
class bytecodes, from server to client and from client to server, for objects when
needed.

You might also like