![]() |
VOOZH | about |
Remote Method Invocation (RMI) in Java is an API that enables an object in one JVM to invoke methods on an object located in another JVM, either on the same machine or a remote system. It supports building distributed applications by allowing seamless client-server communication through method calls.
Stub (Client-side Proxy): It acts as a proxy for the remote object and forwards method calls from the client to the server.
The block consists of
Communication between client and server is handled using a Stub (client-side proxy), while server-side request handling is managed internally by the RMI runtime.
Note: In earlier RMI versions, Skeleton was used on the server side, but it is now deprecated and no longer required.
The following steps demonstrate how to build and run a basic RMI application in Java.
The first thing to do is to create an interface that will provide the description of the methods that can be invoked by remote clients. This interface should extend the Remote interface and the method prototype within the interface should throw the RemoteException.
Example:
The next step is to implement the remote interface. To implement the remote interface, the class should extend to UnicastRemoteObject class of java.rmi package. Also, a default constructor needs to be created to throw the java.rmi.RemoteException from its parent constructor in class.
In modern Java, stub classes are generated dynamically by the JVM, so the rmic tool is not required.
Start the registry service by issuing the following command at the command prompt start rmiregistry
The next step is to create the server application program and execute it on a separate command prompt.
The last step is to create the client application program and execute it on a separate command prompt . The lookup method of the Naming class is used to get the reference of the Stub object.
Note: The above client and server program is executed on the same machine so localhost is used. In order to access the remote object from another machine, localhost is to be replaced with the IP address where the remote object is present.
Search.java , SearchQuery.java , SearchServer.java & ClientRequest.java
Important Observations:
- RMI is a pure java solution to Remote Procedure Calls (RPC) and is used to create the distributed applications in java.
- Stub objects are used on the client side, while server-side communication is handled internally by the RMI runtime.