Java
  Home arrow Java arrow Page 3 - 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 - What Do We Have To Code?


    (Page 3 of 5 )

    Now, before we jump into the code, let me tell you exactly what we have to code:
    • The Remote Object: This interface (yes our remote object is actually an interface) will have only method declarations. Hopefully you know that interfaces don’t always have to have method bodies, just declarations. The Remote Object will have a declaration for each method that you want to export. This remote object would implement the Remote interface, which is present in the Java.rmi package.
    • The Remote Object Implementation: This is a class that implements the Remote Object. If you implement the Remote Object, it's common sense that you would override all of the methods in that object, so the remote object implementation class would actually have all of the method bodies of the methods that we want to export. This method will be extended from the UnicastRemoteObject class.
    • The Remote Server: This is a class that will act like a server to the client wanting to access remote methods. Here's the place where you will bind any string with the object you want to export. The binding process will be taken care of in this class.
    • The Remote Client: This is a class that will help you access the remote method. This is the end user, the client. You will call the remote method from this class. You will use methods to search and invoke that remote method.
    Ok enough of the theory for now. Let's jump into the fun part: the coding...

    The Coding
    We will start by coding the remote object. Save this code as AddServer.Java in your favorite text editor:

    import Java.rmi.*;

    public interface AddServer extends Remote {

    public int AddNumbers(int firstnumber,int secondnumber) throws RemoteException;

    }


    Let's have a look at this code. First of all we import the rmi package to use its contents. We then create an interface that extends the remote interface present in the Java.rmi package. All remote objects must extend the remote interface. We call this remote object AddServer. We have a method called AddNumbers in this remote object, which could be called by a client. We must remember that all remote methods need to throw the RemoteException, which is called whenever some error occurs.

    We will now start coding the remote object implementation. This is the class that would implement the remote object and contain all of the method bodies. Save this code as AddServerImpl.Java in your favorite text editor:

    import Java.rmi.*;

    public class AddServerImpl extends UnicastRemoteObject implements AddServer {
    public AddServerImpl() {
    super();
    }
    public int AddNumbers(int firstnumber,int secondnumber) throws RemoteException {
    return firstnumber + secondnumber;
    }
    }


    First of all we import the rmi package to use its contents. We then create a class that extends the UnicastRemoteObject and implements out remote object that we have created. Next, we create a default constructor for our class, which with the help of the super keyword calls the constructor of the base class UnicastRemoteObject. We also see the body for the AddNumbers method, which throws a RemoteException. This way we are actually overriding the method in the remote object that we have created. The method body should be quite self-explainatory. We are receiving two integer parameters, adding them both and then returning their sum.

    At this point we have two Java files: the remote object and the remote object implementation. We will now compile both of these file using the Javac command, like this:

    Compile Remote Object:
    C:\jdk\bin\Javac workingdir\AddServer.Java

    Compile Remote Object Implementation:
    C:\jdk\bin\Javac workingdir\AddServerImpl.Java

    We end up with two Java files and two class files. We are now going to create our stub and skeleton. For creating the stub and skeleton files we have to use the rmic compiler on the remote object implementation file.

    Rmic Compile Remote Object Implementation:
    C:\jdk\bin\rmic workingdir\AddServerImpl.Java

    After following the above step you will see that two newer class files have been created. They are AddServerImpl_Stub.class (the stub which will reside on the client side) and AddServerImpl_Skel.class (the skeleton which will reside on the server side).

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek