×

Java RMI

What is RMI in Java?

A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in another JVM via RMI.

Two objects, stub, and skeleton are used by the RMI to enable remote communication between the apps.

Understanding stub and skeleton

For communication with the remote object, RMI uses skeleton and stub objects.

Any object whose method can be called from another JVM is considered a remote object. Let's examine how to comprehend stub and skeleton objects:

Stub

A gateway for the client side, the stub is an object. It serves as the conduit for all outbound requests. It is a client-side object that represents a distant object. The following things happen when the caller calls a method on the stub object:

1. It begins communication with a distant virtual machine (JVM),

2. The parameters are written and sent (marshaled) to the distant Virtual Machine (JVM),

3. It anticipates the outcome.

4. The return value or exception is read (unmarshalled), and

5. The value is then returned to the caller.

Skeleton

A gateway for the server-side object, the skeleton is an object. It is used to route each and every request that comes in. The skeleton does the following actions when it gets an incoming request:

1. It reads the remote method parameter

3. It writes and transmits (marshals) the outcome to the caller after invoking the method on the actual remote object in step 2.

A stub protocol that does away with skeletons was added to the Java 2 SDK.

What is RMI in Java

Recognizing the needs of distributed applications

Any program that handles these duties could be distributed.

1. The program needs to find the remote method

3. The program needs to load the object class definitions. It also needs to offer connectivity with remote objects.

With all these features, the RMI application qualifies as a distributed application.

Java RMI Example

The six steps for writing the RMI program are provided.

  • Establish the remote interface.
  • Describe how to implement a remote interface.
  • Utilizing the rmic tool, compile the implementation class and produce the skeleton and stub objects.
  • the rmiregistry utility to launch the registry service
  • Establish and launch the remote application.
  • Establish and launch the client application.

Now let us discuss each of them in brief.

What is RMI in Java

Establish the remote interface

The Remote interface should be extended to create the Remote interface, and all of the RemoteException's methods should be declared. Here, a remote interface that expands the Remote interface is being created. The add() function is the sole one and it declares RemoteException.

import java.rmi.*;  
public interface Subtracter extends Remote{  
public int sub(int x,int y)throws RemoteException;  
}  

Describe how to implement a remote interface

Now present the remote interface implementation. We must either utilize the exportObject() method of the UnicastRemoteObject class or extend the UnicastRemoteObject class to provide the implementation of the Remote interface.

If you decide to extend the UnicastRemoteObject class, you must include a function Object() { [native code] } (constructor) with the RemoteException keyword.

SubtracterRemote.java

import java.rmi.*;  
import java.rmi.server.*;  
public class SubtracterRemote extends UnicastRemoteObject implements Subtracter{  
SubtracterRemote()throws RemoteException{  
super();  
}  
public int sub(int a,int b){
return a-b;
}  
}  

Utilizing the rmic tool, compile the implementation class and produce the skeleton and stub objects

The rmi compiler will then be used to construct skeleton and stub objects. The rmic tool generates skeleton and stub objects while invoking the RMI compiler.

rmic SubtracterRemote 

the rmiregistry utility to launch the registry service

The rmiregistry tool is now used to launch the registry service. A default port number is used if the port number is not specified. We're using port 5000 in this instance.

rmiregistry 5000

Establish and launch the remote application

A server process is now required to host rmi services. The Naming class offers ways to retrieve and save distant objects.

SearchServer.java

import java.rmi.*;  
import java.rmi.registry.*;  
public class SearchServer
{  
public static void main(String args[])
{  
try{  
Subtracter s=new SubtracterRemote();  
Naming.rebind("rmi://localhost:5000/javaTpoint",s);  
}
catch(Exception e)
{
System.out.println(e);
}  
}  

Establish and launch the client application

Using the lookup() function of the Naming class at the client, we are retrieving the stub object and calling the method on it. In this example, localhost is used because the server and client programs are both executing on the same system. Change localhost to the hostname (or IP address) of the machine where the remote item is located if you want to access it from another machine.

ClientServer.java

import java.rmi.*;  
public class ClientServer
{  
public static void main(String args[])
{  
Try
{  
Subtracter s=(Subtracter)Naming.lookup("rmi://localhost:5000/javaTpoint");  
System.out.println(stub.sub(34,4));  
}
catch(Exception e)
{ }  
}  
}  

Output:

30

Related Topics

Blocking Queue in Java Example

Let's first briefly comprehend queue before moving on to the topic of "Blocking Queue." A queue seems to be an orderly list of items in which elements are added from...

8 minutes read.

How to Update Java

As we all know that java can be installed in all operating systems like windows, Linux, macOS. We are available with the java 17 and java 18 versions in the...

3 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

String Palindrome Program in Java

String Palindrome Program in Java The palindrome is a string, phrase, word, number, or other sequences of characters that can be read in both directions i.e. forward (left to right) and...

11 minutes read.

Sleeping Barber Problem in Java

The barbershop in this issue has one barber, one barber chair, and N chairs for customers in the waiting area. We may demonstrate the issue by keeping with the original...

6 minutes read.

How to run Java Program?

How to run Java Program In this section, we will learn how to write, compile and run a Java program in Command Promptusing notepad. In order to run a Java program, we...

2 minutes read.

JAR File in Java

What is a JAR File? JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one...

4 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Vigesimal in Java

A number system with a base of 20 is known as the vigesimal in Java. A base-20 (base-score) numeric system, often known as a vigesimal system, is centered on the twenty...

4 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

Method and Block Synchronization in Java

The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by...

3 minutes read.

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.