Wednesday 1 July 2015

RMI - Addition of 2 numbers example


There are 6 steps to write an RMI program,
  1. Create the remote interface
  2. Provide the implementation of the remote interface
  3. Compile the implementation class and create the stub and skeleton objects using the rmic tool
  4. Start the registry service by rmiregistry tool
  5. Create and start the remote application
  6. Create and start the client application
In this example, we have followed all the 6 steps to create and run the rmi application. The client application need only two files, remote interface and client application. In the rmi application, both client and server interacts with the remote interface. The client application invokes methods on the proxy object, RMI sends the request to the remote JVM. The return value is sent back to the proxy object and then to the client application.
RMI example

1) create the remote interface

For creating the remote interface, extend the Remote interface and declare the RemoteException with all the methods of the remote interface. Here, we are creating a remote interface that extends the Remote interface. There is only one method named add() and it declares RemoteException.
  1. import java.rmi.*;  
  2. public interface Adder extends Remote{  
  3. public int add(int x,int y)throws RemoteException;  
  4. }  

2) Provide the implementation of the remote interface

Now provide the implementation of the remote interface. For providing the implementation of the Remote interface, we need to
  • Either extend the UnicastRemoteObject class,
  • or use the exportObject() method of the UnicastRemoteObject class
In case, you extend the UnicastRemoteObject class, you must define a constructor that declares RemoteException.
  1. import java.rmi.*;  
  2. import java.rmi.server.*;  
  3. public class AdderRemote extends UnicastRemoteObject implements Adder{  
  4. AdderRemote()throws RemoteException{  
  5. super();  
  6. }  
  7. public int add(int x,int y){return x+y;}  
  8. }  

3) create the stub and skeleton objects using the rmic tool.

Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool invokes the RMI compiler and creates stub and skeleton objects.
  1. rmic AdderRemote  

4) Start the registry service by the rmiregistry tool

Now start the registry service by using the rmiregistry tool. If you don't specify the port number, it uses a default port number. In this example, we are using the port number 5000.
  1. rmiregistry 5000  

5) Create and run the server application

Now rmi services need to be hosted in a server process. The Naming class provides methods to get and store the remote object. The Naming class provides 5 methods.
  1. public static java.rmi.Remote lookup(java.lang.String) throws java.rmi.NotBoundException, java.net.MalformedURLException, java.rmi.RemoteException; it returns the reference of the remote object.
  2. public static void bind(java.lang.String, java.rmi.Remote) throws java.rmi.AlreadyBoundException, java.net.MalformedURLException, java.rmi.RemoteException; it binds the remote object with the given name.
  3. public static void unbind(java.lang.String) throws java.rmi.RemoteException, java.rmi.NotBoundException, java.net.MalformedURLException; it destroys the remote object which is bound with the given name.
  4. public static void rebind(java.lang.String, java.rmi.Remote) throws java.rmi.RemoteException, java.net.MalformedURLException; it binds the remote object to the new name.
  5. public static java.lang.String[] list(java.lang.String) throws java.rmi.RemoteException, java.net.MalformedURLException; it returns an array of the names of the remote objects bound in the registry.
In this example, we are binding the remote object by the name sonoo.
  1. import java.rmi.*;  
  2. import java.rmi.registry.*;  
  3. public class MyServer{  
  4. public static void main(String args[]){  
  5. try{  
  6. Adder stub=new AdderRemote();  
  7. Naming.rebind("rmi://localhost:5000/sonoo",stub);  
  8. }catch(Exception e){System.out.println(e);}  
  9. }  
  10. }  

6) Create and run the client application

At the client we are getting the stub object by the lookup() method of the Naming class and invoking the method on this object. In this example, we are running the server and client applications, in the same machine so we are using localhost. If you want to access the remote object from another machine, change the localhost to the host name (or IP address) where the remote object is located.
  1. import java.rmi.*;  
  2. public class MyClient{  
  3. public static void main(String args[]){  
  4. try{  
  5. Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");  
  6. System.out.println(stub.add(34,4));  
  7. }catch(Exception e){}  
  8. }  
  9. }  

  1. For running this rmi example,  
  2.   
  3. 1) compile all the java files  
  4.   
  5. javac *.java  
  6.   
  7. 2)create stub and skeleton object by rmic tool  
  8.   
  9. rmic AdderRemote  
  10.   
  11. 3)start rmi registry in one command prompt  
  12.   
  13. rmiregistry 5000  
  14.   
  15. 4)start the server in another command prompt  
  16.   
  17. java MyServer  
  18.   
  19. 5)start the client application in another command prompt  
  20.   
  21. java MyClient  

Output of this RMI example

RMI RMI

Example source code,

import java.rmi.*;

public class MyClient{

public static void main(String args[]){
try{

Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
System.out.println(stub.add(34,4));

}catch(Exception e){System.out.println(e);}
}

}

import java.rmi.*;
import java.rmi.server.*;

public class AdderRemote extends UnicastRemoteObject implements Adder{

AdderRemote()throws RemoteException{
super();
}

public int add(int x,int y){return x+y;}

}


import java.rmi.*;
public interface Adder extends Remote{

public int add(int x,int y)throws RemoteException;
}


import java.rmi.*;
import java.rmi.registry.*;

public class MyServer{

public static void main(String args[]){
try{

Adder stub=new AdderRemote();
Naming.rebind("rmi://localhost:5000/sonoo",stub);

}catch(Exception e){System.out.println(e);}
}

}




No comments:

Post a Comment