Design Patterns    |     Security    |     Testing    |     Distributed Computing    |     Contact

Distributed Computing

 

RMI - Remote Method Invocation

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

 

RMI abstraction layers:

  • Stub and skeleton layer: intercepts method calls made by the client to the interface reference (stub) and redirects these calls to a remote RMI service (skeleton).
  • Remote reference layer: manages references made from clients to the remote service objects
  • Transport layer: manages the actual network level communication between client and server machines

 

Steps in writing an RMI program

  1. Write Java interfaces
  2. Write Java code for implementation classes
  3. Write Java code for a host program (server)
  4. Write Java code for client program
  5. Deploy Java classes
  6. Generate stubs and skeletons
  7. Run the RMI system

 

Example

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

Write the Java Interface

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;
}

Write the Java Implementation Class

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 
}

Write the Java Code For Server

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();
	}
}

Write the Java Code For the Client

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();
	    } 
}

Deploy Java Classes

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

Generate Stubs and Skeletons

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.

Run the RMI System

On server machine: run the class CalculatorServer

On client machine: run the class CalculatorClient