Remote method invocation is an API. It allows an object to invoke a method on an object that located in another address space. It could be on same computer or a remote computer. RMI is used to build distributed applications. It provides remote communication between two programs. In RMI application, there will be two programs; server program and client program.
Inside server program, remote object is created and reference of that object is made available for client. Client program requests remote objects on server and tries to invoke its method.
Architecture of RMI application
Transport layer connects client and server. It manages the existing connection and also sets new connections.
Stub is representation of remote object at client. It exists on client system; it acts as a gateway for client program. All outgoing requests are routed through it. When caller invokes method on the stub object following tasks will be done.
- Initialize connection with remote virtual machine
- Writes and transmits the parameters to remote virtual machine
- Waits for result
- Reads return value or exception
- Finally returns value to caller
Skeleton is object which exists on server side. Stub communicates with skeleton to pass request to the remote object. When skeleton receives incoming request, following tasks will be done.
- Reads parameter for remote method
- Invokes method on the actual remote object
- Writes and transmits the result to caller
RRL manages the references made by the client to the remote object.
Work fellow in RMI application
- When client makes call to remote object, it is received by the stub which eventually passes this request to RRL.
- When client side RRL receives request, it invokes a method called invoke() of the object remoteRef. It passes request to RRL on the server side.
- RRL on server side passes request to skeleton which finally invokes the required object on the server.
- Result is passed all away back to client.
Goals of RMI are Minimize complexity of application, Preserve type safety, Distributed garbage collection and Minimize difference between working with local and remote objects.
Lets look a simple example of Java RMI example.
create the remote interface
Provide the implementation of the remote interface
Create the stub and skeleton objects using the rmic tool
Start the registry service by the rmiregistry tool
Create and run the server application
Create and run the client application