RMI

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Distributed Computing

Model Applying RMI


What is RMI?

 RMI stands for “Remote Method Invocation”


means communicating the object across the
network.
 “RMI is a Java API that is used to develop
distributed application.”
 It uses remote object to call the method on
another object which is located on a different
JVM.
Basic RMI diagram

 RMI provides for remote


communication between
programs written in the
JAVA as shown in General
function diagram.
 Understanding stub and skeleton
 RMI uses stub and skeleton object for communication with the remote object.

 Stub
 Stub reside on client side.
 Remote object forward request to stub at client side and then stub forward
request to network.
 Stub perform the following steps:
 Initiate remote objects.
 Remote objects passes parameter to the stub.
 Stub forward the request to skeleton.
 Stub displays the result to the caller.
 Skeleton
Skeleton reside on server side.
 Skeleton perform following steps:
 Skeleton receives the request from the stub.
 Skeleton calls the remote method requested by the stub.
 It display the result to the stub.
Architecture of RMI
Layers Of RMI

 The complete RMI has four layers.


1. Application Layer
2. Proxy Layer
3. Remote Reference Layer
4. Transport Layer
1. Application Layer:

It’s a responsibility for the actual logic of the


client and server applications. Generally at the
server side class contain implementation logic
and also apply the reference to the appropriate
object as per the requirement of the logic in
application.
2. Proxy Layer

It’s also called “Stub/Skelton Layer”.


A stub class is a client side proxy handles the
remote objects which are getting from the
reference.
A skeleton class is a server side proxy that set
the reference to the object which are
communicates with stub.
3. Remote Reference layer (RRL)

It’s responsible for manage the references made


by the client to the remote object on the server
so it available on both JVM. The client side RRL
receives the request for methods from the stub
that is transferred into byte stream process
called serialization and then these data send to
the server side RRL.
4. Transport layer

It’s also called “Connection Layer”.


It’s also responsible for the managing existing
connection and also setting up new connections.
So it’s a work like a link between the RRL on the
client side and the RRL on the server side.
Components of RMI

 RMI application contains three components.


1. RMI Server
2. RMI Client
3. RMI Registry
Components Of RMI
1. RMI Server

RMI Server contains object whose methods are


to be called remotely. It creates remote objects
and applies the reference to these objects in the
registry, after that the Registry registers these
objects who are going to be called by client
remotely.
2. Remote client

The RMI client gets the reference of one or more


remote objects from Registry with the help of
object name. Now, It can be invokes the method
on the remote object to the access the services
of the objects as per requirement of logic in RMI
application.
3. RMI registry

In the server side the reference of the object is


applied and after that this reference is set in
the RMI registry. When the client call the
method on this object, It’s not directly call but
it call by the reference which is already set in
the registry so first get the object from this
reference then after calls the methods as per
the requirement of logic in RMI application.
RMI registry

 The RMI Registry is a naming service. RMI server programs use the
server to bind the remote java object with the names.
 Clients executing on local or remote machines retrieve the
remote objects by their names.
 Client executing on local and remote machine receive the remote
object by their names registered with the RMI registry and then
execute method on the object.
 RMI create a remote proxy for that object and send to the client.
 An object proxy contain the reference to an object.
 Start registry by following command

 Start RMI registry


 By default the port 1099 is used by the RMI registry to lockup remote object.

 Now second step to bind the remote object with the RMI registry execute the server
program.

 To use the remote object execute the client program.


 From the figure we can see that server call the RMI registry map a name with a
remote object represented by black circle.

 By using the name of remote object in server registry client program locate the
remote object and then method are called on the remote object by the client
program.
 Goals of RMI
 Following are the goals of RMI −
 • To minimize the complexity of the application.
 • To preserve type safety.
 • Distributed garbage collection.
 • Minimize the difference between working with local and remote
objects.
Steps to write RMI Program

These given the 6 steps to write the RMI program.


 Create the remote interface
 Provide the implementation of the remote interface
 Compile the implementation class and create the stub and skeleton objects
using the rmi tool
 Start the registry service by rmi registry tool
 Create and start the remote application
 Create and start the client application
 1) create the remote interface
 For creating the remote interface, extend the Remote interface and declare the Remote
Exception with all the methods of the remote interface. Here, we are creating a remote
interface that extends the Remote interface. There is only one method named add() and it
declares Remote Exception.
 1. import java. Rmi.*;
 2. public interface Adder extends Remote{
 3. public int add(int x,int y)throws Remote Exception;
 4. }
 2) Provide the implementation of the remote
interface
 Now provide the implementation of the remote interface. For providing the
implementation of the Remote interface, we need to
 Either extend the UnicastRemoteObject class,
 or use the export Object() method of the UnicastRemoteObject class
 In case, you extend the UnicastRemoteObject class, you must define a
constructor that declares Remote Exception.
 1. import java. RMI*;
 2. import java.rmi.server*;
 3. public class Adder Remote extends UnicastRemoteObject implements
Adder{
 4. Adder Remote()throws Remote Exception{
 5. super();
 6. }
 7. public int add(int x,int y){return x+y;}
 8. }
 3) create the stub and skeleton
objects using the rmic tool.
 Next step is to create stub and skeleton objects using the
rmi compiler. The rmic tool invokes the RMI compiler and
creates stub and skeleton objects.
 rmic Adder Remote
 4) Start the registry service by the rmi registry
tool
 Now start the registry service by using the rmiregistry tool. If you don't
specify the port number, it uses a default port number. In this example, we
are using the port number 5000.
 Rmi registry 5000
5) Create and run the server application

public static java.rmi.Remote lookup(java.lang.String) throws It returns the reference of the remote object.
java.rmi.NotBoundException, java.net.MalformedURLException,
java.rmi.RemoteException;

public static void bind(java.lang.String, java.rmi.Remote) throws It binds the remote object with the given name.
java.rmi.AlreadyBoundException, java.net.MalformedURLException,
java.rmi.RemoteException;

public static void unbind(java.lang.String) throws java.rmi.RemoteException, It destroys the remote object which is bound with the given name.
java.rmi.NotBoundException, java.net.MalformedURLException;

public static void rebind(java.lang.String, java.rmi.Remote) throws It binds the remote object to the new name.
java.rmi.RemoteException, java.net.MalformedURLException;

public static java.lang.String[] list(java.lang.String) throws It returns an array of the names of the remote objects bound in the registry.
java.rmi.RemoteException, java.net.MalformedURLException;
 In this example, we are binding the remote object by the name sonoo.
 1. import java. Rmi.*;
 2. import java.rmi.registry.*;
 3. public class My Server{
 4. public static void main(String args[]){
 5. try{
 6. Adder stub=new AdderRemote();
 7. Naming.rebind("rmi://localhost:5000/sonoo",stub);
 8. }catch(Exception e){System.out.println(e);}
 9. }
 10. }
 6) Create and run the client application
 At the client we are getting the stub object by the lookup() method of the
Naming class and invoking the method on this object. In this example, we are
running the server and client applications, in the same machine so we are
using localhost. If you want to access the remote object from another
machine, change the localhost to the host name (or IP address) where the
remote object is located.
 import java.rmi.*;
 public class MyClient{
 public static void main(String args[]){
 try{
 Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
 System.out.println(stub.add(34,4));
 }catch(Exception e){}
 }
 }
Distributed object model

 Views a distributed system as a series of interacting object


 Based on some underlying message passing protocol invisible to the
programmer
 THREE main technologies : RMI ,CORBA, AND DCOM.
DISTRIBUTED OBJECT COMPUTING

 Enable any object in the local system to directly interact with an object on a
remote host.

 GOALS
RMI reduce the complexity of distributed object
Support remote object communication that call object on
different places.
Easy to use.

You might also like