Remote Method Invocation

Sangeevan Siventhirarajah
3 min readJul 18, 2020
Remote method invocation

Remote method invocation is an API. It allows an object to invoke a method on an object that located in another address space. It could be on same computer or a remote computer. RMI is used to build distributed applications. It provides remote communication between two programs. In RMI application, there will be two programs; server program and client program.

Inside server program, remote object is created and reference of that object is made available for client. Client program requests remote objects on server and tries to invoke its method.

Architecture of RMI application

Architecture of RMI application

Transport layer connects client and server. It manages the existing connection and also sets new connections.

Stub is representation of remote object at client. It exists on client system; it acts as a gateway for client program. All outgoing requests are routed through it. When caller invokes method on the stub object following tasks will be done.

  1. Initialize connection with remote virtual machine
  2. Writes and transmits the parameters to remote virtual machine
  3. Waits for result
  4. Reads return value or exception
  5. Finally returns value to caller

Skeleton is object which exists on server side. Stub communicates with skeleton to pass request to the remote object. When skeleton receives incoming request, following tasks will be done.

  1. Reads parameter for remote method
  2. Invokes method on the actual remote object
  3. Writes and transmits the result to caller

RRL manages the references made by the client to the remote object.

Work fellow in RMI application

  1. When client makes call to remote object, it is received by the stub which eventually passes this request to RRL.
  2. When client side RRL receives request, it invokes a method called invoke() of the object remoteRef. It passes request to RRL on the server side.
  3. RRL on server side passes request to skeleton which finally invokes the required object on the server.
  4. Result is passed all away back to client.

Goals of RMI are Minimize complexity of application, Preserve type safety, Distributed garbage collection and Minimize difference between working with local and remote objects.

Lets look a simple example of Java RMI example.

create the remote interface

Provide the implementation of the remote interface

Create the stub and skeleton objects using the rmic tool

rmic AdderRemote

Start the registry service by the rmiregistry tool

rmiregistry 5000

Create and run the server application

Create and run the client application

Output

38

--

--