RMI (Remote Method Invocation)

Suthesana
3 min readJul 18, 2020

RMI stands for Remote Method Invocation.It is an API that provides a mechanism to create distributed application in java. The RMI allows an object residing in one system (JVM) to invoke methods on an object running in another JVM.

RMI provides remote communication between the applications using two objects stub and skeleton.

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.

skeleton

The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are routed through it.

Java RMI Example

Steps to write the RMI program.

  1. Create the remote interface

For creating the remote interface, expand the Remote interface and declare the RemoteException with all the methods for the remote interface. Here, we are creating a remote interface that extends the Remote interface. There is just a one method named add() and it declares RemoteException.

2.Provide the implementation of the remote interface

For providing the implementation of the Remote interface, we have to

i.extend the UnicastRemoteObject class

or

ii. use the exportObject() method of the UnicastRemoteObject class

3.Compile the implementation class and create the stub and skeleton objects using the rmic tool

Next step is to make stub and skeleton objects using the rmi compiler. The rmic tool invokes the RMI compiler and makes stub and skeleton objects.

4.Start the registry service by rmiregistry tool

Now start the registry service by using the rmiregistry tool. If we don’t specify the port number, it uses a default port number. For this example port number 5000 is used

rmiregistry 5000

5.Create and start the remote application

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

Here we are binding the remote object by the name sonoo.

6.Create and start the client application

The output:

--

--