Remote Method Invocation (RMI):
It is a distributed systems technology that allows one Java Virtual Machine (JVM) to invoke object methods that will be run on another JVM located elsewhere on a network.
Figure: The RMI client stub calls the RMI server skeleton.
- The skeleton object is responsible for listening for incoming RMI requests from the stub object and passing these requests on to the RMI service.
- The skeleton object passes the results back to the stub object in the RMI client.
- Communication occurs between stub and skeleton using TCP sockets
Steps to implement RMI:
1. Define the RMI service interface. It should extend the java.rmi.Remote interface and the methods should throws RemoteException.
2. Create the server class that:
a. Export the remote object using exportObject method:
b. Register (rebind) the service in the rmiregister, so other clients can access it.
3. Create the client class. The client obtains the object reference to the remote interface using lookup in the rmiregistry.
The following is an example of RMI Bank Account application, The server supplies operations (deposit, withdraw and getDeposit) to be called remotely by the Client on the bank account.
The Server Application:
The Client Application:
Running the RMI System:
- Copy the necessary file (BankAccount interface) to a directory on the local file system of all clients and the server.
- Compile both Server and Clien application using command javac *.java
- Run the rmiregistry application, using command: rmiregistry.
- In a separate console window, run the server.
- In a separate console window, and preferably a different machine, run the client.