Java
  Home arrow Java arrow Page 4 - An Introduction To RMI With Java
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

An Introduction To RMI With Java
By: Neville Mehta
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 107
    2002-06-24

    Table of Contents:
  • An Introduction To RMI With Java
  • The Concept
  • What Do We Have To Code?
  • The Coding (Contd.)
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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();
    Naming.rebind("addnumbers",add);
    }
    }


    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://127.0.0.1/addnumbers";
    AddServer add;
    add = (AddServer)Naming.lookup(url);
    int result = add.AddNumbers(10,5);
    System.out.println(result);
    }
    }


    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.

    More Java Articles
    More By Neville Mehta


       · I found it good enough. Well, reders of this article must actually question Neville,...
       · Instead of having the client throw the RemoteException, it would be better if the...
       · It is good practice to have each client connect to the server in it's own thread;...
     

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek