Java RMI example for multiple computer

Java RMI is quite old technology but it is still relevant for learning distributed system. Currently, there is lack of example in how to do RMI in multiple computer. In this post, I write RMI server and client code example, that can be run in multiple computer.

The RMI server will work as calculator that contains two remote method namely add and multiply. First, we write remote interface to define the remote method. Here is the remote interface code:

import java.rmi.*;

public interface ICalculator extends Remote{
	public double add(double num1, double num2) throws RemoteException;
	public double multiply(double num1, double num2) throws RemoteException;
}

We then define the implementation of the remote method. Both code reside in server machine. Here is the implementation of ICalculator:

public class Calculator implements ICalculator{
	public double add(double number1, double number2){
		return number1+number2;
	}
	
	public double multiply(double number1,double number2){
		return number1*number2;
	}
}

The server code goes here. What do the server code do? It creates the registry so that it can be looked up by the client. When server program runs, it works with default port, 1099.

import java.rmi.registry.*;
import java.rmi.server.*;
import java.rmi.*;

public class CalculatorRMIServer{
	public static void main(String[] args) throws RemoteException{
		//creating registry in default port untuk objek calculator
		Registry registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
		Calculator calc = new Calculator();
		ICalculator icalc= (ICalculator) UnicastRemoteObject.exportObject(calc,0);
		registry.rebind("calculator",icalc);
		System.out.println("Server ready");
	}
}

The following is the client code. The client code will look for the program argument first. If there is no argument, the server will use localhost as default host.

import java.rmi.*;
import java.rmi.registry.*;

public class CalculatorRMIClient{
	public static void main(String[] args) throws RemoteException,NotBoundException{
		//if there is no argument, use localhost as server
		String host = args.length==0?"localhost":args[0];
		//looking up registry calculator
		Registry registry = LocateRegistry.getRegistry(host,Registry.REGISTRY_PORT);
		ICalculator calculator = (ICalculator)registry.lookup("calculator");
		//implementing calculator operation 
		System.out.println("Result of sum operation ="+calculator.add(10.2 , 0.9));
		System.out.println("Result of multiplication="+calculator.multiply(10.0 , 2.0));
	}
}

Running the example

To run the code, you need to place all the code in single folder. Compile all source code with this following command (in command prompt):

javac *.java

After success compiling, run the server first with this command:

java CalculatorRMIServer

Soon you will see ‘Server ready’ in your command prompt. Then, run the client in another command prompt.

java -Djava.rmi.server.hostname=192.168.10.10 CalculatorRMIClient

We asume that the server host is 192.168.10.10. If your server is in the same machine as client, run without arguments. You will see the result of add and multiply operation.

Happy coding !

Leave a Reply

Your email address will not be published.