Introduction To RMI<br />By<br />Swarup Kulkarni<br />(MCA-II, VIT, Pune)<br />
Remote Method Invocation<br />RMI - Remote Method Invocation<br />Allows to invoke a method of a Java object that executes...
Step 1: Enter and Compile the Source Code<br />We use four source files -<br />First source file:<br />RmiDemoIntf.java<br...
Step 1: (Continued..)<br />The first source file - Code:<br />RmiDemoIntf.java<br />import java.rmi.*;<br />public interfa...
Step 1: (Continued..)<br />The second source file:<br />RmiDemoImpl.java<br />Implements Remote interface<br />implementat...
Step 1: (Continued..)<br />The second source file - Code:<br />RmiDemoImpl.java<br />import java.rmi.*;<br />import java.r...
Step 1: (Continued..)<br />The third source file:<br />RmiServer.java<br />Contains main program for the server machine<br...
First argument names server as “RmiServer”
Second argument is reference to object of  RmiDemoImpl</li></ul>3/11/2009<br />7<br />RMI - Swarup Kulkarni<br />
Step 1: (Continued..)<br />The third source file - Code:<br />RmiServer.java<br />import java.net.*;<br />import java.rmi....
Step 1: (Continued..)<br />The forth source file:<br />RmiClient.java<br />Implements client side of this distributed appl...
Step 1: (Continued..)<br />The forth source file - Code:<br />RmiClient.java<br />import java.rmi.*;<br />public class Rmi...
Step 1: (Continued..)<br />RmiClient.java(Contd..)<br />System.out.println(&quot;The first number is: &quot; + d1);<br />S...
Step 2: Generate Stubs and Skeletons<br />Before using  client and  server we have to generate stub and skeleton.<br />Stu...
Step 2: (Continued..)<br />How to generate stub and Skeleton?<br />Use a tool called RMI Compiler, which is invoked from c...
Step 3: Install Files on Client and Server<br />Copy  RmiDemo.class, RmiDemoImpl_Stub.class, and RmiDemoImpl.class on the ...
Step 4: Start the RMI Registry on the Server<br />The Java 2 SDK provides a program called rmiregistry, which executes on ...
Upcoming SlideShare
Loading in …5
×

Introduction To Rmi

1,837 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,837
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
63
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction To Rmi

  1. 1. Introduction To RMI<br />By<br />Swarup Kulkarni<br />(MCA-II, VIT, Pune)<br />
  2. 2. Remote Method Invocation<br />RMI - Remote Method Invocation<br />Allows to invoke a method of a Java object that executes on another machine.<br />Important feature, because it allows to build distributed applications.<br />Lets discus a simple Client/Server application using RMI.<br />The server receives a request from client and processes it<br />In this req. two numbers are specified<br />Server add these numbers and returns it<br />3/11/2009<br />2<br />RMI - Swarup Kulkarni<br />
  3. 3. Step 1: Enter and Compile the Source Code<br />We use four source files -<br />First source file:<br />RmiDemoIntf.java<br />Defines remote interface, a remote method,<br />All remote interfaces must inherit Remote interface<br />Remote interface is a part of java.rmi.remote<br />Defines no members ,<br />purpose is to indicate that interface uses remote methods.<br />All remote methods can throw RemoteException<br />3/11/2009<br />3<br />RMI - Swarup Kulkarni<br />
  4. 4. Step 1: (Continued..)<br />The first source file - Code:<br />RmiDemoIntf.java<br />import java.rmi.*;<br />public interface RmiDemoIntf extends Remote {<br /> double add(double d1, double d2) throws RemoteException;<br />}<br />3/11/2009<br />4<br />RMI - Swarup Kulkarni<br />
  5. 5. Step 1: (Continued..)<br />The second source file:<br />RmiDemoImpl.java<br />Implements Remote interface<br />implementation of the add( ) method is straightforward<br />All remote objects must extend UnicastRemoteObject<br />UnicastRemoteObject<br />provides functionality that is needed to make objects available from remote machines<br />3/11/2009<br />5<br />RMI - Swarup Kulkarni<br />
  6. 6. Step 1: (Continued..)<br />The second source file - Code:<br />RmiDemoImpl.java<br />import java.rmi.*;<br />import java.rmi.server.*;<br />public class RmiDemoImpl extends UnicastRemoteObject<br /> implements RmiDemoIntf{<br />public double add(double d1, double d2) throws RemoteException {<br />return d1 + d2;<br />}<br />}<br />3/11/2009<br />6<br />RMI - Swarup Kulkarni<br />
  7. 7. Step 1: (Continued..)<br />The third source file:<br />RmiServer.java<br />Contains main program for the server machine<br />This modifies the RMI registry of the machine using rebind() method of Naming class(found in java.rmi)<br />Naming.rebind()<br /><ul><li>Associates name with the object reference
  8. 8. First argument names server as “RmiServer”
  9. 9. Second argument is reference to object of RmiDemoImpl</li></ul>3/11/2009<br />7<br />RMI - Swarup Kulkarni<br />
  10. 10. Step 1: (Continued..)<br />The third source file - Code:<br />RmiServer.java<br />import java.net.*;<br />import java.rmi.*;<br />public class RmiServer{<br />public static void main(String args[]) {<br />try {<br />RmiDemoImpl obj_RmiDemoImpl = new RmiDemoImpl();<br />Naming.rebind(“RmiServer&quot;, obj_RmiDemoImpl);<br />}<br />catch(Exception e) {<br />System.out.println(&quot;Exception: &quot; + e);<br />}<br />}<br />}<br />3/11/2009<br />8<br />RMI - Swarup Kulkarni<br />
  11. 11. Step 1: (Continued..)<br />The forth source file:<br />RmiClient.java<br />Implements client side of this distributed application.<br />Requires thee main things – IP address or name of server, and the two numbers to be added.<br />We create a string that follows URL syntax.<br />The URL includes IP or Name of the server and the string “RmiServer”.<br />The program invokes lookup() method from Naming class which accepts rmi URL as argument and returns a reference of object of RmiDemoIntf .<br />Now all the remote method invocations can be directed to this object.<br />3/11/2009<br />9<br />RMI - Swarup Kulkarni<br />
  12. 12. Step 1: (Continued..)<br />The forth source file - Code:<br />RmiClient.java<br />import java.rmi.*;<br />public class RmiClient {<br />public static void main(String args[]) {<br />try {<br />double d1 = 24.73;<br />double d2 = 11.12;<br />String RmiDemoURL = &quot;rmi://&quot; + “127.0.0.1” + &quot;/RmiServer&quot;;<br />//127.0.0.1 is loopback address replace it with server IP/name<br />RmiDemoIntf obj = (RmiDemoIntf)Naming.lookup(RmiDemoURL);<br />3/11/2009<br />10<br />RMI - Swarup Kulkarni<br />
  13. 13. Step 1: (Continued..)<br />RmiClient.java(Contd..)<br />System.out.println(&quot;The first number is: &quot; + d1);<br />System.out.println(&quot;The second number is: &quot; + d2);<br />System.out.println(&quot;The sum is: &quot; + RmiDemoIntf.add(d1, d2));<br /> }//end of try<br /> catch(Exception e) {<br /> System.out.println(&quot;Exception: &quot; + e);<br /> }//end of catch<br /> }//end of main<br />}//end of class<br />3/11/2009<br />11<br />RMI - Swarup Kulkarni<br />
  14. 14. Step 2: Generate Stubs and Skeletons<br />Before using client and server we have to generate stub and skeleton.<br />Stub resides on client machine.<br />Remote method calls initiated by client are directed to stub.<br />A remote method can have simple arguments or objects as arguments.<br />Objects may have reference to other objects, to send whole information to server the objects in arguments must be serialized.<br />Skeletons are not required by Java 2.(Needed for compatibility with java1.1)<br />Skeleton resides on sever machine, when it receives request, it performs deserialization and invokes appropriate method.<br />In case of response to client, the process works reverse.<br />3/11/2009<br />12<br />RMI - Swarup Kulkarni<br />
  15. 15. Step 2: (Continued..)<br />How to generate stub and Skeleton?<br />Use a tool called RMI Compiler, which is invoked from command line:<br /> rmic RmiDemoImpl;<br />This command generates two new files: RmiDemoImpl_Skel.class (skeleton) and RmiDemoImpl_Stub.class (stub).<br />Before using rmic make sure that CLASSPATH is set to include current directories.<br />rmic by default generates stub as well as skeleton, we have option to suppress it.<br />3/11/2009<br />13<br />RMI - Swarup Kulkarni<br />
  16. 16. Step 3: Install Files on Client and Server<br />Copy RmiDemo.class, RmiDemoImpl_Stub.class, and RmiDemoImpl.class on the client machine.<br />Copy RmiDemontf.class, RmiDemoImpl.class, RmiDemompl_Skel.class, RmiDemompl_Stub.class, and RmiDemo.class on the server machine.<br />3/11/2009<br />14<br />RMI - Swarup Kulkarni<br />
  17. 17. Step 4: Start the RMI Registry on the Server<br />The Java 2 SDK provides a program called rmiregistry, which executes on server.<br />The rmiregistry maps names to object reference.<br />First, check that the CLASSPATH environment variable includes the directory in which your files are located and start RMI Registry:<br /> start rmiregistry<br />When this command runs, it creates a new window. Leave that window open until the experimentation finishes.<br />3/11/2009<br />15<br />RMI - Swarup Kulkarni<br />
  18. 18. Step 5: The Experiment<br />The server code is started from the command line, as shown here:<br />java RmiServer<br />Recall that the RmiServer code instantiates RmiDemoImpl and registers that object with the name “RmiServer”.<br />The client code is started from command line as shown here:<br />java RmiClient<br />Output of progarm:<br />The first number is: 24.73<br />The second number is: 11.12<br />The sum is: 35.85<br />3/11/2009<br />16<br />RMI - Swarup Kulkarni<br />
  19. 19. 3/11/2009<br />17<br />RMI - Swarup Kulkarni<br />Thank You<br />

×