Tuesday, December 1, 2015

RMI IN JAVA

RMI (Remote Method Invocation)
1.    stub
2.    skeleton
The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM.
The RMI provides remote communication between the applications using two objects stub and skeleton.

Understanding stub and skeleton

RMI uses stub and skeleton object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM. Let's understand the stub and skeleton objects:

stub

The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed through it. It resides at the client side and represents the remote object. When the caller invokes method on the stub object, it does the following tasks:
  1. It initiates a connection with remote Virtual Machine (JVM),
  2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
  3. It waits for the result
  4. It reads (unmarshals) the return value or exception, and
  5. It finally, returns the value to the caller.

skeleton

The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are routed through it. When the skeleton receives the incoming request, it does the following tasks:
  1. It reads the parameter for the remote method
  2. It invokes the method on the actual remote object, and
  3. It writes and transmits (marshals) the result to the caller.
In the Java 2 SDK, an stub protocol was introduced that eliminates the need for skeletons.

Steps to write the RMI program

The is given the 6 steps to write the 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

1) create the remote interface


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
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

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

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  

No comments:

Post a Comment

JAC Board Computer || IIT 2025 || Introductory Information Technology || Class 10 || Practical Examination 2025

Marks Division HOE 30 Marks IT Application Report File 20 Marks Viva - Voce 10 Marks 1) Write an HTML code to generate a web p...