RMI In Java

Java RMI Projects



RMI provides for remote communication between programs written in the Java programming language. Java Remote method invocation (RMI)application programming interface (API) enables client and server communications over the internet. Typically client program request to a server program and the server program responds to those requests.

A common example is sharing a word processing program over a network.The Java Remote Method Invocation(RMI) system allows an object running in one Java virtual machine to invoke methods on an object running in another Java virtual machine. It's like placing a class on Machine A and calling methods of that class from Machine B as though they were from the same machine. RMI provides the Way by which the server and the client communicate with each other and pass information back and forth. This type of application is sometimes referred to as a distributed object application

Advantages of RMI

  1. Simple and clean to implement which to more robust, maintainable and flexible applications.
  2. Distributed systems creations are required during decoupling the client and server objects simultaneously.
  3. It is possible to create zero-install client for the users.
  4. No client installation is needed except java capable browsers.
  5. At the time of changing the database, the server objects are needed to be recompiled but server interface and the client remain the same.

Architecture of RMI

RMI is based on client-server architecture model. The stub plays the role of a proxy server for the remote objects. The skeleton lives in the same JVM as the remote object and communication will be handled with the stub. The remote references are managed by the registry. The binding of server with reference to itself will be on the registry. The clients communicate with a registry which in turn obtains a remote reference to the server. This could be a remote host. The remote reference could be obtained by the client from the registry in order to invoke the methods from the remote object.

How RMI works

The RMI implementation is essentially built from three abstraction layers

  1. The Stub/Skeleton Layer: Toget location transparency, RMI has introduces two kinds of objects known as stubs and skeletonsthat serve as an interface between an application and rest of the RMI system. Stubs are present at the client side, whereas skeletons are found on the server side .The purpose of this layer is to transfer the data to the Remote Reference layer. through marshaling and unmarshaling .marshaling is the process of converting data and object into byte stream while unmarshaling is reverse of it .umarshaling convert the stream byte data to the data and object. readable by the client.
  2. The Remote Reference Layer: The Remote Reference Layer The remote reference layer defines and supports the invocation semantics of the RMI connection. This layer maintains the session during the method call.
  3. The Transport Layer: Transport layer is used to make the stream-based network connections over TCP/IP protocol between the JVMs, and is responsible for setting and managing the connections. Even if two JVMs are running on the same physical computer, they connect through their host computers TCP/IP network protocol stack. RMI uses a protocol called JRMP (Java Remote Method Protocol) on top of TCP/IP (an analogy is HTTP over TCP/IP).

Sequence Of Events Performed By The Stub

  1. Initially set a connection with the remote JVM which is containing the remote object.
  2. Marshals the parameters to the remote JVM. i.e (writes and transmits)
  3. Waits for the result of the method invocation.
  4. Unmarshals the return value or exception returned.
  5. Return the value to the caller.

Sequence Of Events Performed By The Skeleton

  1. Unmarshals the parameters for the remote method the same parameters those were marshaled by the stub on the client side.
  2. Invokes those method on the real remote object implementation.
  3. Marshals (writes and transmits) the result (return value or exception) to the caller (which is then unmarshalled by the stub).

Steps to create a RMI system

  1. Create an interface.
  2. Create a class that implements the interface.
  3. Create a server that creates an instance of this class.
  4. Create a client that connects to the server object using Naming.lookup().
  5. Compile these classes.
  6. Run the RMI interface compiler on the .class file of the implementation class .
  7. Start the RMI registry as "start rmiregistry".
  8. Start the server class as "start java myRMIServer".
  9. Run the client program as "java myRMIClient".

Create an Interface

public interface myRMIInterface extends java.rmi.Remote
public java.util.Date getDate() 
throws java.rmi.RemoteException;
//RMI implementation
import java.rmi.*;
import java.rmi.server.
public class myRMIImpl 
extends UnicastRemoteObject implements
public myRMIImpl
(String name) throws
Naming.rebind(name, this);
catch(Exception e)
System out.println("Exception occurred: " + e);
public java.util.
Date getDate()
return new(); 

RMI Server Side Program

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

public class myRMIServer
public static void main(String[] argv)
System.setSecurityManager(new RMISecurityManager());
myRMIImpl implementation = new 
catch (Exception e) 
System.out.println("Exception occurred: " + e);

RMI Client Side Program

String serverName=argv[0];



How compile and run the RMI

  1. Start the rmiregistry

    To start the registry, Windows users should do the following start rmiregistry

    To start the registry, Unix users should do the following:- rmiregistry &

  2. Compile the server

    Compile the server, and use the rmic tool to create stub files.

  3. Start the server

    From the directory in which the classes are located, type the following:- java myRMIServer

  4. Start the client

    We can run the client on local machine , or from a different machine at difrent location. In either case, we'll need to specify the hostname of the machine wherewe are running the server. If we're running it locally, than we use localhost as the hostname.

    java myRMIClient localhost

Java RMI is a useful mechanism for invoking methods of remote objects. Java RMI allows one Java Virtual Machine to invoke methods of another, and to share any Java object type, even if client or server has never come across that object type before.