×

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

JIT in Java

What is JIT ? JIT stands for " Just in Time Compiler ". It is called "Just in time" because it is called at the very last moment when the interpretation...

5 minutes read.

Java Math cbrt() Method

The cbrt() method of Math class returns the cube root of a double value. Syntax: public static double cbrt(double a) Parameters: The parameter ‘a’ represents the value whose cube root is to be determined. Return...

2 minutes read.

Class memory in Java

Anything specifically defined in the Java code is stored either inside the heap or the stack, which is particularly significant. Stack and heap are the two kinds of memory available...

6 minutes read.

User Defined Custom Exceptions in Java

In this tutorial, we will discuss user-defined custom exceptions with examples. Introduction In Java, we have proactively characterised, Exception classes, for example, ArithmeticException, NullPointerException, ArrayoutOfBound and so on. These built-in exceptions are...

3 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Scanner in Java

Static way of Programming: When a variable can’t change its value during run time is called a Static way of programming. In this programming a variable is directly assigned to a...

4 minutes read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.

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

Addition Program in Java

Addition Program in Java We can perform the addition (sum) of two or more numbers by using the arithmetic operator (+). To write an addition program in Java, one must understand...

3 minutes read.

HttpURLConnection

HttpURLConnection Protocol It is a standard set of rules that allow electronic devices to communicate with each other. The http protocol The http protocol is for data communication, distributing, collaborating, hypermedia information systems, on the World Wide...

5 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.