Categories: Java

Remote Method Invocation(RMI) in Java

Remote Method Invocation (RMI) is an API that allows an object to call a method on another object in another address space, which could be on the same machine or another machine. RMI enables remote communication between Java programs and is used to create distributed applications. It is provided in the package java.rmi.

RMI Application Architecture:

In an RMI application, we write two programs: a server program (which runs on the server) and a client program (which runs on the client) (which resides on the client).  Remote communication between the applications is provided using two objects, stub, and skeleton.

A remote object is created within the server program, and a reference to that object is made available to the client (using the registry).

The client program queries the server for remote objects and attempts to invoke their methods.

Stub Object:

A stub is an object that serves as a client-side gateway. It handles all incoming and outgoing requests. It is client-side and represents the remote object.

The block is made up of-

  • A remote object identifier that will be used
  • The name of the method to be invoked
  • parameters to be passed to the remote JVM

It performs the following functions:

It establishes a connection with the remote Virtual Machine (JVM), writes and transmits the parameters to the remote Virtual Machine (JVM), waits for the result, reads (unmarshals) the return value or exception, and finally returns the value to the caller.

Skeleton object:

The skeleton object forwards the request from the stub object to the remote object. It carries out the following functions:

  • It invokes the desired method on the server’s real object.
  • It passes the parameters from the stub object to the method.

Note: Parameters are packed into a message and sent over the network during RMI communication between two machines. The process of packing parameters into a message is known as marshalling. Unmarshalling is the process by which these packed parameters are unpacked from the message.

Working of RMI Application:


The points listed below summarize how an RMI application works-

  • When the client makes a call to the remote object, it is intercepted by the stub, which then forwards the request to the RRL.
  • When the client-side RRL receives the request, it calls the remoteRef object’s invoke() method. It forwards the request to the RRL on the server.
  • The RRL on the server side forwards the request to the Skeleton (server proxy), which then invokes the required object on the server.
  • The outcome is sent back to the client.
To create an RMI Java application, follow the steps outlined below.
  • Create the remote interface.
  • Develop the implementation class (remote object)
  • Create the server program.
  • Create the client program.
  • Compile the program
  • Run the application.

Step 1: Create the remote interface

The first step is to create an interface that contains a description of the methods that can be invoked by remote clients. Client programs communicate with remote interfaces rather than classes that implement them. A remote interface must extend the Remote interface of java.rmi package.


import java.rmi.Remote; 
import java.rmi.RemoteException;  

// Creating Remote interface for our application 
public interface Hello extends Remote {  
   void printMsg() throws RemoteException;  
} 

Step 2: Implementing the remote interface

To implement the remote interface, the class should extend the java.rmi package’s UnicastRemoteObject class.

// Implementing the remote interface 
public class ImplExample implements Hello {  
   
   // Implementing the interface method 
   public void printMsg() {  
      System.out.println("This is an example RMI program");  
   }  
} 

Step 3: Developing the Server Program

An RMI server program should either implement or extend the implementation class. Create a remote object and bind it to the RMI registry here.

To create a server program:

  • Create a client class from which to call the remote object.
  • As shown below, instantiate the implementation class to create a remote object.
  • The method exportObject() of the class UnicastRemoteObject in the package java.rmi.server is used to export the remote object.
  • Get the RMI registry by calling the getRegistry() method of the LocateRegistry class, which is part of the java.rmi.registry package.
  • Bind the newly created remote object to the registry using the bind() method of the Registry class. As parameters, pass a string representing the bind name and the object exported to this method.
import java.rmi.registry.Registry; 
import java.rmi.registry.LocateRegistry; 
import java.rmi.RemoteException; 
import java.rmi.server.UnicastRemoteObject; 

public class Server extends ImplExample { 
   public Server() {} 
   public static void main(String args[]) { 
      try { 
         // Instantiating the implementation class 
         ImplExample obj = new ImplExample(); 
    
         // Exporting the object of implementation class  
         // (here we are exporting the remote object to the stub) 
         Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);  
         
         // Binding the remote object (stub) in the registry 
         Registry registry = LocateRegistry.getRegistry(); 
         
         registry.bind("Hello", stub);  
         System.err.println("Server ready"); 
      } catch (Exception e) { 
         System.err.println("Server exception: " + e.toString()); 
         e.printStackTrace(); 
      } 
   } 

Step 4: Creating the Client Program:

Create a client program in it, retrieve the remote object, and use this object to invoke the required method.

To create a client program-

  • Create a client class from which to invoke the remote object.
  • Get the RMI registry by calling the getRegistry() method of the LocateRegistry class, which is part of the java.rmi.registry package.
  • Using the method lookup() of the class Registry from the package java.rmi.registry, retrieve the object from the registry.
  • As a parameter to this method, you must pass a string value representing the bind name. This will return the remote object to you.
  • Lookup() returns an object of type remote, which is downcast to Hello.
import java.rmi.registry.LocateRegistry; 
import java.rmi.registry.Registry;  

public class Client {  
   private Client() {}  
   public static void main(String[] args) {  
      try {  
         // Getting the registry 
         Registry registry = LocateRegistry.getRegistry(null); 
    
         // Looking up the registry for the remote object 
         Hello stub = (Hello) registry.lookup("Hello"); 
    
         // Calling the remote method using the obtained object 
         stub.printMsg(); 
         
         // System.out.println("Remote method invoked"); 
      } catch (Exception e) {
         System.err.println("Client exception: " + e.toString()); 
         e.printStackTrace(); 
      } 
   } 
}

Step 5: Compiling the Application

To compile the application −

  • Compile the Remote interface.
  • Compile the implementation class.
  • Compile the server program.
  • Compile the client program.

Or,

Open the folder where you have stored all the programs and compile all the Java files as shown below.

Step 6: Executing the Application

  • Start the rmi registry using the following command.
start rmiregistry
  • Run the server class file as shown below.
Java Server
  • Run the client class file as shown below.
java Client 
  • the following output is displayed

Note: also read about the Java.lang.Class class in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago