top

Search

Java Tutorial

RMI (Remote method invocation) The RMI is an API that offers a mechanism for creating distributed implementation in Java. The RMI enables techniques/method to be invoked by an object operating in another JVM. The RMI uses two items, stub and skeleton, to provide distant communication between the apps. RMI Explanation: The communication between client and server is treated using two intermediate items/objects, Stub object (on the client side) and Skeleton object (on the server side). Stub The stub is an object, acting on the client side as a gateway. It routes all the outgoing applications. It lies on the side of the client and represents the object remote. When calling technique on the stub item, the caller performs the following duties: It initiates a remote virtual machine (JVM) link/connection. The parameters are written and transmitted (marshals) to the remote Virtual Machine (JVM) by it. It is waiting for the outcome/result. It reads the return value or exception (unmarshals) and At the end,  it returns the value/response to the caller. Skeleton The skeleton is an object that acts as a gateway to the side object of the server. It routes all incoming requests through it. When the incoming request is received by the skeleton, the following tasks are performed: It reads the remote method parameter It executes/invokes the method on the actual remote object. It writes and transmits the result to the caller (marshals). Below is the 6 - six steps to write the RMI program in java. Creation of remote interface Provide the implementation of the remote interface Compile the implementation class and create the stub and skeleton objects using the rmic tool Start the registry service by rmi registry tool Write and start the remote application Write and start the client application 1. First Define the java remote interface First thing to do is create an interface to describe the techniques that can be invoked by distant clients. This interface should extend the Remote interface and throw the RemoteException inside the interface using the method prototype. // Creating a Search interface   import java.rmi.*;   public interface MySearch extends Remote   {   // Declaring the method prototype   public String query(String search) throws RemoteException;  2. Implement the java remote interface The next step is to implement the remote interface. To execute the remote interface, the class should extend to the UnicastRemoteObject class java.rmi package. In addition, a default constructor must be formed to transfer the java.rmi.RemoteException from its class parent constructor. // Java program to implement the MySearch interface   import java.rmi.*;   import java.rmi.server.*;   public class SearchQuery extends UnicastRemoteObject   implements MySearch  {    SearchQuery() throws RemoteException   {   super();   }   // Implementation of the query interface   public String query(String search)   throws RemoteException   {   String result;   if (search.equals("Reflection in Java"))   result = "Yes it’s found";   else  result = "No its not found";   return result;   }   }   3. Writing Stub and Skeleton objects from the implementation class using rmic The rmic instrument is used to invoke the rmi compiler which produces the Stub and Skeleton items. His prototype is the rmic class's name. For the above program, the following command must be executed at the rmicSearchQuery command prompt.  4. Start/Invoke the rmiregistry Start the service of the registry by putting the following command on the prompt of start rmiregistry.  5. Write and execute the server application program The next step is the development and execution on a separate command prompt of the server application program. The server program uses the creation technique of the LocateRegistry class to produce rmiregistry with the passed port number as the argument within the JVM server. The Naming class rebind method is used to bind the remote object to the new name. //Java server application import java.rmi.*;   import java.rmi.registry.*;   public class SearchServer   {   public static void main(String args[])   {   try  {   // Create an object of the interface   // implementation class   Search obj = new SearchQuery();   // rmiregistry within the server JVM with   // port number 1900   LocateRegistry.createRegistry(1900);   // Binds the remote object by the name   Naming.rebind("rmi://localhost:1900"+   "/test",obj);   }   catch(Exception ae)   {   System.out.println(ae);   }   }   }  6. Write and execute the client application program The last stage is to generate and implement the client application program on a distinct command prompt. The Naming class lookup method is used to get the Stub object reference. //Java client application import java.rmi.*;   public class ClientRequest   {   public static void main(String args[])   {   String answer,value="Reflection in Java";   try  {   // lookup method to find reference of remote object   Search access =   (Search)Naming.lookup("rmi://localhost:1900"+   "/test");   answer = access.query(value);   System.out.println("Article on " + value +   " " + answer+" at online");   }   catch(Exception ae)   {   System.out.println(ae);   }   }   }  To use localhost, the above client and server program runs on the same machine. To access the remote object from another device, the localhost must be replaced with the IP address where the remote object is present. Points to remember: RMI stands for invocation of the remote method. It is a mechanism that enables the access / invoke of an item/object operating on another JVM by an object residing in one system (JVM). It is used to construct/build distributed applications ; it offers Java programs with remote communication. The Stub and Skeleton objects are used for client-to-server interaction. 
logo

Java Tutorial

Java RMI

RMI (Remote method invocation) 

The RMI is an API that offers a mechanism for creating distributed implementation in Java. The RMI enables techniques/method to be invoked by an object operating in another JVM. 

The RMI uses two items, stub and skeleton, to provide distant communication between the apps. 

RMI Explanation: 

The communication between client and server is treated using two intermediate items/objects, Stub object (on the client side) and Skeleton object (on the server side). 

Stub 

The stub is an object, acting on the client side as a gateway. It routes all the outgoing applications. It lies on the side of the client and represents the object remote. When calling technique on the stub item, the caller performs the following duties: 

  • It initiates a remote virtual machine (JVM) link/connection. 
  • The parameters are written and transmitted (marshals) to the remote Virtual Machine (JVM) by it. 
  • It is waiting for the outcome/result. 
  • It reads the return value or exception (unmarshals) and 
  • At the end,  it returns the value/response to the caller. 

Skeleton 

The skeleton is an object that acts as a gateway to the side object of the server. It routes all incoming requests through it. When the incoming request is received by the skeleton, the following tasks are performed: 

  • It reads the remote method parameter 
  • It executes/invokes the method on the actual remote object. 
  • It writes and transmits the result to the caller (marshals). 

Working of RMI

Below is the 6 - six steps to write the RMI program in java. 

  • Creation of remote interface 
  • Provide the implementation of the remote interface 
  • Compile the implementation class and create the stub and skeleton objects using the rmic tool 
  • Start the registry service by rmi registry tool 
  • Write and start the remote application 
  • Write and start the client application

 1. First Define the java remote interface 

First thing to do is create an interface to describe the techniques that can be invoked by distant clients. This interface should extend the Remote interface and throw the RemoteException inside the interface using the method prototype. 

// Creating a Search interface  
import java.rmi.*;  
public interface MySearch extends Remote  
{  
    // Declaring the method prototype  
    public String query(String search) throws RemoteException;  

2. Implement the java remote interface 

The next step is to implement the remote interface. To execute the remote interface, the class should extend to the UnicastRemoteObject class java.rmi package. In addition, a default constructor must be formed to transfer the java.rmi.RemoteException from its class parent constructor. 

// Java program to implement the MySearch interface  
import java.rmi.*;  
import java.rmi.server.*;  
public class SearchQuery extends UnicastRemoteObject  
                         implements MySearch 
{   
    SearchQuery() throws RemoteException  
    {  
        super();  
    }  
    // Implementation of the query interface  
    public String query(String search)  
                       throws RemoteException  
    {  
        String result;  
        if (search.equals("Reflection in Java"))  
            result = "Yes it’s found";  
        else 
            result = "No its not found";  
        return result;  
    }  
}  

 3. Writing Stub and Skeleton objects from the implementation class using rmic 

The rmic instrument is used to invoke the rmi compiler which produces the Stub and Skeleton items. His prototype is the rmic class's name. For the above program, the following command must be executed at the rmicSearchQuery command prompt. 

 4. Start/Invoke the rmiregistry 

Start the service of the registry by putting the following command on the prompt of start rmiregistry. 

 5. Write and execute the server application program 

The next step is the development and execution on a separate command prompt of the server application program. 

  • The server program uses the creation technique of the LocateRegistry class to produce rmiregistry with the passed port number as the argument within the JVM server. 
  • The Naming class rebind method is used to bind the remote object to the new name. 

//Java server application 

import java.rmi.*;  
import java.rmi.registry.*;  
public class SearchServer  
{  
public static void main(String args[])  
{  
try 
{  
// Create an object of the interface  
// implementation class  
Search obj = new SearchQuery();  
// rmiregistry within the server JVM with  
// port number 1900  
LocateRegistry.createRegistry(1900);  
// Binds the remote object by the name  
Naming.rebind("rmi://localhost:1900"+  
"/test",obj);  
}  
catch(Exception ae)  
{  
System.out.println(ae);  
}  
}  
} 

 6. Write and execute the client application program 

The last stage is to generate and implement the client application program on a distinct command prompt. The Naming class lookup method is used to get the Stub object reference. 

//Java client application 

import java.rmi.*;  
public class ClientRequest  
{  
    public static void main(String args[])  
    {  
        String answer,value="Reflection in Java";  
        try 
        {  
            // lookup method to find reference of remote object  
            Search access =  
                (Search)Naming.lookup("rmi://localhost:1900"+  
                                      "/test");  
            answer = access.query(value);  
            System.out.println("Article on " + value +  
                            " " + answer+" at online");  
        }  
        catch(Exception ae)  
        {  
            System.out.println(ae);  
        }  
    }  
}  

To use localhost, the above client and server program runs on the same machine. To access the remote object from another device, the localhost must be replaced with the IP address where the remote object is present. 

Points to remember: 

  • RMI stands for invocation of the remote method. It is a mechanism that enables the access / invoke of an item/object operating on another JVM by an object residing in one system (JVM). 
  • It is used to construct/build distributed applications ; it offers Java programs with remote communication. 
  • The Stub and Skeleton objects are used for client-to-server interaction. 

Leave a Reply

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

Comments

protective orders in virginia

Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.

Johnfrance

Thank you for your valuable information.

sam

Thank you for this wonderful article!

Belay Abera

This article was really helpful to me, thank you!

dfsdf

super article!