×

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

Hierarchy of operators in Java

Operators are the most frequently used terminology in any area of programming, and it helps in various approaches to efficiently solve daily life problems by computer programming. The simple addition of...

4 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Types of Events in Java

One of Java's key ideas is the principle of an event. Events in Java are activities that result in a change in the state or behavior of an object. The...

4 minutes read.

How to use scanner in Java

Scanner class in Java is the part of java.util package. Java programming language has various ways to read input from the user, Scanner class is one of the classes to...

5 minutes read.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 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.

String isnullorempty in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 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.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 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.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute 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.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Java Lambda foreach

Before understanding lamda foreach, one must know about foreach loop in Java. foreach loop in Java Since J2SE 5.0, there has been a for-each loop in Java, also known as an extended...

4 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.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.