Module 5 - Part 2 - Remote Method Invocation (RMI) Notes
Module 5 - Part 2 - Remote Method Invocation (RMI) Notes
Vu Pham 1
RMI Architecture
• RMI is an API that provides a mechanism to create
distributed applications in Java.
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:
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).
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.
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:
Vu Pham 14
Working of RMI Application - Java RMI
Example
RMI Example
Vu Pham 15
Working of RMI Application - Java RMI
Example
RMI Example
Vu Pham 16
Working of RMI Application - Java RMI
Example
RMI Example
Vu Pham 17
Working of RMI Application - Java RMI
Example
RMI Example
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.
rmic AdderRemote
Vu Pham 20
Working of RMI Application - Java RMI
Example
RMI Example
rmiregistry 5000
Vu Pham 21
Working of RMI Application - Java RMI
Example
RMI Example
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