Remote Method Invocation

Hirushka Nirmani
4 min readJul 18, 2020

Introduction for RMI

RMI support an object to invoke a method that running on another JVM (remote object). In RPC for passing necessary parameters, we used pass by value method. But, when the parameters get complex, RMI support passing object references as well.

Sever application creates an object that is accessible remotely known as remote object. Server registers objects that are available for clients through the “rmiregistry” naming feature within that resides in the jdk and server bind and arbitrary name to the remote object.

In this RMI concept, a remote object is created, and the reference of that object is available for the client. Then the client is simply, tries to invoke method of the remote object in the server. In order to communicate between the client and the skeleton is very important. Therefore, let’s get an idea about the stub and skeleton.

Stub

The stub is the represent of the remote object that acts as a gateway for the client which all the request that sent by the client go through the stub. Stub is responsible for keeping the connection with the remote JVM, marshals the parameters of the client request that needs to be send to the server, unmarshalls the received response from the server and return its output to the application.

Skelton

The skeleton is the represent of the remote object that act as a gateway to the server and all the request that receives, coming through the skeleton object. The skeleton object is responsible for the unmarshalls the client request and read it parameters, then it invokes the correct method by looking into the request and marshals the result.

RMI Components

RMI Registry

Each remote object needs to register their location through the RMI registry as I mention earlier. RMI clients find the remote object through look up services.

Server — hosting remote object

It construct an implementation of the object and provides access to the methods through the skeleton as we discussed.

Client — Using a remote object

Client asks the registry for the location of the object and send all the request through the stub.

Explanation of HelloWorld Project

this HelloWorld Project that we are going to discuss is contains:

1. Create HelloWorld remote service interface

2. Implement the HelloWorld remote service interface

3. Create RMI Server

4. Create RMI Client

Now we are going to talk about one by one from the above list.

  1. Create HelloWorld remote service interface

In order to create the remote interface, we have to extend it from the Remote interface and each and every method should throw with Remote Exception.

2. Implementation of the HelloWorld remote service interface

Now we are going to do the implementation for the HelloWorld Interface. First, we have to extend this HelloWorldImpl class with the UnicastRemoteObject interface. After adding the constructor, it also should throws with the Remote Exception. By extending it with the UniCastRemoteObject, we can use point to point remote communication, RMI’s default socket based transport for communication.

Now we have to create the stub and the skeleton object using the rmi compiler.

“rmic HelloWorld”

By using rmicregistry tools, then we have to start the registry service.

“rmiregistry 5000” (here 5000 is the port number of the service)

3. Create RMI Server

We are using Naming.rebind method to bind the object to a new name (in the example it is hwrmi). We can run the server by using “java HelloWorldServer” command.

4. Create RMI Client

As we discussed, in client object, we are trying to get the stub object by using the Naming.lookup method which returns a reference of the remote object. We are passing the binding name that we used to bind the object before (in this case it is hwrmi). Finally, run the client application and test the output.

In this example project we are getting the passing the name from the client and accepting a greeting from the server. As well as we are trying to capture the request sending time of the client and the reply time of the server as well.

You can view the code for my example project that we discussed -> https://github.com/hirushka/RMI-example.git

--

--