Java RMI

Java RMI Introduction

Java Remote Method Invocation (RMI) enables remote communication between programs running across different Java Virtual Machines (JVM). RMI essentially allows an object running in one JVM to invoke a method on another object running in a different JVM. Java RMI provides the ability to write distributed objects in a simple and secure manner with the power of passing complete objects as arguments and return values.
Java RMI applications typically have a client and server. A server program binds the remote objects via registry and makes it accessible by clients. The client program typically does a look-up for the remote objects in the registry, obtains a remote object reference and perform a method call on the remote object. RMI is the mechanism which the server and client use to exchange the information.

Java RMI Application Architecture
Some of the key Java interfaces and classes necessary to understand and build an RMI application are listed below.
  • java.rmi.Remote – Marker interface that needs to be extended directly or indirectly by a remote interface. Refer to Java RMI Interface Example below.
  • java.rmi.RemoteException – Superclass of all exceptions thrown by the RMI runtime during the remote method invocation.
  • java.rmi.server.UnicastRemoteObject – Provides the server function and provides a singleton remote object.
Note: To use this application invoke the rmiregistry application which is a simple bootstrap nameserver that comes with the Java distribution.

Java RMI Interface Example

First step in a RMI application is to design the remotely accessible part of the application from another Java virtual machine. This is achieved by extending the java.rmi.Remote interface. In this example we define a simple interface with a single method to be invoked over RMI.

package com.sourcetricks.intf;import java.rmi.Remote;import java.rmi.RemoteException;public interface ComputeIntf extends Remote {int doubleThis(int input) throws RemoteException;}

Java RMI Server Example

To implement a RMI server the remote object usually extends the UnicastRemoteObject and implements the remote interface we have defined earlier. Then we create a Registry instance and then bind the remote object reference to a specific name which can be accessed by client program over RMI.
package com.sourcetricks.server;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;import java.rmi.server.UnicastRemoteObject;import com.sourcetricks.intf.ComputeIntf;public class RmiServer extends UnicastRemoteObjectimplements ComputeIntf {private static final long serialVersionUID = 1L;protected RmiServer() throws RemoteException {super();}@Overridepublic int doubleThis(int input) throws RemoteException {return (input * 2);}public static void main(String[] args) {try {// Name to be used in the registryString name = "ComputeEngine";// The remote objectRmiServer engine = new RmiServer();// Creates and exports a Registry instance on// the local host that accepts requests on the// specified port.Registry registry = LocateRegistry.createRegistry(3500);// Replaces the binding for the specified name// in this registry with the supplied remote ref.// If there is an existing binding for the specified// name, it is discarded.registry.rebind(name, engine);System.out.println("RmiServer bound");} catch (Exception e) {System.err.println("RmiServer exception:");e.printStackTrace();}}}

Java RMI Client Example

Writing an RMI client application to access a remote object is simple. In the example below we locate the remote object registry on a specified port. We look-up the registry with the specific name and get a reference to the remote object. Then we invoke the remote method similar to a local method and the invocation happens over RMI.

package com.sourcetricks.client;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;import com.sourcetricks.intf.ComputeIntf;public class RmiClient {public static void main(String args[]) {try {String name = "ComputeEngine";// Returns a reference to the the remote object// Registry for the local host on the specified// port.Registry registry = LocateRegistry.getRegistry(3500);// Returns the remote reference bound to the// specified name in this registry.ComputeIntf comp = (ComputeIntf) registry.lookup(name);System.out.println(comp.doubleThis(10));} catch (Exception e) {System.err.println("RmiClient exception:");e.printStackTrace();}}}

Java RMI References