×

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

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Contextual keywords in Java

Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar. These are the keywords in the code...

3 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Advantages of Generics in Java

Generic offers a variety of benefits. The programmer's life is made easier by using generic Java. In this section, we are going to discuss about Java's generic’s and its benefits. 1....

4 minutes read.

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.

Sum of digits in string in java

To find the sum of all digits in a string, you need to traverse through the string one by one character; if the character is an integer, you need to...

2 minutes read.

Check whether a Number is a Power of 4 or Not in Java

There are many ways to figure out if an integer is a power of 4. This section will go over a variety of techniques for figuring out whether or not...

11 minutes read.

Java Speech Recognition

The customer experience and convenience are improved with its utilization. Command and control interest in buying and voice synthesizers are supported by the cross-platform API defined by the API. For...

4 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Special Operators in Java

An operator in Java is a special symbol. It is used to perform operations on two or more variables.  We all know basic operations in Java. There are 8 types...

5 minutes read.

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

How to Convert Hexadecimal to Decimal in Java

How to Convert Hexadecimal to Decimal in Java There are two methods to convert Hexadecimal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method It is a static method of...

2 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

6 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

Alice and Bob Problem Java

Alice and Bob were two friends who liked to play games. Both together found a game, which has the description below. The game begins with an integer num, used to create...

2 minutes read.