Skip to content


Remote method invocation

In distributed computing, client-server communication takes place between objects. In CORBA, two different objects can communicate with each other. But,If you’re programming in java, then place where your objects reside are part of JVM. So what if you need to communicate with the objects in two different JVM’s. In this case RMI is the solution.
What is RMI?
RMI stands for remote method invocation. Object-to-object communication between different java virtual machines (JVM) can be performed using remote method invocation. So the java program in one machine can communicate with one ore more objects residing in different JVM’s. As RMI represents the high-level view of networking, it internally uses sockets.

RMI Architecture
RMI architecture is as shown in the figure below:


Stub & Skeletons: Stub,Skeletons are .class files,which are created after “rmic” compilation. Let’s see the role of stub & skeleton in short.
1. Stub : Stub is client-side proxy representing remote object.It looks like local object on client.Role of stub is marshalling as well as demarshalling.

2. Skeleton: Skeleton is the server-side construct that interface with server & server-side remote reference layer.

Remote Reference Layer
For remote reference layer & transport layer only marshaled data is passed or traveled.

Remore refernce layer deals with the low-level transport interface. This layer makes use of “JRMP” (java remote method protocol) protocol.It is also responsible for providing a stream to the stub & skeleton layers.

It sets up the connections to remote address spaces, manage connections and understands how to interpret and manage reference made from clients to the remote service objects.

Transport Layer
It provides the basic connectivity across the network. It is responsible for the Client-server connectivity as well as firewall penetration strategies.

RMI Registry
RMI registry is a program that maintains the table of object and references. RMI registry is accessed by server to insert the remote object entry. RMI registry is accessed by client to retrieve the remote object entry.
Steps to Write RMI application

Step 1 : Write remote interface
Step 2 : Write implementation of remote interface
Step 3 : Write Server
Step 4 : Compiler using (javac & rmic)
Step 5 : Run rmiregistry
Step 6 : Run Server  
Let’s understand this concept with the help of example.Take a look at the sourceode below:

Remore interface Java File: (helloremote.java)

import java.rmi.*;
 
public interface helloremote extends Remote
{
public String sayhello() throws RemoteException;
}

Remote Implementation Java File : (hellpimpl.java)

import java.rmi.server.*;
 
public helloimpl extends UnicastRemoteObject implements helloremote
{
public helloimpl() throws RemoteException
{
System.out.println(“Implementation Constructor);
}
 
public string sayhello() throws RemoteException
{
return”hello”;
}

Server java File (helloserver.java)

import java.rmi.*;
 
public class helloserver
{
public static void main(String[] args) throws Exception
{
helloimpl h1= new helloimpl();
Naming.rebind(“obj”,h1);
System.out.println(“Server Initilised”);
}
}

Code explaination: Naming.rebind() method inserts the server side object into the rmi registry.

Client java file ( helloclient.java)

import java.rmi.*;
 
public class helloclient
{
public static void main(String[] args)
{
System.out.println(“Client Initilised”);
helloremote hremote=(helloremote)Naming.lookup(“rmi://localhost/obj”);
System.out.Println(hremote.sayhello());
}
}

Code explaination: Naming.lookup() method retrieves the object from the RMI registry to call the method. Here we are trying this example on our own machine,if you want to try it in computer in network you can do so. RMI can use the specific ports ranging from 1024 to 65535.

Compilation Steps:
Let’s see the steps we require to compile & run the RMI application.

Step 1: javac *.java  // compile aa the java files
Step 2: rmic helloimpl  //compile the remote interface to create stub & skeleton
Step 3: start rmiregistry  //start the rmiregistry
Step 4: start  // this will open another command prompt at client
Step 5: java helloserver // this will execute the server
Step 6: java helloclient//this will excute client and will excute the whole RMI app

If you’ve executed the application and see the hello message in that case, congratulations you’ve working RMI app.

After reading this article you know what is RMI and how to create simple RMI application. There are plenty of other advanced topics,you’ve to explore like RMI-IIOP etc.

I hope the information above helped. If you’ve any questions or suggestions then please don’t hesitate to post them here.


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Litty says

    great & simple explanation! with visual presentation…



Some HTML is OK

or, reply to this post via trackback.