RMI (Remote Method Invocation)
RMI is provides a mechanism to create distributed application in java. This mechanism allows an object to residing in one system (JVM) to an object running in another JVM. It is an API.
In RMI application there are two programs we write.
- server program (resides on the server)- a remote object is created and reference of that object is made available for the client (using the registry).
- client program (resides on the client)- requests the remote objects on the server and tries to invoke its methods.
- Transport Layer − Connection between client and server. It controls the existing connection and new connections.
- Stub − A stub is a representation (proxy) of the remote object at client. It resides in the client system; it acts as a gateway for the client program.
- Skeleton − This is the object which resides on the server side. stub communicates with this skeleton to pass request to the remote object.
- RRL(Remote Reference Layer) − It is the layer which manages the references made by the client to the remote object.
Requirements for the distributed application
The is given the 6 steps to write the RMI program
- Create the remote interface
- Provide the implementation of the remote interface
- Compile the implementation class and create the stub and skeleton objects using the rmic tool
- Start the registry service by rmi registry tool
- Create and start the remote application
- Create and start the client application
Here we have to follow 6 steps to create and run the RMI application. The client application consist of two files,
1. Remote interface.
2. Client application.
In the RMI application, both client and server interacts with the remote interface.
The client application invokes methods on the proxy object, RMI sends the request to the remote JVM. The return value is sent back to the proxy object and then to the client application.
Step 1 : create the remote interface
Here, I am going to create a adder interface(remote interface) that extends the Remote interface and create add() method which passes the two parameters (int x and int y)
1. Creating the adder interface.
2. Extend the Remote interface.
3. Declare the Remote Exception with all the methods of the remote interface. (There is only one method named add() and it declares RemoteException)
Step 2 : Provide the implementation of the remote interface
Here i am going to extend the UnicastRemoteObject provide the implementation of the remote interface.
I need to define a constructor that declares RemoteException.
Step 3: create the Main method of server
Step 4: Create the client class
Open command prompt and go to the location where your project files are located and first compile all the java files.
Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool invokes the RMI compiler and creates stub and skeleton objects.
Or else if you a package name, then try the above command with your package name.
rmic <package name>.AdderRemote
Step 6: Start the registry service by the rmi registry tool
Now start the registry service by using the rmi registry tool. If you don’t specify the port number, it uses a default port number. In this project, I am using the port number 5000.
Step 7: Create and run the server application
Run the server in command prompt. Similarly if you have the package name, then state it with your package name. Here you need to specify your server class name. I’m using the name “Server”.
if you don’t have package name: java Server
if you have the package name: java <package name>.Server
Step 8: Create and run the client application
Run the client server in another command prompt. If you have the package name, then state it with your package name. Here you need to specify your client class name. I’m using the name “Client”.
If you want to access the remote object from another machine, change the localhost to the host name (or IP address) where the remote object is located.
if no any package name: java Client
if has the package name: java <package name>.Client
follow this link to visit my code