Online-Academy
Look, Read, Understand, Apply

RMI - Example

RMI, which stands for Remote Method Invocation, is a mechanism in Java that allows an object running in one Java Virtual Machine (JVM)—potentially on a different machine—to invoke methods on an object residing in another JVM. It is used to build distributed applications in Java.

RMI makes calling a method on a remote object seem virtually the same as calling a method on a local object. Key components facilitate this:

  • Remote Interface: An interface that declares the methods that can be invoked remotely. It must extend the java.rmi.Remote interface, and all methods must throw java.rmi.RemoteException.
  • Remote Object (Server-side): The actual implementation of the remote interface. It handles the method logic and runs on the server JVM.
  • Stub (Client-side Proxy): An object on the client machine that acts as a proxy for the remote object. When the client calls a method, the stub handles the communication:
    • It establishes a connection to the remote JVM.
    • It marshals (packages) the parameters into a message.
    • It sends the message to the server.
    • It waits for the result.
    • It unmarshals (unpackages) the result or exception and returns it to the client.
  • RMI Registry: A simple naming service on the server where the remote object is registered under a specific name. The client uses this registry to look up the remote object's reference (the stub).

Both Server and client are in same machine and in same directory

Search.java // Interface import java.rmi.*; public interface Search extends Remote { // Declaring the method prototype public String find(String search) throws RemoteException; } ////SearchQuery class implementing Search Interface import java.rmi.*; import java.rmi.server.*; public class SearchQuery extends UnicastRemoteObject implements Search { // Default constructor to throw RemoteException // from its parent constructor SearchQuery() throws RemoteException { super(); } // Implementation of the query interface public String find(String search) throws RemoteException { String result; if (search.equals("Java is Coffee")) result = "Yes, Java is Coffee"; else result = "No, Java is not coffee"; return result; } } ///SearchServer -- waiting for client request import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.UnicastRemoteObject; public class SearchServer { public SearchServer(){} public static void main(String args[]) { try { Search stub = new SearchQuery(); Naming.bind("rmi://localhost:1099/find",stub); System.out.println("Search Server is listening..."); } catch(Exception ae) { System.out.println("SearchServer: "+ae); } } } ///Client asking for service from server import java.rmi.*; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class ClientRequest { private static Search stub = null; private ClientRequest(){} public static void main(String args[]) { String answer,value="Java is Coffee"; try { Registry reg = LocateRegistry.getRegistry("localhost"); stub = (Search) reg.lookup("find"); answer = stub.find(value); // lookup method to find reference of remote object System.out.println("Answer: "+answer); } catch(Exception ae) { System.out.println("Client Request: "+ae.getMessage()); } } }
Executing : 
E:\rmi>javac *.java E:\rmi>start rmiregistry E:\rmi>javac SearchServer.java E:\rmi>start rmiregistry //Starting Server E:\rmi>java SearchServer Search Server is listening...
//Client
E:\rmi>java ClientRequest
Answer: Yes, Java is Coffee