Published on

Hariprasanna V (9843824677)

1 Like
  • Be the first to comment

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

No notes for slide
  • Defining the remote interfaces : A remote interface specifies the methods that can be invoked remotely by a client. Clients program to remote interfaces, not to the implementation classes of those interfaces. Part of the design of such interfaces is the determination of any local objects that will be used as parameters and return values for these methods; if any of these interfaces or classes do not yet exist, you need to define them as well. Implementing the remote objects: Remote objects must implement one or more remote interfaces. The remote object class may include implementations of other interfaces (either local or remote) and other methods (which are available only locally). If any local classes are to be used as parameters or return values to any of these methods, they must be implemented as well. Implementing the clients: Clients that use remote objects can be implemented at any time after the remote interfaces are defined, including after the remote objects have been deployed.
  •   /** This is the actual implementation of Rem that * the RMI server uses. The server builds an instance * of this then registers it with a URL. The * client accesses the URL and binds the result to * a Rem (not a RemImpl; it doesn't have this). */
  • Class4

    1. 1. Advanced Java Unit 4 RMI CMSC 291 Shon Vick
    2. 2. Agenda <ul><li>Present some ideas about distributed computing </li></ul><ul><li>Briefly look at CORBA and contrast it with RMI </li></ul><ul><li>Look at the Java mechanism for Remote Method Invocation (RMI) </li></ul><ul><li>See some examples </li></ul>
    3. 3. Overview of RMI Applications <ul><li>Pure Java answer to RPC, DCOM, and CORBA </li></ul><ul><li>CORBA allows object on different machines to communicate </li></ul><ul><li>RMI allows objects on different JVMs potentially on different machines to communicate </li></ul>
    4. 4. CORBA <ul><li>The Common Object Request Brokering System (CORBA) glues together diferent objects models regardless of implementation language </li></ul><ul><li>In order to do so it uses a common interface definition language that needs to be compiled separately for every language for which there is a binding </li></ul>
    5. 5. Introduction to CORBA <ul><li>So what is an object (class, interface) in the CORBA sense ? </li></ul><ul><ul><li>Collection of data items usually called attributes or slots </li></ul></ul><ul><ul><li>Collection of behavioral attachments usually called methods </li></ul></ul>
    6. 6. What Is Distributed Computing All About <ul><li>Goal is to break up a monolithic application into smaller components </li></ul><ul><li>Client - user of a resource </li></ul><ul><li>Server - provider of a resource </li></ul>Client Server Request Response
    7. 7. Technical Overview of Architecture <ul><li>The client and object implementation are isolated from the ORB by an IDL interface </li></ul><ul><li>CORBA requires that objects be defined by OMG IDL </li></ul><ul><li>A method request does not pass directly from client to server but rather is mediated by the ORB </li></ul><ul><li>Method invocation is the same whether the object is local or remote </li></ul>
    8. 8. CORBA Interfaces <ul><li>IDL - interface definition language </li></ul><ul><ul><li>CORBA is language independent </li></ul></ul><ul><ul><li>Can provide a standard notation for software components </li></ul></ul><ul><ul><li>IDL supports library function interfaces just as well as distributed objects across a network </li></ul></ul>C++ IDL Client ORB Java IDL Server
    9. 9. IDL - Separating Interface From Implementation <ul><li>A standard notation language for defining interfaces to objects </li></ul><ul><li>OMG IDL is a simple subset of C++ </li></ul><ul><li>IDL is like a contract for defining an API </li></ul><ul><li>Designed to be efficient across networks </li></ul>
    10. 10. Role of IDL ORB I D L I D L I D L I D L I D L I D L Client Side Object Implementation Side COBOL C Ada C++ Small talk JAVA I D L I D L I D L I D L I D L I D L C++ COBOL Small talk JAVA C C++ Ada ORB
    11. 11. How the IDL is Used IDL IDL Compiler Skeleton Code Stub Code Client Code Object Impl Code Language Compiler and Linker ORB Stub Client Skel Object
    12. 12. Programming Steps <ul><li>Define the IDL interfaces </li></ul><ul><li>Implement these interfaces with C++ classes </li></ul><ul><li>Write a client mainline to bind to server </li></ul><ul><li>Write a server mainline which creates instances of the classes </li></ul><ul><li>Register the server </li></ul>
    13. 13. Comparison of RMI and CORBA <ul><li>RMI is Java to Java based </li></ul><ul><ul><li>JNI allows Java to other languages </li></ul></ul><ul><li>No mapping of objects – no IDL </li></ul><ul><li>RMI deals only with byte code </li></ul><ul><li>No complicated request broker - just a simple registry </li></ul><ul><li>Makes a distinction for a remote object -allows errors to be handled </li></ul>
    14. 14. RMI <ul><li>An Overview of RMI Applications </li></ul><ul><li>Looking at a Simple Example </li></ul><ul><ul><li>Walking through a RMI Server </li></ul></ul><ul><ul><li>Walking through a Client Program </li></ul></ul><ul><ul><li>Running the applications </li></ul></ul><ul><li>Next time </li></ul><ul><ul><li>Additional Examples </li></ul></ul><ul><ul><li>Reviewing Some More Examples </li></ul></ul>
    15. 15. An Overview of RMI Applications <ul><li>Two Parts to a RMI based distributed object application </li></ul><ul><ul><li>Server </li></ul></ul><ul><ul><ul><li>creates remote objects </li></ul></ul></ul><ul><ul><ul><li>makes them available to client </li></ul></ul></ul><ul><ul><ul><li>waits for clients to invoke methods these remote objects </li></ul></ul></ul><ul><ul><li>Client: </li></ul></ul><ul><ul><ul><li>Use the references to the remote Objects </li></ul></ul></ul>
    16. 16. RMI Distributed Object Applications <ul><li>Locate remote objects </li></ul><ul><li>Communicate with remote objects </li></ul><ul><li>Load class bytecodes for objects that are passed around </li></ul>
    17. 17. Locating Remote Objects <ul><li>Applications can use one of two mechanisms to obtain references to remote objects. </li></ul><ul><ul><li>An application can register its remote objects with the rmiregistry - RMI's simple naming facility </li></ul></ul><ul><ul><li>An application can pass and return remote object references as part of its normal operation </li></ul></ul>
    18. 18. Communicating with Remote Objects <ul><li>Details of communication between remote objects are handled by RMI </li></ul><ul><li>From the perspective of the programmer it’s as if the objects are local </li></ul><ul><li>There is some overhead that makes it slower but the communication looks like a standard Java method invocation </li></ul>
    19. 19. Loading the Class <ul><li>The types and the behavior of an object, previously available only in a single virtual machine can be transmitted to another, possibly remote, virtual machine. </li></ul><ul><li>Contrast with the reading and writing of objects that we have discussed in the previous units </li></ul>
    20. 20. Remote Interfaces, Objects and Methods <ul><li>A distributed application built using Java RMI is made up of interfaces and classes </li></ul><ul><li>An object becomes remote by implementing a remote interface, which has the following characteristics </li></ul><ul><ul><li>A remote interface extends the interface java.rmi.Remote. </li></ul></ul><ul><ul><li>Each method of the interface declares java.rmi.RemoteException in its throws clause, in addition to any application-specific exceptions. </li></ul></ul>
    21. 21. Remote Objects <ul><li>RMI treats a remote object differently from a non remote object when the object is passed from one virtual machine to another. </li></ul><ul><li>Rather than making a copy of the implementation object in the receiving virtual machine, RMI passes a remote stub for a remote object. </li></ul><ul><li>The stub acts as a proxy for the remote object and is basically a remote reference. </li></ul><ul><li>The caller invokes a method on the local stub, which is responsible for carrying out the method call on the remote object. </li></ul>
    22. 22. Layers Client Server Stub Skeleton Remote Reference Layer Transport Layer
    23. 23. What happens Where? <ul><li>Layer 1 is the Application Layer </li></ul><ul><li>Layer 2 is the client stub/skeleton layer. These are the proxy objects these are produced by the rmic command </li></ul><ul><li>Layer 3 is the remote reference that deals with the the actual remote invocations </li></ul><ul><li>Layer 4 is the transport layer responsible for actually setting up the connections and handling the transport of data between machines </li></ul>
    24. 24. Using Remote Objects <ul><li>A stub for a remote object implements the same set of remote interfaces that the remote object implements </li></ul><ul><li>This allows a stub to be cast to any of the interfaces that the remote object implements </li></ul><ul><li>Only those methods defined in a remote interface are available to be called in the receiving virtual machine. </li></ul>
    25. 25. Using RMI – General Steps <ul><li>Design and implement the components of your distributed application. </li></ul><ul><li>Compile sources and generate stubs and skeletons </li></ul><ul><li>Make classes network accessible </li></ul><ul><li>Start the application. </li></ul>
    26. 26. Design and Implement the Application Components <ul><li>Defining the remote interfaces </li></ul><ul><li>Implementing the remote objects </li></ul><ul><li>Implementing the clients </li></ul>
    27. 27. Compile Sources and Generate Stubs <ul><li>This is a two-step process. </li></ul><ul><ul><li>In the first step you use the javac compiler to compile the source files containing the implementation of the remote interfaces and implementations, the server classes, and the client classes. </li></ul></ul><ul><ul><li>In the second step you use the rmic compiler to create stubs for the remote objects. RMI uses a remote object's stub class as a proxy in clients so that clients can communicate with a particular remote object. </li></ul></ul>
    28. 28. Make Classes Network Accessible/ Starting <ul><li>In this step you make everything--the class files associated with the remote interfaces, stubs, and other classes that need to be downloaded to clients--accessible via a Web server </li></ul><ul><li>Starting the application includes running the RMI remote object registry, the server, and the client. </li></ul>
    29. 29. The Four Required Classes <ul><li>To use RMI you need to build man classes </li></ul><ul><ul><li>An interface for the remote object to be used by both the client and the server </li></ul></ul><ul><ul><li>The RMI client which looks up the object on a remote machine, cast it to the needed type of the interface given in the step 1 </li></ul></ul><ul><ul><li>The object implementation – implements the interface of step 1 </li></ul></ul><ul><ul><li>The RMI server – which creates an instance of the object from step 3 </li></ul></ul>
    30. 30. Steps for Compiling and Running the System <ul><li>Compile the client and the server </li></ul><ul><li>Generate the client stub using rmic </li></ul><ul><li>Start the RMI registry – this only needs to be done once </li></ul><ul><li>Start the the server – done on same machine as in step 3 </li></ul><ul><li>Start the client – this doesn’t have to be on the same machine as 3 and 4 </li></ul>
    31. 31. A Simple Example <ul><li>In this example the remote object just returns a message string </li></ul><ul><ul><li>The interface </li></ul></ul><ul><ul><li>The client </li></ul></ul><ul><ul><li>The Remote Object Implementation </li></ul></ul><ul><ul><li>The RMI Server </li></ul></ul>
    32. 32. The interface import java.rmi.*;   /** The RMI client will use this interface directly. * The RMI server will make a real remote object that * implements this, then register an instance of it * with some URL. */   public interface Rem extends Remote { public String getMessage() throws RemoteException ; }
    33. 33. The RMI Client <ul><li>Looks up the object from the appropriate host using Naming.lookup </li></ul><ul><li>Casts it to the appropriate type then uses it like a local object </li></ul><ul><li>Unlike CORBA RMI has to know the host that is providing the remote services </li></ul><ul><ul><li>uses URL of the form rmi:://host/path or rmi: rmi:://host:port/path </li></ul></ul><ul><ul><li>the default port is 1099 </li></ul></ul>
    34. 34. The RMI Client <ul><li>A number of exceptions must be caught </li></ul><ul><ul><li>RemoteException </li></ul></ul><ul><ul><li>NotBoundException </li></ul></ul><ul><ul><li>MalformedURLException </li></ul></ul><ul><li>Requires java.rmi for RemoteException as well Naming and and NotBound Exception </li></ul><ul><li>Requires for MalformedURLException as we have already seen </li></ul><ul><li>Many Clients will pass Serializable objects to the remote objects so importing is usually done as well – not needed here however </li></ul>
    35. 35. The Client Code <ul><li>Import the needed Packages </li></ul>import java.rmi.*; // For Naming, RemoteException, etc. import*; // For MalformedURLException import*; // For Serializable interface
    36. 36. <ul><li>public class RemClient { </li></ul><ul><li>public static void main(String[] args) { </li></ul><ul><li>try { </li></ul><ul><li>String host = (args.length > 0) ? args[0] : &quot;localhost&quot;; </li></ul><ul><li>// Get the remote object and store it in remObject: </li></ul><ul><li>Rem remObject = </li></ul><ul><li>(Rem)Naming.lookup(&quot;rmi://&quot; + host + &quot;/Rem&quot;); </li></ul><ul><li>// Call methods in remObject: </li></ul><ul><li>System.out.println(remObject.getMessage()); </li></ul><ul><li>//catch the exceptions … </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>The Client Code Body Can now use like a local object
    37. 37. Catching the Exceptions in the Client <ul><li>try{ </li></ul><ul><li>// … </li></ul><ul><li>} catch(RemoteException re) { </li></ul><ul><li>System.out.println(&quot;RemoteException: &quot; + re); </li></ul><ul><li>} catch(NotBoundException nbe) { </li></ul><ul><li>System.out.println(&quot;NotBoundException: &quot; + nbe); </li></ul><ul><li>} catch(MalformedURLException mfe) { </li></ul><ul><li>System.out.println(&quot;MalformedURLException: &quot; </li></ul><ul><li>+ mfe); </li></ul><ul><li>// … </li></ul>
    38. 38. The Remote Object Implementation <ul><li>This class must extend UnicastRemoteObject and implement the interface discussed previously </li></ul><ul><li>The constructor show throw a RemoteException </li></ul>
    39. 39. The Remote Object import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class RemImpl extends UnicastRemoteObject implements Rem { public RemImpl() throws RemoteException {}   public String getMessage() throws RemoteException { return(&quot;Here is a remote message.&quot;); } }
    40. 40. The RemImpl Object <ul><li>This is the actual implementation of Rem that the RMI server uses. </li></ul><ul><li>The server builds an instance of this then registers it with a URL </li></ul><ul><li>The client accesses the URL and binds the result to Rem (not a RemImpl; it doesn't have this). </li></ul>
    41. 41. The RMI Server <ul><li>Builds an object and registers it with a particular URL using Naming.rebind to replace any other bindings or Naming.bind which throws a AlreadyBoundException if there is a previous binding </li></ul><ul><li>Bind really means register here </li></ul><ul><li>You have to catch RemoteException and URLMalformedException </li></ul>
    42. 42. RemServer mport java.rmi.*; import*;   public class RemServer { public static void main(String[] args) { try { RemImpl localObject = new RemImpl(); Naming.rebind(&quot;rmi:///Rem&quot;, localObject); } catch(RemoteException re) { System.out.println(&quot;RemoteException: &quot; + re); } catch(MalformedURLException mfe) { System.out.println(&quot;MalformedURLException: &quot; + mfe); } } } Makes an Object Registers an Object
    43. 43. Putting it all Together <ul><li>Compile the Client and the Server </li></ul><ul><li>javac </li></ul><ul><li>Generate the Client Stub and Server Skeleton </li></ul><ul><li>rmic RemImpl </li></ul><ul><li>Starts the RMI registry </li></ul><ul><li>Rmiregistry [&] </li></ul><ul><li>Start the server Start the client </li></ul><ul><li>java RemServer [&] java RemClient </li></ul>
    44. 44. Next Unit <ul><li>RMI/CORBA Issues </li></ul><ul><li>More Examples </li></ul><ul><li>Start Chapter 11 - Threads </li></ul>
    45. 45. Next Time <ul><li>Study Chapter 7 and review Example </li></ul><ul><li>Homework posted Monday AM </li></ul><ul><li>Read Chapter 11 - Threads </li></ul>
    46. 46. Selected References <ul><li>Advanced Techniques for Java Developers Chapter 6,7 </li></ul><ul><li> </li></ul><ul><li>Exploring Java, O’Reilly, Niemeyer & Peck </li></ul><ul><li>Java RMI Remote Method Invocation, Troy Downing, IDG Books </li></ul><ul><li>The RMI example comes from Core Web Programming, Marty Hall, Prentice Hall </li></ul>