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.
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.
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-
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.
The skeleton object forwards the request from the stub object to the remote object. It carries out the following functions:
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.
The points listed below summarize how an RMI application works-
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:
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-
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 −
Or,
Open the folder where you have stored all the programs and compile all the Java files as shown below.
Step 6: Executing the Application
start rmiregistry
Java Server
java Client
Note: also read about the Java.lang.Class class in Java
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…