Distributed Computing – RMI (Remote Method Invocation)
Remote Method Invocation (RMI) is a Java technology that allows methods to be invoked remotely over a network. It enables the development of distributed applications in which objects on one Java Virtual Machine (JVM) can invoke methods on objects residing in another JVM. In this article, we will explore RMI in Java, its architecture, and provide code examples to illustrate its usage.
1. Introduction to RMI
RMI is a mechanism that facilitates communication between different Java applications running on separate JVMs. It allows Java objects to interact and invoke methods on remote objects, providing a powerful way to build distributed systems.
2. RMI Architecture
RMI applications typically consist of three main components:
2.1. Remote Interface
The remote interface defines the methods that can be invoked remotely. These methods are declared in an interface, and the implementing class provides the actual logic for the remote methods.
2.2. Remote Object
The remote object is an instance of the class that implements the remote interface. It extends the UnicastRemoteObject
class and overrides the methods defined in the interface.
2.3. RMI Server and Client
The RMI server hosts the remote objects, making them available for remote method invocation. Clients connect to the server and invoke methods on the remote objects. RMI uses a stub and skeleton mechanism to marshal and unmarshal method calls and parameters between the client and server.
3. Creating an RMI Application
Let’s create a simple RMI application that calculates the sum of two numbers remotely. We will define a remote interface, implement it, and create both server and client components.
3.1. Remote Interface
First, define the remote interface:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Calculator extends Remote {
int add(int a, int b) throws RemoteException;
}
3.2. Remote Object
Implement the remote object that provides the actual implementation:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class CalculatorImpl extends UnicastRemoteObject implements Calculator {
public CalculatorImpl() throws RemoteException {
super();
}
public int add(int a, int b) throws RemoteException {
return a + b;
}
}
3.3. RMI Server
Create an RMI server that hosts the remote object:
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class RMIServer {
public static void main(String[] args) {
try {
Calculator calculator = new CalculatorImpl();
LocateRegistry.createRegistry(1099);
Naming.rebind("CalculatorService", calculator);
System.out.println("CalculatorService is running...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.4. RMI Client
Create an RMI client that connects to the server and invokes remote methods:
import java.rmi.Naming;
public class RMIClient {
public static void main(String[] args) {
try {
Calculator calculator = (Calculator) Naming.lookup("rmi://localhost/CalculatorService");
int result = calculator.add(5, 3);
System.out.println("Result: " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4. Running the RMI Application
To run the RMI application, follow these steps:
- Compile the remote interface and implementation classes.
- Start the RMI server using the
RMIServer
class. - Run the RMI client using the
RMIClient
class.
5. Conclusion
RMI is a powerful technology for building distributed applications in Java. It enables remote method invocation between Java objects running on different JVMs, allowing developers to create distributed systems and services. With its straightforward architecture and Java’s built-in support for RMI, developing distributed applications becomes more accessible and efficient.