Remote Method Invocation(RMI)

Basically RMI is a method for accessing / invoking an object operating on another JVM by an object residing in one JVM. RMI is used to develop distributed applications; provides Java programs with remote communication using stub and skeleton. In RMI application we have to write two programs server program which is resides on the server and the client program which is resides on the client. Server program’s main functionality is to create a remote object and the reference of that object is made available to the client using registry. Client program’s main functionality is to request the remote objects on the server and invokes its methods.

RMI Architecture

A stub is a representation of the remote object which is resides in the client system. It acts as a gateway for the client program. All the outgoing requests are routed through stub.

Skeleton is the server-side objects that resides at the server side. Stub communicates to the remote object with that skeleton to pass request. All the incoming requests are routed through skeleton.

RMI registry is a namespace that is used to place all server objects in. Each time an object is created by the server, it registers that object with the RMIregistry (using methods bind) (or reBind). They are registered by means of a special name known as bind name.

To invoke a remote object, a reference for that object is provided by the client. The client then obtains the object from the registry using its bind name.

RMI process

Now let’s move on to how to create a RMI application to the basic calculations on a remote server.

  1. First we have to create the remote interface.
Calculator.java

2. Implementation of the Remote Interface.

AdderRemote.java

Here Remote Interface Implementation is doing by extending the UnicastRemoteObject class.

3. Create stub and skeleton objects.

stub and skeleton objects are created by using RMI compiler. The rmic tool invokes the RMI compiler and generates artifacts such as stub and skeleton.

4. Start the registry service.

Starting the registry service by using rmiregistry tool in default port 5000.

5. Create and run the server application.

Server.java

6. Create and run the client application.

Client.java
Output

So the basic concept of the RMI is invoking methods in between two jvms using calls. In distributed and cloud computing this concept is heavily used.

In this post, I have demonstrated basic RMI concepts and how to create a sample Calculator application using RMI.

Full Stack Developer | BSc. (Hons.) in Software Engineering | University of Kelaniya, Sri Lanka