Hello World System Using RMI (Remote Method Invocation)
Distributed and Cloud Computing
RMI is a mechanism that allows an object which sides in one system (JVM) to access an object running on another JVM. RMI is used to build distributed applications. It provides remote communication between Java programs. Stub and Skeleton are used remote communication between two objects.
What is Stub and Skeleton?
Stub: A stub is a representation of the remote object at client. It resides in the client system; it acts as a gateway for the client program.
Skeleton: A Skeleton is an object which resides on the server side. Stub communicates with this skeleton to pass request to the remote object.
Let’s see this through a simple application
We have to implement three java files such as RMIInterface.java, ServerOperation.java and ClientOperation.java using a IDE.
This is the file structure that we are going to implement. Ignore the ClientOperation.class and RMIInterface.class and ServerOperation.class because it would generated when compile.
The first thing is we have to design a Remote
Interface. It will implement both Server and Client. The Interface
must always be public and extend Remote
. All methods described in the Remote
interface must list RemoteException
in their throws clause.
We have only one method called helloTo()
RMIInterface.java : Interface
Our Server extends UnicastRemoteObject
and implements the Interface
we made before. In the main method we bind the server on localhost with the name “MyServer”.
All server objects are placed in RMI Registry. It registers this object with the RMIregistry (using bind() or rebind() methods) when the server creates an object each time. These are registered are known as bind name.
ServerOperation.java : class
Ok, next we create the Client. The Client uses an RMIInterface
Object that “looks” To invoke a remote object, the client needs a reference of that object to “find” the Server. That time, the client fetches the object from the registry using its bind name (using lookup() method). Server and receive responses could be called with that RMIInterface
.
ClientOperation.java : class
When run the application, we have these steps,
Open the src directory which is located all three java files.
Click on the path and type “cmd” and enter. Then the command prompt is open for the directory.
Then we need to compile each java file.
Javac RMIInterface.java
Javac ServerOperation.java
Javc ClientOperation.java
Then your src directory is shown as below.
Then type start rmiregistry. It comes as separated window as below.
Next open a new command prompt inside the directory and type java ClientOperation. Then we get the result.