You are on page 1of 19

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 java.rmi.*;
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 java.rmi.registry.
• 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 java.rmi.registry.
• Fetch the object from the registry using the method lookup() of the
class Registry which belongs to the package java.rmi.registry
• Invoke the required method using the obtained remote object.
Example
import java.rmi.*; Adder.java
public interface Adder extends Remote
{
public int add(int x, int y) throws RemoteException;
}
import java.rmi.*;
import java.rmi.registry.*;
import java.util.*;
public class Client {
public static void main(String[] args) throws RemoteException {
Client c=new Client();
c.connectRemote();
}
private void connectRemote() throws RemoteException {
try {
Scanner sc=new Scanner (System.in);
Registry reg=LocateRegistry.getRegistry("localhost",9999); Client.java
Adder add=(Adder)reg.lookup("Hi Server");
System.out.println("Enter two numbers");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("Addition is = "+ add.add(a,b));
}
catch(NotBoundException|RemoteException e)
{
System.out.println("Exception"+ e);
}
}
}
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
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=LocateRegistry.createRegistry(9999);
reg.rebind("Hi Server", new Server()); Server.java
System.out.println("Server is ready");
}
catch(RemoteException e)
{
System.out.println("Exception" + e);
}
}
}
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RMIInterface extends Remote RMIInterface.java
{
public String helloTo(String name) throws RemoteException;
}
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import javax.swing.JOptionPane;
public class ClientOperation {
private static RMIInterface look_up; ClientOperation.java
public static void main(String[] args)
throws MalformedURLException, RemoteException, NotBoundException {
Registry Naming=LocateRegistry.getRegistry("localhost",1888);
look_up = (RMIInterface) Naming.lookup("MyServer");
String txt = JOptionPane.showInputDialog("What is your name?");
String response = look_up.helloTo(txt);
JOptionPane.showMessageDialog(null, response);
}
}
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
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 {
System.err.println(name + " is trying to contact!");
return "Server says hello to " + name; ServerOperation.java
}
public static void main(String[] args) {
try {
Registry Naming = LocateRegistry.createRegistry(1888);
Naming.rebind("MyServer", new ServerOperation());
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Tutorial
• Define RMI and RPC
• Describe the design implementation of java RMI?
• List the methods in servlet.
• Explain request-reply protocols

You might also like