×

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:

  1. Making a remote interface explicit
  2. putting the remote interface in place
  3. Making Stub and Skeleton objects with RMI from the implementation class (RMI compiler)
  4. Launch the rmiregistry.
  5. The server application programme should be created and run.
  6. 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

  1. To build distributed Java applications, RMI, a pure Java alternative to Remote Procedure Calls (RPC), is employed.
  2. Stub and Skeleton objects are utilised for client-server communication.

Related Topics

How to Round Double Float up to Two Decimal Places in Java

It indicates 15 digits just after the decimal place in Java whenever a double data type is used in front of a variable. For example, when representing rupees and other...

4 minutes read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 minutes read.

Thread Synchronization in Java

In Java, the smallest processing component is a thread, which is a small subprocess. It follows a different course of action. Threads are autonomous. If an exception occurs in one thread,...

6 minutes read.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

Zigzag Array in Java

In this tutorial, we discuss, what is zigzag array and its example. Even we will create the java program. In this program, we convert the simple array into a zigzag...

4 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

Lombok Java

What is Lombok java? A well-liked and widely-used Java framework that is used to reduce or eliminate boilerplate code is called Project Lombok. Both time and effort are saved. We may...

7 minutes read.

Lambda expressions in Java

A brief introduction to Lambda expression in java In this topic, we will discuss the lambda expression in java. A lambda expression in Java is an enhanced version of an anonymous...

13 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Advantages and Disadvantages of Strings in Java

What is a String? Java is an object-oriented, platform-independent, high-level, general-purpose, and interpreted programming language.Sun Microsystems is known as the founder of java in 1991; the Java programming language was developed...

4 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

Powerful Number in Java

We will define a powerful number in this article and write Java programs to determine whether a given number is a powerful number or not. Java coding interviews and academic...

6 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.