You are on page 1of 10

Distributed Communication:

Objects located in two different JVM’s wanted to have


communication.
The various technologies available in java is
RMI: ( Remote Method Invocation) Java to java communicaiton.

Note: But as per JVM specification the communication between the


objects located in different JVM’s is not possible directly. So this made
rise for socket programming.

Client Server

Socket 2 Server
Application Socket
5 Applicaiton

1 6 4 3
Service
Client
Client Object

We take the responsibility to design the socket and server socket, and
while socket designing the socket, we give the information where the
server socket is running. This results in increase of Application
development time.
Role of Socket Application (proxy)
1) Takes the request from the client.
2) Converts the request into server socket understandable format or
serializable format
3) Gives the request to the server socket
4) Collects the response from the server socket
5) Deserialize the response.
6) Finally gives the response to the client.

Role of Server Socket Application (Helper)


1) It should be running on a particular Port no.
2) After receiving the request it has to decode the request
3) Give the request to the service object
4) Collects the response from the service object
5) Encode the response
6) Finally response is given to socket application.

Remote Method Invocation (RMI):


We wanted our application to be third party vendor independent. Even
though we make use of third party vendor provided socket and server
socket.
To meet the above requirement SunMicroSystem has introduced the
concept of Remote Method Invocation (RMI).
RMI provides the standards, to design, develop, Network enabled
Objects.
In RMI client and server are provided by some third party vendors,
Socket and server socket are also provided by some other third party
vendors, for communication the API is provided by Sun Micro Systems.
In RMI we need,
Client: Who makes a request from a remote JVM.
Server: Which holds Remote Objects.
Socket: In RMI we call it as Stub.
Sever Socket: In RMI we call it as Skelton.

Elements require to write a RMI Application:


1) Write an interface which you are exposing to the client.
( ie., a Remote Interface)
2) Your interface should extend java.rmi.Remote where Remote is a
marker interface.
3) Here, declare methods which you wanted to expose to your clients.
4) Methods declared should throw java.rmi.RemoteException.

Program: //Interface
Public interface MyRemote extends java.rmi.Remote
{
String sayHai() throws java.rmi.RemoteException;
}
//Save it as MyRemote .java

//Implementation Class
How to write Implementation class
1) Here the primary requirement is to make the remote object network
enabled.
Your class should extend java.rmi.server.Unicast.Remoteobject
Then declare a constructor to a class, and the constructor should
throw (java.rmi.RemoteException)

In actual, we are writing the implementation class to provide


implementation for the methods declared in MyRemote interface, to
do this your class should implement your interface.
//Implementation class
Public class MyImpl extends java.rmi.server.UnicastRemoteObject
implements MyRemote
{
Public MyImpl() throws java.rmi.RemoteException
{ }
Public String sayHai()
{
System.out.println(“In say Hai “);
Return “ Hai Hai from rmi”;
}
}
2) Then generate stub and skeleton class files, using the tool given by
sun micro system.

Rmic<Implementation class name>


C:\rmi> rmic MyImpl

3) Write a registry application using which you place the remote


object into RMI Registry.
First get the remote object so create an instance of your
implementation class
MyImpl mi=new MyImpl();

The above step leads to invocation of MyImpl() constructor


declared in your implementation class, which in turn invokes the
constructor of UnicastRemoteObject.
At this point Remote Object becomes network enabled.

4) In the Unicast Remote Object you have a method named as export


object()
i) Skeleton instance is created, and it has a reference to Remote
object.
ii) Stub instance is created it is set with the property of the
skeleton have property means port no where skeleton is
running.
iii) Stub is attached as a logical reference to the remote object.
iv) The next responsibility is to place the Remote Object into the
RMI registry, so here we have class named as
Java.rmi.Naming, this class consists of the following static
methods.
Bind(), rebind(), unbind(), lookup(), list().

//Registry Application
When we bind the remote object into the rmi registry, we
expect remote object into the rmi registry, rather we get stub
instance.

Program Registry Application

Public class MyRegistry


{
Public static void main(String s[])throws Exception
{
MyImpl mi= new Myimpl();

Java.rmi.Naming.bind(“lalith”,mi);
System.out.println(“Object is binded into RMI registry”);
}
}
//Save as MyRegistry.java

5) Finally write the client application first make a request with


lookup(“ “) Then make the request with the business logic
methods are exposing to the client via your remote interface.
//Client Application

Public class Client


{
Public static void main(String s[]) throws Exception
{
Java.rmi.Remote r=java.rmi.Naming.lookup(“lalith”);
MyRemote mr=(MyRemote)r;
System.out.println(mr.sayHai());
}
}

Steps to execute the above application


1) Set path
2) Set class path
3) Javac *.java
4) //generate stub and skeleton
Rmic <Implementation class name>
Rmic MyImpl
5) //Start rmi registry
Start rmi registry
6) //Start your Registry
Start java MyRegistry
7) Java client

RMI Architecture

Client Server
property
Stub Instance 4 Skeleton
instance stub

7
5
3 8 6
RO

Client

lookup() RMI Registry 1


2 Bind()

Stub instance

Required class files to be provided to the client are:


1. MyRemote class file
2. Stub class file
3. Client class file

But in the server we have


1) Stub
2) Skeleton
3) MyImpl
4) MyRemote
5) MyRegistry
So we conclude that in rmi, everything is developed in the
server and when the client makes the request required
respectively files are given to the client.

You might also like