RMI in Java

Allows a java object that executes on one machine to invoke a method of java object executes on another machine.
It allows us to build distributed applications.
  • RMI gives java programmers the ability to distribute computing over the network.
  • RMI provides a Remote interface for creating remote objects.
  • A client can invoke the methods of a remote object.
  • The RMI API provides classes and methods to handle all the underlying communication for accessing remote methods
  • RMI also handles serialization of objects that r passed as arguments to method of remote objects. 

RMI consists of 3 layers:
  • The stub/skeleton layer 
  • The Remote Reference Layer
  • Transport Layer
Stub/Skeleton Layer:
  • Stub is the client side proxy representing the remote object. 
  • It defines all the interfaces supported by the remote object implementation.
  • This is a local object on the client side  and also maintains a connection to the server-side object. 
  • Stub interfaces with the client side RRL 
  • Simply, RMI stub is a java object that resides on the client machine. 
  • Remote method calls initiated by the client are actually directed to the stub. 
  • An object passed as an argument to a remote method call must be serialized and sent to the server machine. This is done by stub. 
  • A Skeleton is the java object resides on the server machine .
  • Skeleton is the server-side construct that interfaces with the server side RRL 
  • It receives method invocation request from the client side RRL. 
  • It perform deserialization , and invoke the appropriate code on the server.                       
RMI procedure   
  • Define an Interface that extends java.rmi.Remote interface 
  • Define a class that implements this interface.This class will acts as the remote object’s class.
  • Generate the stub and skeleton classes that are needed for the remote implementation using rmic program.
  • Create a client program that will make the RMI calls to the server.
  • Start the rmiregistry and run remote server and client.
  RMI components


  • Remote Interface
  • RMI Server
  • RMI Client
 Remote Interface:
The Remote interface must have the following properties:
The interface must be public.
The interface must extend the interface java.rmi.Remote.
Every method in the interface must declare that it throws java.rmi.RemoteException. Other exceptions may also be thrown.

// an interface for remote classes.
import java.rmi.*;
interface Interf extends Remote
{
public String disp(String s)throws RemoteException;
}



RMI Server class

The Remote class itself has the following properties:
It must implement a Remote interface.
It should extend the java.rmi.server.UnicastRemoteObject class. Objects of such a class exist in the address space of the server and can be invoked remotely.
It can have methods that are not in its Remote interface. These can only be invoked locally.

//2 implement the interface in a server side appln

import java.rmi.*;
import java.io.*;
import java.rmi.server.*;

public class Serv extends UnicastRemoteObject implements Interf
{
public Serv() throws RemoteException
{}
public String disp(String s)throws RemoteException
{
System.out.println(s);
String s1="";
DataInputStream dis=new DataInputStream(System.in);
try
{s1=new String();
System.out.println("enter message");
s1=dis.readLine();
}
catch(Exception e)
{
System.out.println(e);
}
return s1;
}

public static void main(String str[])
{
try
{
Serv serv=new Serv(); //Instantiates the server object
Naming.rebind("ref",serv); // Binding objects to a Registry Service
}
catch(Exception e)
{
}
}
}

    
RMI client :

  • The Client itself is just a Java program. It need not be part of a Remote or Serializable class, although it will use Remote and Serializable classes.
  • The name of a remote object includes the following information:
  • The Internet name (or address) of the machine that is running the Object Registry with which the remote object is being registered.
  • If the Object Registry is running on the same machine as the one that is making the request, then the name of the machine can be omitted.
  • The port to which the Object Registry is listening. If the Object Registry is listening to the default port, 1099, then this does not have to be included in the name.
  • The local name of the remote object within the Object Registry.
import java.io.*;
import java.rmi.*;

public class Client
{
public static void main(String str[])
{
String s1="",s2="";
try
{
Interf in=(Interf)Naming.lookup("ref");
//Interf in=(Interf)Naming.lookup(“rmi://127.0.0.1/ref”);
DataInputStream dis=new DataInputStream(System.in);
while(true)
{s1=new String();
s2=new String();
System.out.println("Enter msg");
s1=dis.readLine();
s2=in.disp(s1);
System.out.println(s2);
}
}

catch(Exception e)
{
System.out.println(e);
}
}
}
 



Steps to run your RMI program
  • Compile the Interface
  •               here javac Interf.java
  • Compile the RMIServer class
  •         Here javac Serv.java
  • Compile the Client class
  •         Here javac Client.java
  • Compile the server class using rmic compiler which produces the skeleton and stub classes.  rmic Serv
  • Start the rmiregistry
  • To start the registry, Windows users should do the following (assuming that your java\bin directory is in the current path):-
  • start rmiregistry to start the registry or simply rmiregistry
  • Start the server
  • From the directory in which the classes are located, type the following:-
  •       java Serv
  • Start the client
  • You can run the client locally, or from a different machine. In either case, you'll need to specify the hostname of the machine where you are running the server. If you're running it locally, use localhost as the hostname.
  •      java Client