coderz.py

Keep Coding Keep Cheering!

Remote Method Invocation(RMI) in Java

java thread class

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.
RMI

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.

Stored Programs

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
Output

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement