Updated May 22, 2023
Overview of RMI Architecture
In distributed application architecture, it is always a need for communication between two different applications. In Java-based applications, one application communicates with another remote/different application running somewhere else using an RMI architecture mechanism.
RMI stands for Remote Method Invocation. Java provides an API allowing an object residing in one JVM (Java Virtual Machine) to access or invoke an object running on another. The other JVM could be on the same machine or a remote machine. This is an interesting feature because, in real-time applications, it becomes very easy for Java applications to communicate directly with each other without any external communication mechanism. Also, it is always a need for secure communication between applications based on distributed application architecture.
RMI Design
Before we go into detailed architecture, we will understand the basic design of RMI architecture.
- RMI API is provided in the package java.rmi. Let’s introduce two terms for the understanding of RMI design architecture. First is the client, the JVM that will call the remote object; second is the server, the JVM contains the remote object. So, the client will call the server, in this case, on the object for method invocation.
- The server will then return the reference of the object to the client. The catch here is both the objects, i.e., local and remote, will appear as a local object on the server. There will be no differentiation between the two. The syntax of the methods of both objects is also the same. Therefore, the server JVM acts like a normal JVM without knowing of any object, whether it is local or remote.
- The same object can be both a server and a client. The program obtains the remote object’s reference and utilizes it as a local object. The RMI infrastructure is responsible for finding the remote object, intercepting method calls, and processing the remote request remotely. The client invokes methods on the object only after obtaining a reference to a remote object.
RMI Architecture
Below is a diagram of RMI architecture in a simple way. You will find various forms of the same architecture on the internet, but we have a simple one to help explain it better.
Let’s start by connecting the dots from a design perspective with an architecture diagram.
The client and server applications are the respective JVMs of the client machine and server machines. In the RMI application, we write two programs: the client program, which resides on the client, and the server program, which resides on the server machine.
1. Application Layer
This layer is the actual systems, i.e. client and server, which are involved in communication. The Java program on the client side communicates with the Java program on the server side.
2. Stub
We have client objects from the design intro; In RMI architecture, it is known as Stub. It is an object that resides on the client machine and acts as a proxy for the remote object. It is like a gateway for the client program.
The stub has the same methods as a remote object. When the client calls on the stub object, the stub forwards this request to a remote object (Skeleton) via RMI infrastructure, which is then executed on the server.
Stub Performs the following events:
- Initiates connection with remote JVM.
- Writes and transmits (Marshals) parameters to remote JVM.
- Waits for the result.
- Reads (Unmarshalls) the returned result.
- Pass the received result to the caller.
3. Skeleton
The server object, which is located in a server machine, is referred to as the Skeleton. Stub communicates with the server application with the help of an intermediate Skeleton object.
The responsibility of the skeleton object is to send parameters to method implementation and send the return values back to the client.
Skeleton Performs the following events:
- Reads the parameter passed by the client.
- Invokes the method on an actual Remote object.
- Transmit/pass the result to the caller.
4. Stub / Skeleton layer
- The Proxy Layer, also known as the Stub/Skeleton layer, intercepts calls made by the client and redirects them to the remote object. Stub and Skeleton are the proxies for the client and server. The Stub and Skeleton objects are like an interface between an application and the rest of the RMI System.
- This layer aims to transfer data to Remote Reference Layer by Object Serialization. This process of converting data/objects into a byte stream is known as Marshalling, and the reverse is known as Unmarshalling. Marshaling is performed when requesting the object from the server, and Unmarshalling is performed when data/object reference is received from the server.
5. Remote Reference Layer
- The Remote Reference Layer connects the proxy layer to the RMI mechanism. This layer is responsible for communicating and transferring objects between client and server. This layer defines and supports the invocation semantics of the RMI connection.
- The remote Reference Layer maintains the session during the method call. i.e., It manages the references made by the client to the remote server object. This layer is also responsible for handling duplicated objects.
6. Transport Layer
The transport layer is responsible for setting up communication between the two machines. This layer uses standard TCP/IP protocol for connection. This layer performs the actual transportation of data. This layer is part of the Remote Reference Layer.
Conclusion
- The Remote Method Invocation (RMI) is a highly useful API provided in Java that facilitates communication between two separate Java Virtual Machines (JVMs). It allows an object to invoke a method on an object residing in another address space.
- It provides a secure way for applications to communicate with each other. It achieves this functionality using concepts Stub (Client calling object) and Skeleton (Remote object residing on the server).
- RMI is used to build distributed applications. It preserves the type of safety. RMI architecture minimizes the complexity of the application in a distributed architecture.
Recommended Articles
This has been a guide to RMI Architecture. Here we discussed the basic concept, RMI design, and architecture. You can also go through our other suggested articles to learn more –