Java/J2EE and SOA (346 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (17 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (343 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is Remote Method Invocation in Java?

Published on Sep 06,2019 7K Views

A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.
11 / 22 Blog from Advance Java

Remote Method Invocation is a way that a programmer makes use of Java programming language and its development environment remotely. Its all about how the objects on different computers interact in a distributed network. In this article on Remote Method Invocation in Java, I will tell you how to create an RMI application across Client and Server.

Below topics are covered in this article:

Let’s get started!

What is RMI in Java?

The RMI (Remote Method Invocation) is an API that provides a mechanism to create a distributed application in Java. The RMI allows an object to invoke methods on an object running in another JVM. The Remote Method Invocation provides remote communication between the applications using two objects stub and skeleton.

Understanding Stub and Skeleton

The stub object on the client machine builds an information block and sends this information to the server. The block consists of:

  • An identifier of the remote object to be used
  • Method name which is to be invoked
  • Parameters to the remote JVM

Stub and Skeleton - RMI in Java - EdurekaSkeleton Object

The skeleton object passes the request from the stub object to the remote object. It performs the following tasks:

  • It calls the desired method on the real object present on the server.

  • It forwards the parameters received from the stub object to the method.

With this, let move further and see how to create an RMI Application

Steps to create RMI Application

Below steps helps you to create RMI Application:

  1. Defining a remote interface
  2. Implementing the remote interface
  3. Creating Stub and Skeleton objects from the implementation class using RMIC (RMI complier)
  4. Start the RMI registry
  5. Create and execute the server application program
  6. Create and execute the client application program

Now, let’s get into the details of these steps.

Step 1: Defining a remote interface

The first thing that we have to do is to create an interface. This will provide a description of the methods that can be invoked by remote clients. This interface should extend the Remote interface and the method prototype within the interface should throw the RemoteException.

// Creating a Search interface
import java.rmi.*;
public interface Search extends Remote{
// Declaring the method prototype
public String Query(String search) throws RemoteException;
}

Step 2: Implementation of remote interface

The next step is to implement the remote interface. In order to implement the remote interface, the class should extend to the UnicastRemoteObject class of java.rmi package. Also, a default constructor needs to be created to throw the java.rmi.RemoteException from its parent constructor.

// Java program to implement the Search interface
import java.rmi.*;
import java.rmi.server.*;
public class SearchQuery extends UnicastRemoteObject implements Search{
// Default constructor to throw RemoteException from its parent constructor
SearchQuery() throws RemoteException{
super();
}// Implementation of the query interface
public String query(String search) throws RemoteException{
String result;
if (search.equals("Reflection in Java"))
result = "true";
else
result = "false";
return result;
}
}

Step 3:  Creating Stub and Skeleton objects from the implementation class using rmic

The RMIC tool is used to invoke the RMI compiler that creates the Stub and Skeleton objects. Its prototype is RMIC class name.

STEP 4: Start the RMIregistry
You need to start the registry service by issuing the command at the command prompt start RMIregistry

STEP 5: Create and execute the server application program
The next step is to create the server application program and execute it on a separate command prompt.

  • The server program uses the createRegistry method of LocateRegistry class to create rmiregistry within the server JVM with the port number passed as an argument.

  • The rebind method of Naming class is used to bind the remote object to the new name.

//program for server application
import java.rmi.*;
import java.rmi.registry.*;
public class SearchServer{
public static void main(String args[]){
try{
// Create an object of the interface implementation class
Search obj = new SearchQuery();

// rmiregistry within the server JVM with
// port number 1900
LocateRegistry.createRegistry(1900);
<p style="text-align: justify;">// Binds the remote object by the name
//edureka
Naming.rebind("rmi://localhost:1900"+
"/edureka",obj);
}
catch(Exception ae){
System.out.println(ae);
}
}
}

Step 6: Create and execute the Client Application program
The last step is to create the Client Application program and execute it on a separate command prompt. The lookup method of Naming class is used to get the reference of the Stub object

The above client and server program is executed on the same machine and that’s why localhost is being used. In order to access the remote object from another machine, localhost is to be replaced with the IP address where the remote object is present.

So this brings us to the end of the RMI in Java article. I hope you found it informative and helped you in understanding the Fundamentals.

Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “RMI in Java ” article and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDate
Java Certification Training Course

Class Starts on 28th January,2023

28th January

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th February,2023

25th February

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Remote Method Invocation in Java?

edureka.co