Home arrow Java arrow Page 4 - An Introduction To RMI With Java

An Introduction To RMI With Java

Remote Method Invocation (or RMI for short) allows us to execute methods on remote servers. In this article Nevile introduces us to RMI with a simple example and fundamentals in Java.

Author Info:
By: Neville Mehta
Rating: 4 stars4 stars4 stars4 stars4 stars / 142
June 24, 2002
  1. · An Introduction To RMI With Java
  2. · The Concept
  3. · What Do We Have To Code?
  4. · The Coding (Contd.)
  5. · Conclusion

print this article

An Introduction To RMI With Java - The Coding (Contd.)
(Page 4 of 5 )

Since we have the entire source compiled, let's create our client and server to see this demo in action. Save this code as RmiServer.Java in your favorite text editor:

import Java.rmi.*;
import Java.net.*;

public class RmiServer {
public static void main (String args[]) throws RemoteException, MalformedURLException {
AddServerImpl add = new AddServerImpl();

Firstly we import the Java.rmi package and the Java.net package. We also use the throws clause to catch any necessary exceptions. Here we make an object out of the remote object implementation and on the very next line we use the rebind method to bind this object with the string addnumbers. This example shows exactly what I mean by binding.

From now on, whenever the client wants to make a reference to the remote object it would use the string addnumbers to do so. The rebind method takes two parameters: first, the string variable and second the object of the remote object implementation class.

Let's create the client. Save this code as RmiClient.Java in your favorite text editor:

import Java.rmi.*;
import Java.net.*;

public class RmiClient {
public static void main(String args[]) throws RemoteException, MalformedURLException {
String url="rmi://";
AddServer add;
add = (AddServer)Naming.lookup(url);
int result = add.AddNumbers(10,5);

First of all we import the Java.rmi package and the Java.net package. We also use the throws clause to catch all necessary exceptions. We then somehow try to make an object out of the remote object by using the lookup method in the Naming class, which is a Static method (that's the reason why we don't need to make an object of the Naming class and call it, we just use the class name).

The lookup method takes the complete URL name of the remote object, which consists of the complete machine I.P. address and the string which the object is associated to. This is the binding name of the object. As you can see, we use the RMI protocol to reference the remote object. This lookup method returns an object to us that we will have to typecast to the remote object data type before it can be used. Typecasting is a process through which we convert data from one data type to the other.

Since we have both our server and client source ready, let's compile them both:

Compile Remote Server:
C:\jdk\bin\Javac workingdir\RmiServer.Java

Compile Remote Client:
C:\jdk\bin\Javac workingdir\RmiClient.Java

Before we start testing our code we have to start the RMI Registry. The RMI registry is the place where all the bound data will be stored. Without it RMI wouldn't work!

Start Rmi Registry Server:
C:\jdk\bin\start rmiregistry

You will notice a new blank DOS prompt window will open. That's the RMI registry running. Make sure you leave it open as it is. Just like client server code, we first run the Rmi server in one DOS Prompt and then the RMI client in the other.

Start RMI Server:
C:\jdk\bin\Java workingdir\RmiServer

Start RMI Client:
C:\jdk\bin\Java workingdir\RmiClient

If everything worked out well then you should get the output of 15. We passed two integer values 10 and 5 to the AddNumbers method, which added them and returned an integer 15 back to us. If you got 15 then you have successfully executed a remote method. Of course, its not remote in the true sense as in this case you are the client and the server, but if you have a network then you can easily try this remotely.
blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials