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).
Next: The Coding (Contd.) >>
More Java Articles
More By Neville Mehta