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();
name.put(1,"Ganesh");
name.put(2,"jazz");
}
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.


Server


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 
                                                                                           name(storage)
System.out.println("Clients can invoke the methods....");
}
}

Client


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 .  



26 comments:

Unknown 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 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?

Unknown said...
This comment has been removed by a blog administrator.
Vivek Reddy said...

I would say while reading your article, i felt very proud. Because the information you written very useful, please keep posting this type of articles. If you guys looking for a Advanced Java Training in Bangalore. please click below link.

Best Institute For Java in Marathahalli Bangalore

Infocampus said...

I really like all the points you made.
Web Designing Training In Bangalore

Web Design Courses In Bangalore

Java Training In Bangalore

iOS Training In Bangalore

Unknown said...

I quite impressed with your java concepts and questionnaire, we would happy to go through these concepts and we also train on IT technologies such as Java Training in Marathahalli Bangalore. Kindly visit our site for free demo.

Dipanwita said...

Loved this blog. Very useful and interesting. java training in chennai

Home LIft said...

very informative message and it is very useful. best java training in chennai

Home LIft said...

Nice post.. keep sharing.. java training in chennai | java training center in chennai | java training institute in chennai

Praylin S said...

Great work! Really nice post.. Keep us updated.
Placement Training in Chennai
Placement Training institutes
Placement Training Centres in Chennai
Unix Training in Chennai
Tally Institute in Chennai
Advanced Excel Training in Chennai

SeoIndhu said...

Big thumbs up for making such a wonderful blog with great content.
wifi camera in chennai |

Fire extinguisher in chennai |

Fire alarm systems in chennai

SeoIndhu said...

Great explanation, your article inspired me the most. Thanks for sharing.....
Automatic water level controller in OMR |

Water level indicator in coimbatore |

Automatic water level controller in coimbatore

SeoIndhu said...

Thanks for your post. This is excellent information. The list of your blogs is very helpful for those who want to learn, It is amazing!!! You have been helping many application.....
Garments Rack in Chennai |

Four sided racks in Chennai |

Supermarket rack in Chennai

Sivag said...

Thanks for sharing a nice Blog. Very helpful Information. I got Knowledge by reading your blog. Keep updating more....
Spoken english training in chennai |

Tally training in chennai |

Tally training in vadapalani

Anonymous said...

Service Center iPhone Bandar Lampung
Service HP Pringsewu LampungYoutuber Lampung
Service HP Pringsewu LampungService Center Acer Indonesian
Lampung ServiceService Center Apple

manisha said...

Thanks for sharing this valuable information and we collected some information from this blog.
Java Training in Noida

Nisha San said...

Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai

shiny said...

I am requesting you to post many useful blogs like this. Your blog has given me a chance to fulfill my potential. Web Designing Course Training in Chennai | Web Designing Course Training in annanagar | Web Designing Course Training in omr | Web Designing Course Training in porur | Web Designing Course Training in tambaram | Web Designing Course Training in velachery

shiny said...
This comment has been removed by the author.
aravind said...

Great post! I am actually getting ready to across this information, It’s very helpful for this blog. Also great with all of the valuable information you have Keep up the good work you are doing well.Thanks a lot for sharing such a good source with us
DevOps Training in Chennai

DevOps Online Training in Chennai

DevOps Training in Bangalore

DevOps Training in Hyderabad

DevOps Training in Coimbatore

DevOps Training

DevOps Online Training

lavanya said...

Although there is a lot of discussion about the future of PHP, it is clear that PHP does have a future. It is by far the most used programming language for websites. ... If you want to start a career as a PHP developer you won't run out of options when it comes to finding a job
Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

anand said...

amazing post
Software Testing Training in Chennai | Certification | Online
Courses

Software Testing Training in Chennai

Software Testing Online Training in Chennai

Software Testing Courses in Chennai

Software Testing Training in Bangalore

Software Testing Training in Hyderabad

Software Testing Training in Coimbatore

Software Testing Training

Software Testing Online Training

radhika said...

Thanks for sharing a nice Blog. Very helpful Information. I got Knowledge by reading your blog. Keep updating more....


AWS Course in Chennai

AWS Course in Bangalore

AWS Course in Hyderabad

AWS Course in Coimbatore

AWS Course

AWS Certification Course

AWS Certification Training

AWS Online Training

AWS Training

vivekvedha said...

Thanks for sharing a nice Blog. Very helpful Information. I got Knowledge by reading your blog. Keep updating more....
acte reviews

acte velachery reviews

acte tambaram reviews

acte anna nagar reviews

acte porur reviews

acte omr reviews

acte chennai reviews

acte student reviews

swaroop said...

very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.

Web Designing Training in Bangalore

Web Designing Course in Bangalore

Web Designing Training in Hyderabad

Web Designing Course in Hyderabad

Web Designing Training in Coimbatore

Web Designing Training

Web Designing Online Training

sanjay said...

Useful content, I have bookmarked this page for my future reference.
Cyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course |
CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course

Post a Comment

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