Add to Google Reader or Homepage

Java RMI Tutorial

How to perform Remote Method Invocation in java

What is meant  by RMI?

Remote Method Invocation refers to invoking a method on an object which resides in a remote machine.For example, let's take the client/server model. In which the server is  usually a remote machine and the client invokes a method of an object which resides in the server to accomplish the required task.

Let us see what are the components needed  by us to perform  the RMI,

Server - A remote machine which serves us the requested operation.

Client - A machine that requests the server to perform some tasks on its behalf.

RMI Registry

In general, if you want to invoke a method of a class what do you actually need? an object of that class. If the class is available in your machine, then there is no problem for you to create an object of that class and invoke the method.

But in RMI the class for which you would like to create an object is not in your machine it is available in the remote machine i.e, the server. So you would need some means to get the object of that class(in the server). What to do? RMI registry has been created for this purpose.

Consider RMI registry as a book where in the objects needed by the clients are registered by the server with a name (in our case it is a RMI url) ,So if any clients needs  an object, it will request the RMI registry(book) with a name, then the RMI registry provides the object corresponding to that name to the client. Now client can access the methods.

 Stub - I would consider a stub as a cached copy of the remote object in the client machine. What is a remote object? An object of a class that implements the Remote Interface is called remote object.  

what do you meant by Remote Interface? An interface that extends the Remote Interface of the java.rmi package

Why should i need to extend the remote interface?

If you want to make a method to be remotely accessible then you should include this method in the interface that extends the Remote Interface.

For example,

import java.rmi.*;
public interface NameCollections extends Remote //Remote interface
public String getName(int index)throws RemoteException;//Remotely 
                                                                                                                                   accessible method

 A class that implements the Remote interface is like as follows:

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

class NameStorage extends UnicastRemoteObject implements NameCollections //A
                                                                                                    class that implements the remote interface
private Map name;
public NameStorage()throws RemoteException

name=new HashMap();
public String getName(int index)throws RemoteException
String str=name.get(new Integer(index));
return str;

 NameStorage name=new NameStorage(); //Here name is the remote object

Skeleton - It is exactly similar to the stub but it resides in the server machine, After java 1.2 the skeleton has been omitted. So let's leave this component.


import java.rmi.*;
import javax.naming.*;
class NameServer 
public static void main(String args[])throws RemoteException,NamingException
Context ictxt=new InitialContext();
NameStorage store=new NameStorage();
ictxt.bind("rmi:Storage",store);//Binding the remote object(store) with a 
System.out.println("Clients can invoke the methods....");


import java.rmi.*;
import java.util.*;
import javax.naming.*;
class NameClient
public static void main(String args[])throws NamingException,RemoteException
Context itxt=new InitialContext();
//Searching for the remote object in the RMI registry  
NameCollections coll=(NameCollections)itxt.lookup("rmi://localhost/Storage");
String n1=coll.getName(2);// Invoking the remote method
System.out.println("Name Obtained from the server:"+n1);

Though it appears as invoking the remote method, it doesn't actually invoke the remote method, then what? It actually invokes the method on the stub object(which is available in the client machine).

This stub object would in turn  call the remote method which is available in the server and forwards the return value from the server to  the  client.

Since it is a lengthy post, i have dealt with the execution of the RMI program in another
 post How to execute a RMI program .  


sarabjeet said...

i really like that you are giving information on core and advance java concepts. Being enrolled at i found your information very helpful indeed.thanks for it.

Sean K said...

It seems that RMI is not actively supported by Oracle-JavaSoft. The user-group email is not responding, and the archive is not accessible.

I have a problem where the RMI Naming.lookup does not work when the url passed to lookup is a IPv6 (rmi://[IPv6 address]:4242/Controller

Ganesh Bhuddhan said...

What is the error message? that you encountered,when you try to look up in the url (i.e,IPv6 address).. and have you tried it with IPv4 address?

Hima P said...
This comment has been removed by a blog administrator.
vijay kumar said...

NICE BLOG!!! Thanks for sharing useful information about FNT Softwre Solutions and being one of best Software Training institute in Bangalore we agree that this blog is very useful for the students who are searching for best software courses, I would really like to come back again right here for like wise good articles or blog posts. Thanks for sharing...Java training company bangalore.

Post a Comment

java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top