Distributed Programming using RMI


Published on

Published in: Business, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Distributed Programming using RMI

  1. 1. IADCS Diploma Course Distributed Programming with Java U Nyein Oo COO/Director(IT) Myanma Computer Co., Ltd
  2. 2. Why Distributed Application? <ul><li>Most of development and deployment environments are Networked </li></ul><ul><li>Applications design to be distributed </li></ul><ul><li>(which is become necessary) </li></ul><ul><li>Process is distributed across multiple networked computers </li></ul><ul><li>Implemented as Client/server Systems. </li></ul>
  3. 3. Client/Server Systems? <ul><li>Client requests resource from Server </li></ul><ul><li>Server accept the client’s request and process and give result back to client </li></ul><ul><li>Typically there are three components in client/server system. </li></ul>
  4. 4. Three components of CSS <ul><li>User Interface Layer( implemented by application client like web browser) </li></ul><ul><li>Information Processing Layer( implemented by application client/server/server support) </li></ul><ul><li>Information Storage Layer( implemented by database server like web server,FTP server) </li></ul>
  5. 5. IPL at Client Machine User Interface Layer (Web Browser) Information Processing Layer (Application Client) Information Storage Layer (Application Server) Client Machine Server Machine
  6. 6. IPL at Server Machine User Interface Layer (Web Browser) Information Processing Layer (Application Client) Information Storage Layer (Application Server) Client Machine Server Machine
  7. 7. Different type of Method Invocation <ul><li>Distributed Computing Environment(DCE) </li></ul><ul><li>Distributed Components Object Model(DCOM) </li></ul><ul><li>Common Object Request Broker Architecture (CORBA) </li></ul><ul><li>Java’s Remote Methods Invocation(RMI) </li></ul>
  8. 8. Distributed Computing Environment(DCE) <ul><li>Industry standard of the OSF(Open Software Foundation) </li></ul><ul><li>Vendor-neutral set of Distributed Computing </li></ul><ul><li>Provide security services to protect and control access to data </li></ul><ul><li>Run of the major computing platforms and designed to support distributed application </li></ul><ul><li>Key Technology for security,www and Distributed Objec </li></ul>
  9. 9. How Distributed Systems work? <ul><li>Organized into cell(which is nothing but a group of processing resources,services and users that support a common function and share a common set of DCE devices) </li></ul><ul><li>For example …separate department in a company like(admin,finance..ect) </li></ul>
  10. 10. The services and Technologies used within DCE Cell <ul><li>Directories Service </li></ul><ul><li>Distributed File Service(DFS) </li></ul><ul><li>Distributed Time Service(DTS) </li></ul><ul><li>Security Service </li></ul><ul><li>Remote Procedure Calls(RPC) </li></ul><ul><li>DCE Threads </li></ul>
  11. 11. What is a DCOM? <ul><li>Distributed Component Object Model </li></ul><ul><li>Product of Microsoft Corporation </li></ul><ul><li>Started with OLE technology that used to support compound documents.(one that is product of multiple application) </li></ul><ul><li>COM is solution for OLE and different from DCE in its design approach. </li></ul>
  12. 12. DCOM(cont) <ul><li>COM is object oriented whereas DCE is procedural oriented </li></ul><ul><li>A model ,programming Language can be used COM to create distributed application </li></ul><ul><li>DCOM allows a COM object on one computer to access methods of another COM object on some other computers. </li></ul><ul><li>The remote object is accessed the same manner as the local object. </li></ul>
  13. 13. DCOM(cont) <ul><li>DCOM is distributed COM over multiple computers. </li></ul><ul><li>Local Object invokes the methods of Local COM library of the remote object it is trying to access. </li></ul><ul><li>COM library process the functions calls using COM runtime </li></ul><ul><li>COM Runtime communicate using Object RPC </li></ul><ul><li>The most important feature is application security is based on Windows NT Policy. </li></ul>
  14. 14. CORBA <ul><li>Common Object Request Broker Architecture </li></ul><ul><li>Most popular architecture for building distributed object oriented application </li></ul><ul><li>Is open standard not tied to particular vendor </li></ul><ul><li>Objects are accessible via ORBs </li></ul><ul><li>The client interface to ORB is written by IDLKnown as IDL stub </li></ul><ul><li>The Server interface to ORB is IDL Skeleton </li></ul>
  15. 15. Method invocation in CORBA IDL Stub IDL Skeleton Client Host Server Host client Object Server Object ORB
  16. 16. Benefits using CORBA <ul><li>Open standard and works on all platforms </li></ul><ul><li>Both clients and servers can be distributed </li></ul><ul><li>Language independent </li></ul><ul><li>Just have to provide IDL interface to object </li></ul><ul><li>An international standard and recognized by most of software vendors. </li></ul>
  17. 17. Why Java Distributed Object Model Need? <ul><li>TCP Socket is big Overhead </li></ul><ul><li>DCE ‘s RPC does not gel with Java OODA. </li></ul><ul><li>DCOM has good performance for Windows and does not support java object fully. </li></ul><ul><li>CORBA is excellent but Java programmer will have to learn IDL to develop DJA. </li></ul>
  18. 18. RMI (your best choice for java) <ul><li>JavaSoft design for Object oriented distributed pure java application. </li></ul><ul><li>RMI also need client stub & server skeleton </li></ul><ul><li>Support seamless remote invocation on objects in different VMs. </li></ul><ul><li>Support callbacks from server to applets. </li></ul><ul><li>Integrated DO model into Java Language </li></ul>
  19. 19. RMI(cont;) <ul><li>Make differences between DO model and Local Java Object model apparent. </li></ul><ul><li>Writing reliable distributed applications as simple as possible. </li></ul><ul><li>Preserve the type-safety provided by the Java Runtime Environment </li></ul><ul><li>Save Java environment provided by security managers and class loaders. </li></ul>
  20. 20. RMI Architecture <ul><li>JDOM is simply extension of JOM </li></ul><ul><li>Objects interact each other within same JVM in Java’s Object Model </li></ul><ul><li>Objects interact with an object in another JVM at Java Distributed Object Model </li></ul><ul><li>Client Object(local object) does not reference directly on Server Object(remote object) and references a remote interface </li></ul>
  21. 21. <ul><li>Allows server objects to differentiate between their local and remote interface </li></ul><ul><li>Present different access modes </li></ul><ul><li>No need to have server class file at the compilation of client objects </li></ul><ul><li>RMI follow like CORBA except ORB </li></ul><ul><li>RMI Setup the Remote Reference Layer </li></ul>Advantages using Remote Interface
  22. 22. Three tier Architecture of Java RMI Client Object Server Object Stub Skeleton Remote Reference Layer Remote Reference Layer Transport Layer Transport Layer Lower Layer Protocols Lower Layer Protocols
  23. 23. <ul><li>Explain java.rmi packages: </li></ul><ul><ul><li>java.rmi </li></ul></ul><ul><ul><li>java.rmi.registry </li></ul></ul><ul><ul><li>java.rmi.server </li></ul></ul><ul><ul><li>java.rmi.activation </li></ul></ul><ul><ul><li>java.rmi.dgc </li></ul></ul><ul><li>Three tiered layering of Java RMI </li></ul>Inside RMI
  24. 24. java.rmi Package <ul><li>This package provides the remote interface </li></ul><ul><li>It does not have any methods </li></ul><ul><li>It supports the following classes: </li></ul><ul><ul><li>MarshalledObject class </li></ul></ul><ul><ul><li>Naming class </li></ul></ul><ul><ul><li>RMISecurityManager class </li></ul></ul><ul><li>It defines exceptions such as RemoteException </li></ul>
  25. 25. java.rmi.registry Package <ul><li>This package provides classes and interfaces for remote registry </li></ul>
  26. 26. java.rmi.server Package <ul><li>This package implements remote stubs and skeletons </li></ul><ul><li>It is also used to support RMI communications </li></ul><ul><li>It consists of the following classes: </li></ul><ul><ul><li>RemoteObject class </li></ul></ul><ul><ul><li>RemoteServer class </li></ul></ul><ul><ul><li>RMILoader class </li></ul></ul><ul><ul><li>Operation class </li></ul></ul><ul><ul><li>LogStream class </li></ul></ul>
  27. 27. java.rmi.activation Package <ul><li>This package is used to activate remote objects, when needed </li></ul><ul><li>It consists of the following classes: </li></ul><ul><ul><li>Activable class </li></ul></ul><ul><ul><li>ActivationDesc class </li></ul></ul><ul><ul><ul><li>getClassName( ) </li></ul></ul></ul><ul><ul><ul><li>getCodeSource( ) </li></ul></ul></ul><ul><ul><ul><li>getData( ) </li></ul></ul></ul><ul><ul><ul><li>getGroupID( ) </li></ul></ul></ul><ul><ul><ul><li>getRestartMode( ) </li></ul></ul></ul><ul><ul><li>ActivationGroup class </li></ul></ul>
  28. 28. java.rmi.dgc Package <ul><li>This package provides classes and interfaces that the RMI distributed garbage collector uses </li></ul><ul><li>The server side of the distributed garbage collector uses this package </li></ul><ul><li>It has two methods, dirty( ) and clean( ) , which indicate that the client has referenced a remote object, and that the remote reference has been completed, respectively </li></ul>
  29. 29. Three Tiered Layering of Java RMI <ul><li>The client object evokes the methods of stub </li></ul><ul><li>The stub uses the remote layer to communicate with the skeleton </li></ul><ul><li>The remote layer uses the transport layer to establish the connection between local and remote server </li></ul>
  30. 30. Implementing RMI <ul><li>Implementing RMI involves two steps namely: </li></ul><ul><ul><li>Implementing RMI on Remote Host </li></ul></ul><ul><ul><li>Implementing RMI on Local Host </li></ul></ul>
  31. 31. Implementing RMI on Remote Host <ul><li>Step 1 </li></ul><ul><li>Create a remote interface: </li></ul><ul><ul><li>This has to be public and must extend the Remote interface </li></ul></ul><ul><ul><li>This method must throw RemoteException </li></ul></ul><ul><li>Step 2 </li></ul><ul><li>Create a class that implements the remote interface: </li></ul><ul><ul><li>This class should create and initialize the remote object </li></ul></ul><ul><ul><li>It should implement all the methods defined in the remote interface </li></ul></ul><ul><ul><li>There should be a main( ) method that will be executed as a remote class </li></ul></ul><ul><ul><li>This main( ) method should also take care of security using the setSecurityManager( ) method </li></ul></ul>
  32. 32. Implementing RMI on Remote Host (Contd…) <ul><li>Step 3 </li></ul><ul><li>Create stub and skeleton classes using the rmic command. </li></ul><ul><li>Step 4 </li></ul><ul><li>Copy the remote interface and stub file to the client host. </li></ul><ul><li>Step 5 </li></ul><ul><li>Start the Remote Registry Server using the command start rmiregistry. </li></ul><ul><li>Step 6 </li></ul><ul><li>Execute the program using the java command to create an object of class. This class registers itself with the remote registry </li></ul>
  33. 33. Implementing RMI on Local Host <ul><li>Once RMI is implemented on the remote host, the remote server is up and running </li></ul><ul><li>Create a client program to remotely invoke the method of the object, and display the results </li></ul>
  34. 34. Genreation Remote Interface //remote interface import java.net.*; import java.security.*; import java.rmi.*; public interface MyServer extends java.rmi.Remote{ String getAndSetMessage(String message) throws java.rmi.RemoteException; }
  35. 35. Generation Client RMI import java.rmi.RMISecurityManager; import java.rmi.Naming; import java.net.*; import java.security.*; public class MyClient{ public static void main(String args[]){ System.setSecurityManager(new RMISecurityManager()); try{ MyServerser=(MyServer)Naming.lookup(&quot;//;); String serverString=ser.getAndSetMessage(&quot;I amd Trying &quot;); System.out.println(&quot;reply from the server is &quot;+serverString); }catch(Exception e){ System.out.println(e); } } }
  36. 36. Generation Server RMI import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; import java.security.*; import java.net.*; public class MyServerImpl extends UnicastRemoteObject implements MyServer{ static String hostName=&quot;;; String name; public MyServerImpl(String name) throws RemoteException{ super(); this.name=name; }
  37. 37. Generation Server RMI (cont) public String getAndSetMessage(String message)throws RemoteException{ return(&quot;My name is &quot;+name+&quot;Thanks for message&quot;+message); } public static void main(String args[]){ System.setSecurityManager(new RMISecurityManager()); try{ String objectname=&quot;Servertest&quot;; MyServerImpl theServer=new MyServerImpl(objectname); Naming.rebind(&quot;//&quot;+hostName+&quot;/&quot;+objectname,theServer); System.out.println(&quot;I am Registered&quot;); }catch(Exception e) { System.out.println(e); } }}
  38. 38. Steps to Compilation <ul><li>Compilation Remote Interface File </li></ul><ul><li>(same as regular .java file) </li></ul><ul><li>Compilation Remote Client File </li></ul><ul><li> (same as regular .java file) </li></ul><ul><li>Compilation Remote Server File </li></ul><ul><li>I.same as regular .java file </li></ul><ul><li>II. User rmic compiler to create stub and skeleton class files like </li></ul><ul><li>c:jdk1.3.1in>rmic MyServerImpl </li></ul>
  39. 39. <ul><li>Starting rmi registry </li></ul><ul><li>>start rmiregistry </li></ul><ul><li>Run as follow </li></ul><ul><li>>java –Djava.security.policy=policy MyServerIMpl </li></ul><ul><li>Edit policy for permission grant </li></ul>Steps to Compilation(cont)
  40. 40. Example-2(Remote Interface) //Remote Interface import java.rmi.*; import java.net.*; public interface Server1 extends Remote { public void doSomething() throws RemoteException; }
  41. 41. Client Remote import java.rmi.*; import java.net.*; import java.rmi.server.*; public class Client1 { public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); try { Server1 ro = (Server1)Naming.lookup(&quot;doSomething&quot;); System.out.println(&quot;Location: &quot;+System.getProperty(&quot;LOCATION&quot;)); ro.doSomething(); } catch (Exception e){ e.printStackTrace(); System.exit(-1); } } }
  42. 42. Server Remote import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; public class Server1Impl extends java.rmi.server.UnicastRemoteObject implements Server1 { public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); try { Server1Impl obj = new Server1Impl(); Naming.rebind(&quot;doSomething&quot;, obj); System.out.println(&quot;doSomething bound in registry&quot;); } catch (Exception e) { e.printStackTrace(); return; } }
  43. 43. Server Remote (cont) public Server1Impl() throws RemoteException { } public void doSomething() throws RemoteException { System.out.println(&quot;This message is printed by the Server1 object&quot;); System.out.println(&quot;Location: &quot; + System.getProperty(&quot;LOCATION&quot;)); } }
  44. 44. Invoke rmi registry & Run Client Remote
  45. 45. Running Remote Server