Remote Method Invocation
RMI
Java
Networking
Distributed Systems

What is Remote Method Invocation (RMI)?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Remote Method Invocation (RMI) is a Java API that allows an object located in one Java Virtual Machine (JVM) to access/invoke methods on an object located in another JVM. RMI is used primarily for building distributed applications, where the applications are spread across different networks or systems. By using RMI, Java code can execute across multiple environments, either on the same machine or over a network, without needing to rewrite the entire codebase.

How RMI Works

RMI works by creating a client-server relationship between the machine hosting the remote object (server) and the machine that seeks to invoke methods on the remote object (client). Here’s a basic outline of the process:

  1. Stub and Skeleton Layer: This layer sits between the application and the network. Stubs on the client side act as a proxy for the remote object and handle the network communications. Skeletons used to exist on the server side to handle communication between the stub and the actual object on the server but are no longer required from Java 2 platform (JDK 1.2) onwards.
  2. Remote Reference Layer: This layer manages references made from clients to the server's remote objects.
  3. Transport Layer: This layer handles the actual data transmission across the network.

Detailed Steps Involved in an RMI Call:

  • The remote interface is defined and must extend java.rmi.Remote.
  • The remote object implements this interface and provides concrete definitions of the remote methods declared in the interface.
  • The server registers one or more objects with the RMI registry (or another naming service) for discovery by clients.
  • The client looks up the remote object in the RMI registry and invokes methods using the stub object.

RMI Components

Key components in an RMI application include:

  • Remote Interface: Specifies the methods of a remote object.
  • Remote Object Implementation: The implementation of the remote interface.
  • Client Program: Accesses the implementation of the remote object.
  • RMI Registry: A simple lookup service that allows clients to find remote objects.

Advantages of Using RMI

  • Ease of Use: RMI allows the programmer to design and implement distributed applications, as it abstracts much of the complexity of remote communications.
  • Security: Java's built-in security model extends to RMI, helping to protect systems from unauthorized access.
  • Transparency: RMI makes interaction with remote objects as simple as invoking methods on local objects.
  • Portability: Programs written in Java are portable across many operating systems and hardware configurations.

Example of Remote Method Invocation

Here's a simple example of an RMI application:

Remote Interface

java
1import java.rmi.Remote;
2import java.rmi.RemoteException;
3
4public interface Calculator extends Remote {
5    double add(double a, double b) throws RemoteException;
6}

Remote Object Implementation

java
1import java.rmi.server.UnicastRemoteObject;
2import java.rmi.RemoteException;
3
4public class CalculatorImpl extends UnicastRemoteObject implements Calculator {
5    public CalculatorImpl() throws RemoteException {
6        super();
7    }
8
9    public double add(double a, double b) throws RemoteException {
10        return a + b;
11    }
12}

Server Program

java
1import java.rmi.registry.LocateRegistry;
2import java.rmi.registry.Registry;
3
4public class CalculatorServer {
5    public static void main(String[] args) {
6        try {
7            CalculatorImpl calc = new CalculatorImpl();
8            Registry registry = LocateRegistry.createRegistry(1099);
9            registry.rebind("CalculatorService", calc);
10            System.out.println("Calculator Service is ready.");
11        } catch (Exception e) {
12            System.err.println("Server exception: " + e.toString());
13            e.printStackTrace();
14        }
15    }
16}

Client Program

java
1import java.rmi.registry.LocateRegistry;
2import java.rmi.registry.Registry;
3
4public class CalculatorClient {
5    public static void main(String[] args) {
6        try {
7            Registry registry = LocateRegistry.getRegistry("localhost");
8            Calculator stub = (Calculator) registry.lookup("CalculatorService");
9            double response = stub.add(5.0, 3.5);
10            System.out.println("Result: " + response);
11        } catch (Exception e) {
12            System.err.println("Client exception: " + e.toString());
13            e.printStackTrace();
14        }
15    }
16}

Summary

ComponentDescription
Remote InterfaceDefines the methods that can be invoked remotely.
Remote ObjectImplements the remote interface and its methods.
ClientLooks up the remote object and calls its methods.
RMI RegistryHelps in locating the remote objects.
StubActs as a proxy for the remote object at the client.
Skeleton (Deprecated)Facilitated communication in earlier versions of Java.

RMI continues to be a strong choice for developers needing robust, scalable, and flexible distributed systems. This approach fits nicely into the Java ecosystem, leveraging object-orientation and built-in security features.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.