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.
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).
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:
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:
Below is the 6 - six steps to write the RMI program in java.
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;
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; } }
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.
Start the service of the registry by putting the following command on the prompt of start rmiregistry.
The next step is the development and execution on a separate command prompt of the server application program.
//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); } } }
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:
Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.
Thank you for your valuable information.
Thank you for this wonderful article!
This article was really helpful to me, thank you!
super article!
Leave a Reply
Your email address will not be published. Required fields are marked *