Remote Method Invocation

Yasiru Kavishka
5 min readJul 18, 2020

--

Introduction for Remote Method Invocation

As we discussed about the RPC (Remote Procedure Calls) in previous article, both RPC and RMI is for facilitate the communication between the client and the server (direct communication).

Simply, Remote Method Invocation is to allow an object to invoke an object that runs in a different JVM. RMI is only for java and it is object oriented. In RPC we used to pass parameters/ values using pass by value method, with the parameters getting complex from the type the requirement of passing parameters with reference is occurs. This requirement can be fulfilled with RMI.

The communication in RMI is mainly focused with the remote object which is created by the server. 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. After the object is created and available for the client, it makes attempts to invoke methods.

Component of Remote Method Invocation

RMI Registry

Every remote object needs to be registered based on their location through the RMI registry an RMI clients use look up services for finding the remote object.

Server — hosting remote object

It constructs an implementation of the object and provides access to the methods through the skeleton.

Client — Using a remote object

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

Stub

The stub is the represent of the remote object that acts as a gateway for the client. All the requests that sent by the client go through the stub and keeps the connection with the server. It marshals the parameters of the client requests, unmarshalls the received response from the server and give the result 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.

Example project

This project has two part client and server programs. Client application has the registry, gets the remote reference form the remote object and uses that reference to invoke the remote method. Client passes two numbers with its request and server will do some simple operation and choose the bigger number in value and send back to the client, so that client can receive the response and give it to user.

The image given below shows the project structure of the example project. Now we are going to discuss the example project class by class. This project contains 4 parts mainly.

1. MaxSelector interface

2. MaxSelectorImpl class (implementation of the MaxSelector interface)

3. RmiClient

4. RmiServer

Project files
  1. MaxSelector Interface

Remote interface should be declared as public since the client tries to load the remote object that implements the remote interface. The interface should extend with Remote interface and each method should be declared with the remote exception.

2. MaxSelectornImpl (the implementation of the MaxSelector interface)

First of all we have to extends with the UniCastRemoteObject and then implement with its interface. By extending it with the UniCastRemoteObject, we can use point to point remote communication, RMI’s default socket-based transport for communication. After adding its constructor and implementing the method, we have focus on creating the stub and the skeleton object using the RMI complier.

3. RmiServer class

In this class we hate use the Naming.rebind method for binding the object to a new name which is max according to the example.

4. RmiClient class

In the client we are using Naming.lookup method for getting the stub object and it returns a reference of a remote object with the name that we used to bind the object. Now we have the ability to access the method to choose the max number with the use of stub object.

To run the project and to get an output we have to compile all the files and create the stub and the skeleton object by running the rmi compiler command.

Then, start the registry service by running the command rmiregistry. in the example I have used port 5050.

Execute the java RmiServer.

Finally, execute the RmiClient and get the reuired output.

Output

For more information download the example project from herehttps://github.com/Yasiru-kavishka/Java-RMI

--

--

Yasiru Kavishka

Software Engineering Undergraduate at University of Kelaniya