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.
Next: Conclusion >>
More Java Articles
More By Neville Mehta