Remote Method Invocation in Java

Note:
java.rmi package: Remote Method Invocation (RMI) has been deprecated in Java 9 and later versions, in favor of other remote communication mechanisms like web services or Remote Procedure Calls (RPC).

Remote Method Invocation (RMI) is an API that allows an object to invoke a method on an object that exists in another address space, which could be on the same machine or on a remote machine. Through RMI, an object running in a JVM present on a computer (Client-side) can invoke methods on an object present in another JVM (Server-side). RMI creates a public remote server object that enables client and server-side communications through simple method calls on the server object.

Stub Object: The stub object on the client machine builds an information block and sends this information to the server.

The block consists of

Skeleton Object: The skeleton object passes the request from the stub object to the remote object. It performs the following tasks

Working of RMI

The communication between client and server is handled by using two intermediate objects: Stub object (on client side) and Skeleton object (on server-side) as also can be depicted from below media as follows:

These are the steps to be followed sequentially to implement Interface as defined below as follows:

  1. Defining a remote interface
  2. Implementing the remote interface
  3. Creating Stub and Skeleton objects from the implementation class using rmic (RMI compiler)
  4. Start the rmiregistry
  5. Create and execute the server application program
  6. Create and execute the client application program.

Step 1: Defining the remote interface

The first thing to do is to create an interface that will provide the description of the methods that can be invoked by remote clients. This interface should extend the Remote interface and the method prototype within the interface should throw the RemoteException.

Example: