Remote Method Invocation

Posted By : Surbhi Gupta | 29-Nov-2017

Introduction to Remote Method Invocation

1. The RMI (Remote Method Invocation) is an API that gives a mechanism to create distributed application in java. 

2. It is a mechanism that provides an object residing in one system (JVM) to access or invoke an object running on another JVM

3.The RMI provides remote communication between the applications using two objects stub and skeleton.

4. It is provided in the package java.rmi.

Architecture of an RMI Application

A remote object is an object whose method can be invoked from another JVM. Let's understand the stub and skeleton objects:

1. Stub

  1.1 A stub is a presentation (proxy) of the remote object at client side.It shows in the client system and  acts as a gateway for the client program.

  1.2.When the guest summons technique on the stub question, it does the accompanying errands: 

      1.2.1. Introduction of association with remote Virtual Machine (JVM), 

      1.2.2.  It composes and transmits (marshals) the parameters to the remote Virtual Machine (JVM), 

      1.2.3. It sits tight for the outcome 

      1.2.4. It peruses (unmarshals) the arrival esteem or special case and 

      1.2.5. It at long last, restores the incentive to the guest.

2. Skeleton 

2.1. This is the object which resides on the server side. stubcommunicates with this skeleton to pass request to the remote object.

2.2.  Its goes about as an gateway for the server side object. All the approaching solicitations are steered through it. At the point when the skeleton gets the approaching solicitation, it does the accompanying assignments: 

    2.2.1.It peruses the parameter for the remote strategy 

    2.2.2.It conjures the strategy on the real remote question, and 

    2.2.3.It composes and transmits (marshals) the outcome to the guest.

3.Transport Layer − This layer connects the client and the server side. It manages the existing connection between them  and also sets up new connections.

4.RRL(Remote Reference Layer) − It is the layer which manages the references made by the client to the remote object.

In the Java 2 SDK, an stub protocol was introduced that eliminates the need for skeletons.

Defining the Remote Interface

// 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");  
   }  
} 

Developing the Server Program

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

Developing the Client Program

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

About Author

Author Image
Surbhi Gupta

Surbhi is a Java Developer and a simple ,straightforward, friendly person. She loves to work in a challenging job.

Request for Proposal

Name is required

Comment is required

Sending message..