Remote Method Invocation (RMI)
Introduction
RMI is a Java API that allows an object residing in one JVM to
invoke methods on an object residing in another JVM.
Basically, it is used as a medium of communication between
two different JVMs
It also enables Java applications to communicate over a
network.
Stub
• Stub: Client proxy that forwards method calls to the
remote object.
• It initialize a connection with remote JVM.
• It writes and transmits the parameters
• Wait for result.
• Read the return value and exception.
• Finally, returns the value to the sever.
Skeleton
• Skeleton: Server Dispatcher handling calls from stub.
• Reads the parameters for remote method.
• Invokes the method on actual remote object.
• Writes and transmits the result to the caller.
Layers of RMI
RMI consists of four main layers:
I. Application Layer: Where the client and server object
reside.
II. Stub and Skeleton Layer: Handles communication between
client and server.
III. Remote Reference Layer (RRL): Manages invocations
between JVMs.
IV. Transport Layer: Manages the transmission of methods.
(Uses TCP/IP protocol)
RMI Architecture
Application
Layer
Stubs &
Skeleton Layer
Remote
Reference Layer
Transport Layer
How RMI is Implemented?
1. Design the interface for the service.
2. Implement the methods specified in the interface.
3. Generate the stub and the skeleton. (via rmic)
4. Register the service by name and location.
5. Use the service in an application.
RMI Communication Process
rmiregistry 4.
(finds object by name) rmic (stores object by name)
5. 2.
3. 3.
Client Stub Skeleton Implemen
tation
(uses) 1.
(implements)
Interface
Client Host Server Host
Implementing RMI in Java
1. Defining Remote Interface:
• It must have java.rmi.Remote and throw RemoteException.
• This exception handles the Network issues.
Hello.java
Implementing RMI in Java
2. Creating Server:
• In the server, we bind object to the RMI registry, making it available for the
clients.
HelloImpl.java
Implementing RMI in Java
3. Creating the Client:
• The client looks up the remote object in the RMI registry and invokes its
methods.
RMIClient.java
And, that is how RMI can be implemented in JAVA
Conclusion
• RMI provides Java-to-Java communication.
• RMI supports various security features.
• RMI enables distributed computing in JAVA.
• Can be used in Cloud-based Systems or Microservices.
Thanks