Remote Method Invocation-RMI

suji guna
3 min readJul 18, 2020

RMI stands for Remote Method Invocation. it’s a mechanism that permits an object residing in one system (JVM) to access/invoke an object running on another JVM.

RMI is employed to create distributed applications; it provides remote communication between Java programs. it’s provided within the package java.rmi.

RMI Architecture

In an RMI application, we have to write 2 programs, a server program and a client program .

  • Inside the server program, a remote object is created and reference of that object is made available for the client (using the registry).
  • The client program requests the remote objects on the server and tries to invoke its methods.

The components of the architecture

  • Transport Layer − This layer connects the client and the server. It manages the existing connection and also sets up new connections.
  • Stub − A stub is a representation (proxy) of the remote object at client. It resides in the client system; it acts as a gateway for the client program.
  • Skeleton − This is the object which resides on the server side. stub communicates with this skeleton to pass request to the remote object.
  • RRL(Remote Reference Layer) − It is the layer which manages the references made by the client to the remote object.

Example

There are 6 steps to be written for the RMI program.

  • Creating the remote interface
  • Providing the implementation of the remote interface
  • Compiling the implementation class and creating the stub and skeleton objects using the rmic tool
  • Starting the registry service by rmiregistry tool
  • Creating and starting remote application
  • Creating and starting the client application

Step 1:Creating the remote interface

For creating a remote interface, extend the Remote interface and declare the RemoteException with all the methods of the remote interface. And, we are creating a remote interface that extends the Remote interface. there’s just one method named add() and it declares RemoteException.

Step 2: Providing the implementation of the remote interface

To provide the implementation of the Remote interface, we should sollow either one of below,

  • Extend the UnicastRemoteObject class,
  • Use the exportObject() method of the UnicastRemoteObject class

Step 3: Compiling the implementation class and creating the stub and skeleton objects using the rmic tool

Next step is creating stub and skeleton objects using the rmi compiler. The rmic tool invokes RMI compiler and it creates stub and skeleton objects.

  • rmic AdderRemote

Step 4: Starting the registry service by rmiregistry tool

Now we have to start the registry service by using the rmiregistry tool. If we don’t specify the port number, it uses a default one. Here, port number 5000 is used.

  • rmiregistry 5000

Step 5: Creating and starting remote application

Now rmi services need to be hosted in a server process.

Step 6: Creating and starting the client application

The ouputs are:

THANK YOU:)

--

--

suji guna

B.Sc in Software Engineering. University of Kelaniya, SriLanka.