RMI – Remote Method Invocation
RMI – Object Oriented version of RPC
RMI allows a program to invoke methods on an object located on a different machine
RMI is the distributed object system that is built into the core Java environment. Allows programmers to develop distributed Java programs with the same syntax and semantics used for non distributed programs
RMI architecture is based on the principle that the definition of behaviour and the implementation of that behaviour are separate concepts. This separation is done using interfaces. Using RMI the code that defines the behaviour and the code that implements the behaviour are separated and
RMI defines two classes that implement the same interface. One class runs in the server and represents the implementation of behaviour
The second class acts as a proxy for the remote service and it runs on the client
A Java calculator that implements the four basic arithmetic operations: addition, subtraction, multiplication and division The Java calculator runs on server and is accessed remotely by a client Java program using RMI
public interface Calculator extends Remote { public long subtract(long a, long b) throws java.rmi.RemoteException; public long add(long a, long b) throws java.rmi.RemoteException; public long multiply(long a, long b) throws java.rmi.RemoteException; public long divide(long a, long b) throws java.rmi.RemoteException; }
public class CalculatorImpl extends UnicastRemoteObject implements Calculator { public CalculatorImpl() throws java.rmi.RemoteException { super(); } public long add(long a, long b) throws java.rmi.RemoteException { System.out.println("SERVER: add method : " + a + " + " + b); return a + b; } public long subtract(long a, long b) throws java.rmi.RemoteException { System.out.println("SERVER: subtract method : " + a + " - " + b); return a - b; } // TODO: multiply and divide methods, implemented in similar fashion }
public class CalculatorServer { public CalculatorServer() { try { Calculator calc = new CalculatorImpl(); LocateRegistry.createRegistry(1099); Naming.rebind("rmi://localhost:1099/CalculatorService", calc); } catch (Exception e) { e.printStackTrace(); } } public static void main(String args[]) { new CalculatorServer(); } }
public class CalculatorClient { public static void main(String[] args) { try { Calculator calc = (Calculator) Naming.lookup("rmi://192.168.1.104/CalculatorService"); long rezSubtract = calc.subtract(4, 3); long rezAdd = calc.add(4, 5); long rezMultiply = calc.multiply(3, 6); long rezDivide = calc.divide(9, 3); } catch (Exception e) { e.printStackTrace(); } }
Classes developed in previous steps need to be compiled and packaged in a jar file. The jar file will be deployed on both client and server machines
This step is required only for Java version 1.4 and earlier
Stubs and Skeletons are generated using RMI compiler rmic
rmic CalculatorImpl
This command generates the stub and skeleton classes, CalculatorImpl_Stub and CalculatorImpl_Skel, in the current directory.
On server machine: run the class CalculatorServer
On client machine: run the class CalculatorClient