RMI program in Java
Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the same computer or a separate machine. Through RMI, a computer-based (Client-side) object running in the java virtual machine can call the methods over an existing object running in another JVM (on the server side). Through straightforward method calls on the server object, RMI's public remote server object makes it possible for the client and server-side communications.
Stub Object: A block of information is created and sent to the server by the client's stub object.
Operation of RMI
Stub object (on the client side) and Skeleton object (on the server side) are the two intermediate objects that handle communication between the client and server, as can also be seen from the below media:
The following are the actions that must be taken in this order to implement the interface as stated below:
- Making a remote interface explicit
- putting the remote interface in place
- Making Stub and Skeleton objects with RMI from the implementation class (RMI compiler)
- Launch the rmiregistry.
- The server application programme should be created and run.
- The client application software should be created and run.
Step 1: The first step is to define the remote interface
Making an interface with a description of the methods that remote clients can call is the first thing that has to be done. The RemoteException should be thrown by the method prototype in this interface, which should extend the Remote interface.
An example is:
// design of a search interface
import java.rmi.*;
public interface Find extends Kamal
{
// Making the process of the model
public String enquiry (String find) throws KamalException;
}
Step 2: working to implement the remote interface
To incorporate the remote interface, the class must extend the java. RMI package's UnicastRemoteObject class. Additionally, a default function Object() must be written in order to raise the java. RMI. RemoteException from the class's parent function Object().
// The Search interface is implemented using a Java application.
// The Search interface is implemented using a Java application.
import java.rmi.*;
import java.rmi.server.*;
public class QuerySearch extends UnicastRemoteObject
implement the Search
{
// remote exception is thrown by default by the method Object()
// inherited from its parent constructor
QuerySearch () throws RemoteException
{
super();
}
// putting the query interface into practice
public String query(String search)
throws RemoteException
{
String answer;
if (search.equals("Java use of reflection "))
answer = "Located";
else
result = "Not Located";
return answer;
}
}
Step 3: Use rmic to produce Stub and Skeleton instances from the implementation class
The Stub and Skeleton objects are produced by RMI compiler, which is called the rmic tool. Rmic classname serves as its prototype. For the software, as mentioned earlier, the command rmic SearchQuery must be entered at the command prompt.
Step 4: Launch the rmiregistry
The command start rmiregistry at the command prompt will start the registry service.
Step 5: Create and execute the server application program
The server application programme must be written and run on a different command prompt.
- The server programme passes the port number as a parameter to the create registry function of the LocateRegistry class, which then creates the rmiregistry inside the server JVM.
- The remote object is bound to the new name using the Naming class rebind function.
// Java server application programme
import java.rmi.*;
import java.rmi.registry.*;
public class ServerSearch
{
public static void main(String args[])
{
try
{
// Make an interface-related item
// class for implementation
Search sr = new QuerySearch();
// the server JVM's rmiregistry with
// port 2000
LocateRegistry.createRegistry(2000);
// ties the distant item to its name
// sreejarao
Naming.rebind("rmi://localhost:2000"+
"/sreejarao",sr);
}
catch(Exception ae)
{
System.out.println(ae);
}
}
}
Step 6: Establish and run the client application program
The client application program must be written before running on a different command prompt as the final stage. The Naming class lookup method is used to obtain the Stub object's reference.
Note: The client and server programmes mentioned above run on the same system, using localhost. The IP address of the computer hosting the remote object should be substituted for localhost to access it from another machine.
Important Observations for Search.java, QuerySearch.java, SearchServer.java, and ClientRequest.java
- To build distributed Java applications, RMI, a pure Java alternative to Remote Procedure Calls (RPC), is employed.
- Stub and Skeleton objects are utilised for client-server communication.