Chapter 3
Remote Method Invocation
(RMI)
RMI
• The RMI (Remote Method Invocation) is an API that provides a mechanism to create
distributed application in java.
• If any application performs the below tasks then it is a distributed application.
• The application need to locate the remote method
• It need to provide the communication with the remote objects, and
• The application need to load the class definitions for the objects.
• The RMI allows an object to invoke methods on an object running in another JVM.
• The RMI provides remote communication between the applications using two
objects stub and skeleton.
Architecture of RMI
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.
• 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
• It finally, returns the value to the caller.
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
• It writes and transmits (marshals) the result to the caller.
RMI Steps
• Create the remote interface
• Provide the implementation of the remote interface
• Compile the implementation class and create the stub and skeleton
objects using the rmic tool
• Start the registry service by rmiregistry tool
• Create and start the remote application
• Create and start the client application
Example
• Define the remote interface
• Develop the implementation class (remote object)
• Develop the server program
• Develop the client program
• Compile the application
• Execute the application
1. Create remote interface
Extend the Remote interface and declare the RemoteException
import [Link].*;
public interface Adder extends Remote
{
public int add(int x, int y) throws RemoteException
}
Here, interface name is Adder and the method name is add().
2. Create the implementation class (Remote
object)
• Write an implementation class separately or can directly make the
server program implement this interface.
3. Create, define and run server application
• Create a client class from where to invoke the remote object.
• Create a remote object
• Get the RMI registry using the getRegistry() method of the
LocateRegistry class which belongs to the package [Link].
• Bind the remote object created to the registry using the Rebind()
method of the class name Registry.
4. Create, define, run the client application
• Create a client class
• Get the RMI registry using the getRegistry() method of the
LocateRegistry class which belongs to the package [Link].
• Fetch the object from the registry using the method lookup() of the
class Registry which belongs to the package [Link]
• Invoke the required method using the obtained remote object.
Example
import [Link].*; [Link]
public interface Adder extends Remote
{
public int add(int x, int y) throws RemoteException;
}
import [Link].*;
import [Link].*;
import [Link].*;
public class Client {
public static void main(String[] args) throws RemoteException {
Client c=new Client();
[Link]();
}
private void connectRemote() throws RemoteException {
try {
Scanner sc=new Scanner ([Link]);
Registry reg=[Link]("localhost",9999); [Link]
Adder add=(Adder)[Link]("Hi Server");
[Link]("Enter two numbers");
int a=[Link]();
int b=[Link]();
[Link]("Addition is = "+ [Link](a,b));
}
catch(NotBoundException|RemoteException e)
{
[Link]("Exception"+ e);
}
}
}
import [Link].*;
import [Link].*;
import [Link].*;
public class Server extends UnicastRemoteObject implements Adder {
public Server() throws RemoteException {
}
public int add(int n1, int n2) throws RemoteException {
return n1+n2;
}
public static void main (String arg[]) throws RemoteException
{
try {
Registry reg=[Link](9999);
[Link]("Hi Server", new Server()); [Link]
[Link]("Server is ready");
}
catch(RemoteException e)
{
[Link]("Exception" + e);
}
}
}
import [Link];
import [Link];
public interface RMIInterface extends Remote [Link]
{
public String helloTo(String name) throws RemoteException;
}
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ClientOperation {
private static RMIInterface look_up; [Link]
public static void main(String[] args)
throws MalformedURLException, RemoteException, NotBoundException {
Registry Naming=[Link]("localhost",1888);
look_up = (RMIInterface) [Link]("MyServer");
String txt = [Link]("What is your name?");
String response = look_up.helloTo(txt);
[Link](null, response);
}
}
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ServerOperation extends UnicastRemoteObject implements RMIInterface{
private static final long serialVersionUID = 1L;
protected ServerOperation() throws RemoteException {
super();
}
@Override
public String helloTo(String name) throws RemoteException {
[Link](name + " is trying to contact!");
return "Server says hello to " + name; [Link]
}
public static void main(String[] args) {
try {
Registry Naming = [Link](1888);
[Link]("MyServer", new ServerOperation());
[Link]("Server ready");
} catch (Exception e) {
[Link]("Server exception: " + [Link]());
[Link]();
}
}
}
Tutorial
• Define RMI and RPC
• Describe the design implementation of java RMI?
• List the methods in servlet.
• Explain request-reply protocols