Remote Method Invocation(RMI)

Shalika Prasad
3 min readJul 17, 2020

--

It acts as an interface related to java. We can tell that is an API as well as it provides a mechanism or a way to create your own distributed application using Java. We can invoke remote methods on running in another JVM. It provides a remote communication or bridge among the applications. As well as it has 2 objects called stub and skeleton.

Lets we discuss what is the architecture of this API.

It has two major parts that we want to discuss. There are stub and skeleton. But if you see RPC, you can see just stub on both sides. But, in RMI you can see as two objects that is act as a gateway. These have two different tasks.

Example:

We suppose If the client has 2 numbers to add, divide, subtract like that. The client sends that 2 numbers to the server and gets the result as the reply from the server.

  1. Let's see how to create CalculatorService class,

We want to import the Remort interface given by RMI. Using this Remort Interface, It makes a connection to access remort computer. Then the client can connect this server.

import java.rmi.Remote;

After we want to create a Remote Exception. This exception helps to identify any failure during communication.

import java.rmi.RemoteException

Now we create the interface. If any failure comes when doing that method, it throws an exception.

2. Let's see how to create the implementation part of above interface.

If the client sends non-legal or non-accessibility URL to the server, we can get an output as a Malformed URL Exception.

import java.net.MalformedURLException;

We know after creating the interface, we bind the objects. Thus, the client can call the interface to execute the needed things. After we bind, if we try to again bind that interface, it can see it as an Already Bound Exception.

import java.rmi.AlreadyBoundException;

Using the RMI Naming class, you can store references of objects. That means all references store in the remote object registry.

import java.rmi.Naming;

Using RMI Security Manager, we can handle all of the security policies.

import java.rmi.RMISecurityManager;

Using Locate Registry, It helps you to find the corresponding registry.

import java.rmi.registry.LocateRegistry;

Using Unicast Remote Object, when client request, we don’t want multicast, because, one client request one method. So, we want to Unicast not multicast. This library uses to export the remote method to the client.

import java.rmi.server.UnicastRemoteObject;

Now, create the implementation class:

3. Now, we see how to create the Main method to run the server.

After we want to create stub and skeleton objects. For that, we can use RMI compiler.

rmic CalculatorServiceImpl

4. Let's see Client-side

We want the Not Bound Exception to identify whether bound or not.

import java.rmi.NotBoundException;

Client Class:

Conclusion

I think that you learn more about RMI.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response