Remote Method Invocation
In my last blog I wrote about RPC. This is about Remote Method Invocation, shortly called RMI which an API we use to create distributed systems in java invoking methods of an object (remote object) which is running on another Java Virtual Machine (JVM). This could be on same machine or on a remote machine. Simply RMI is the Java version of what we known as a remote procedure call (RPC), but it has the ability to pass one or more objects along with the request.
What really happens?
- Inside the server program, a remote object is created and reference of that object is made available for the client (using the registry).
- The client program requests the remote objects on the server and tries to invoke its methods.
RMI uses stub and skeleton object for communication with the remote object.
Stub is the gateway for client side objects. Requests from the client side to the server side are sent through the stub in the client side. This builds an information block inside the client and send it to the server side.
This information block contains following elements.
- An identifier of the remote object to be used
- Method name which is to be invoked
- Parameters to the remote JVM
what stub does?
- First it initiate the connection with the remote JVM which it needs to communicate.
- Then it marshalls the message that contains parameters and send it to the remote JVM.
- It waits until the remote JVM send the result.
- After receiving the result stub unmarshalls it and send it to the caller.
This is same as the stub, but it works in the server side. So the Skeleton is the gateway for server side objects.
- Receive the requests from client.
- Then reads the parameters sent to invoke the relevant method.
- After getting a result then the result is sent to the caller after marshalling.
By using RMI we can obtain type safety and minimize the complexities of applications.
The remote interface
A remote interface provides the description of all the methods of a particular remote object. The client communicates with this remote interface.