What is Remote Method Invocation (RMI)?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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:
- 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.
- Remote Reference Layer: This layer manages references made from clients to the server's remote objects.
- 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
Remote Object Implementation
Server Program
Client Program
Summary
| Component | Description |
| Remote Interface | Defines the methods that can be invoked remotely. |
| Remote Object | Implements the remote interface and its methods. |
| Client | Looks up the remote object and calls its methods. |
| RMI Registry | Helps in locating the remote objects. |
| Stub | Acts 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.

