[Go to site: main page, start]

0% found this document useful (0 votes)
2 views26 pages

Module 5 - Part 2 - Remote Method Invocation (RMI) Notes

The document provides an overview of Remote Method Invocation (RMI) in Java, detailing its architecture and the roles of stub and skeleton objects in facilitating communication between client and server applications. It outlines the steps to implement an RMI application, including creating a remote interface, providing its implementation, and running both server and client applications. Additionally, it includes code examples for creating the remote interface and implementing the server and client functionalities.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views26 pages

Module 5 - Part 2 - Remote Method Invocation (RMI) Notes

The document provides an overview of Remote Method Invocation (RMI) in Java, detailing its architecture and the roles of stub and skeleton objects in facilitating communication between client and server applications. It outlines the steps to implement an RMI application, including creating a remote interface, providing its implementation, and running both server and client applications. Additionally, it includes code examples for creating the remote interface and implementing the server and client functionalities.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OOPS - Module 5

Remote Method Invocation


(RMI)

Vu Pham 1
RMI Architecture
• RMI is an API that provides a mechanism to create
distributed applications in Java.

• It allows an object(on client side) to invoke methods on


an object running in another JVM (on server side).

• The RMI provides remote communication between the


applications using two objects, namely stub (client side)
and skeleton (server side).

Vu Pham 2
Understanding stub and skeleton
• RMI uses stub and skeleton object for communication
with the remote object.
• A remote object is an object whose method can be
invoked from another JVM.

stub
• The stub is an object, acts as a gateway for the client
side.
• All the outgoing requests are routed through it.
• It resides at the client side and represents the remote
object.

Vu Pham 3
Understanding stub and skeleton
stub
• When the caller invokes method on the stub object, it
does the following tasks:

→ It initiates a connection with remote Virtual Machine


(JVM),
→It writes and transmits (marshals) the parameters to the
remote Virtual Machine (JVM),
→It waits for the result
→It reads (unmarshals) the return value or exception, and
→It finally, returns the value to the caller.
Vu Pham 4
Understanding stub and skeleton
skeleton
• The skeleton is an object, acts as a gateway for the server
side object.
• All the incoming requests are routed through it.

• When the skeleton receives the incoming request, it does


the following tasks:
→ It reads the parameter for the remote method
→ It invokes the method on the actual remote object, and
→ It writes and transmits (marshals) the result to the caller.

Vu Pham 5
Understanding stub and skeleton

Vu Pham 6
Architecture of an RMI Application
• In an RMI application, we write two programs, a server
program (resides on the server) and a client program
(resides on the client).

Vu Pham 7
Architecture of an RMI Application
• The following diagram shows the architecture of an RMI
application.

Vu Pham 8
Architecture of an RMI Application

Vu Pham 9
Working of RMI Application
1. When the client makes a call to the remote object, it is
received by the stub which eventually passes this request
to the Remote Reference Layer (RRL). In an RMI
application, we write two programs, a server program
(resides on the server) and a client program (resides on
the client).

2. When the client side RRL receives the request, it


invokes a method called invoke() of the object
remoteRef(). It passes the request to the RRL on the
server side.
Vu Pham 10
Working of RMI Application
3. When RRL on the server side passes the request to the
skeleton (proxy on the server) which finally invokes the
required objects on the
server.

4. The result is passed all the way back to the server.

Vu Pham 11
Understanding stub and skeleton
skeleton
• The skeleton is an object, acts as a gateway for the server
side object.
• All the incoming requests are routed through it.

• When the skeleton receives the incoming request, it does


the following tasks:
→ It reads the parameter for the remote method
→ It invokes the method on the actual remote object, and
→ It writes and transmits (marshals) the result to the caller.

Vu Pham 12
Working of RMI Application - Java RMI
Example
The 6 steps to write the RMI program / (Steps to
Implement Remote Method Invocation in Java)
are as follows:

1. Create the remote interface


2. Provide the implementation of the remote interface
3. Compile the implementation class and create the stub
and skeleton objects using the rmic tool
4. Start the registry service by rmi registry tool
5. Create and start the remote application
6. Create and start the client application
Vu Pham 13
Working of RMI Application - Java RMI
Example
RMI Example
• This example follows all the 6 steps to create and run the
rmi application.
• The client application need only two files, remote
interface and client application. In the rmi application,
both client and server interacts with the remote interface.
The client application invokes methods on the proxy
object, RMI sends the request to the remote JVM.
• The return value is sent back to the proxy object and then
to the client application.

Vu Pham 14
Working of RMI Application - Java RMI
Example
RMI Example

Vu Pham 15
Working of RMI Application - Java RMI
Example
RMI Example

Step 1: Create the Remote Interface


• For creating the remote interface, extend the Remote
interface and declare the RemoteException 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 RemoteException.

Vu Pham 16
Working of RMI Application - Java RMI
Example
RMI Example

Step 1: Create the Remote Interface


import [Link].*;
public interface Adder extends Remote
{
public int add(int x,int y)throws RemoteException;
}

Vu Pham 17
Working of RMI Application - Java RMI
Example
RMI Example

Step 2: Provide the implementation of the remote


interface
For providing the implementation of the Remote interface,
we need to
• Either extend the UnicastRemoteObject class,
• Use the exportObject() method of the
UnicastRemoteObject class
In case, if we extend the UnicastRemoteObject class, we
must define a constructor that declares RemoteException.

Vu Pham 18
Working of RMI Application - Java RMI
Example
RMI Example
Step 2: Provide the implementation of the remote
interface
import [Link].*;
import [Link].*;
public class AdderRemote extends UnicastRemoteObject
implements Adder{
AdderRemote()throws RemoteException{
super();
}
public int add(int x,int y){return x+y;} }

Vu Pham 19
Working of RMI Application - Java RMI
Example
RMI Example
Step 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 AdderRemote

Vu Pham 20
Working of RMI Application - Java RMI
Example
RMI Example

4) Start the registry service by the rmiregistry 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.

rmiregistry 5000

Vu Pham 21
Working of RMI Application - Java RMI
Example
RMI Example

Step 5: Create and run the server application

• Now rmi services need to be hosted in a server process.


The Naming class provides methods to get and store the
remote object.

• The Naming class provides 5 methods namely


lookup(),bind(),unbind(),rebind() and list()

Vu Pham 22
Working of RMI Application - Java RMI
Example
Step 5: Create and run the server application
• In this example, we are binding the remote object by
the name abcd.
import [Link].*;
import [Link].*;
public class MyServer{
public static void main(String args[]){
try{
Adder stub=new AdderRemote();
[Link]("rmi://localhost:5000/abcd",stub);
}catch(Exception e){[Link](e); } } }
Vu Pham 23
Working of RMI Application - Java RMI
Example
RMI Example
Step 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.

Vu Pham 24
Working of RMI Application - Java RMI
Example
RMI Example
Step 6: Create and run the client application
import [Link].*;
public class MyClient{
public static void main(String args[]){
try{
Adder stub=(Adder)[Link]("rmi://localhost:
5000/abcd");
[Link]([Link](34,4));
}catch(Exception e){} } }

Vu Pham 25
THANK YOU

Vu Pham 26

You might also like