Table of contents
1.
Introduction
2.
The Architecture of RMI Application
3.
Working of an RMI Application
4.
Implementation of an RMI Application
4.1.
Defining a remote interface
4.1.1.
Program
4.2.
Implementing Remote Interface
4.2.1.
Program
4.3.
Creating Stub and Skeleton
4.4.
Start The Registry Service
4.5.
Server Application
4.6.
Client Application
4.6.1.
Program
5.
Frequently Asked Questions
6.
Conclusion
Last Updated: Mar 27, 2024

Remote Method Invocation In Java

Introduction

Often in Object-Oriented Programming, the developer is required to create distributed applications. Remote Method Invocation or RMI helps developers build such applications.

RMI allows an object placed in one system or Java Virtual Machine(JVM) to access or invoke an object running on another JVM.

The java.rmi package provides the RMI and allows remote communication between Java programs. Apart from these core uses, there are many key benefits of working with an RMI application. It minimizes the difference between working with local and remote objects and the complexity of the application. RMI also preserves type safety and allows distributed garbage collection of the application.

We will learn the architecture, working, and implementation of an RMI application in Java.

Recommended topics, procedure call in compiler design and Hashcode Method in Java

The Architecture of RMI Application

While creating an RMI application, you have to write two programs; Server and Client.

Two intermediary objects handle the server and the client's communication; the Stub object, which is on the server-side, and the Skeleton object, which is on the client-side. Stub and Skeleton are essentially the remote objects on both sides that communicate.

The image below shows a simplified architecture of an RMI application.

 

The transport layer connects the Client and the Server and manages the existing connections while setting up new connections. The Remote Reference Layer(RRL) works the references made by the Client to the remote object.

Must Read, Multithreading in java, Duck Number in Java

Working of an RMI Application

For an RMI Application to work, it should meet a few basic requirements. Firstly, the application needs to locate the remote method. The application must also provide communication with the remote objects and needs to load the class definitions for the objects. If an application can perform these operations, it can form a distributed application.

Also see, Spring Boot Architecture

Implementation of an RMI Application

Following a few steps in a subsequent order can allow you to implement the RMI application interface quickly. 

Defining a remote interface

Firstly, we will create an interface that describes the methods that remote clients can invoke. The interface should extend the Remote interface, and the method prototype in the interface should throw the RemoteException.

Program

import java.rmi.*;
public interface Adder extends Remote{


//service to perform simple addition of two integers
public int add(int x,int y)throws RemoteException;
}

You can also try this code with Online Java Compiler
Run Code

Implementing Remote Interface

Next, we will implement the remote interface. For that, the class should extend to UnicastRemoteObject class of java.rmi package. We also need to make a default constructor to throw the java.rmi.RemoteException from its parent constructor in the class.

Program

import java.rmi.*;
import java.rmi.server.*;


//implementing the adder service
public class AdderRemote extends UnicastRemoteObject implements Adder{


AdderRemote()throws RemoteException{
super();
}


public int add(int x,int y){return x+y;}


}
You can also try this code with Online Java Compiler
Run Code

Creating Stub and Skeleton

We will now use to rmi compiler to create stub and skeleton objects. The rmic tool calls on the RMI compiler and creates stub and skeleton objects. 

rmic AdderRemote 

Start The Registry Service

Now we will use the rmiregistry tool to start the registry service. It will use a default port number if you don't specify. You can begin the registry service using the command prompt.

Here we are using 5000 as our port number.

rmiregistry 5000

Server Application

We now need to write the server program and execute it on a different command prompt from the registry.

We use the rebind method of the Naming class to bind the remote object to the new name.

Program
import java.rmi.*;


public class MyServer{


public static void main(String args[]){
try{


Adder stub=new AdderRemote();


//we are binding the remote object by the name sonoo
Naming.rebind("rmi://localhost:5000/sonoo",stub);


}catch(Exception e){System.out.println(e);}
}


}
You can also try this code with Online Java Compiler
Run Code

Client Application

Finally, we will create the client application program and execute it using a separate command prompt. We get the stub object by the lookup() method from the Naming class and invoke the method on this object.

Here we will be running the Client and server applications on the same system. You will need to change the localhost to the hostname or IP address of the remote object if you want to access the remote object from another machine.

Program

import java.rmi.*;


public class MyClient{


public static void main(String args[]){
try{


Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
System.out.println(stub.add(34,4));


}catch(Exception e){System.out.println(e);}
}


}
You can also try this code with Online Java Compiler
Run Code


You can also use online java compiler for compile and run the code for good practice.

Frequently Asked Questions

1. Is Java RMI still used?

Ans: While RMI is still functional and handy, its application is limited, and generally, the best method to use it is in-house. Written in 2001, Java RMI is still a valuable and critical tool in many businesses. So it is safe to say that Java RMI is still used.

 

2. Is RMI only for Java?

Ans: RMI is only defined for use with the Java development platform. If you are keen on using the RMI implementation for distributed application development in other languages, COBRA is a substitute that is not tied to a single platform.

 

3. What is the difference between RMI and RPC?

Ans: Remote Procedure Call(RPC) is similar to RMI in functionality with some key differences.

 

4. What are the advantages of using RMI?

Ans: Following are some key advantages of using RMI applications.

  • It is object-oriented and can pass full objects as arguments and return values instead of predefined data types.
  • It is safe with built-in Java security mechanisms.
  • RMI is easy to write and implement.
  • It is system compatible. So once you write the code, you can use it anywhere.

Conclusion

This article extensively discussed Remote Method Invocation and its implementation, architecture, and functionality in Java. We still haven't covered all the possibilities and methods of running and executing RMI applications. So keep on learning with coding ninjas.

We hope that this blog has helped you enhance your knowledge regarding RMI applications in Java, and if you would like to learn more, check out our articles on Basics of JavaGetting Started with Java, and Learn OOPS. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass