×

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

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

Java Math log10() Method

The log10() method of Math class returns the base 10 logarithmic value for the specified double argument. Syntax: public static double log10(double a) Parameters: The parameter ‘a’ represents a double value. Return Value: The log10() method...

2 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

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

Applet Program in Java

Applet Program in Java An applet is a program that can be embedded in a web page. Applets programs are run by a web browser. It mainly works on the client-side....

3 minutes read.

Java Tokens

Classes and methods are included in the Java program. The procedures also provide the expressions and statements required to finish a specific operation. Tokens make up the sentences and expressions...

4 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.