1 / 48

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000. Lecture 19 Advanced Java Concepts Multithreading (continued) Remote Method Invocation (RMI) [ Deitel : Chapter 20] Fri . 11/3 – Mon. 11/6. Homework Status. HW# Assigned Due.

miriam-love
Télécharger la présentation

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UMass Lowell Computer Science 91.460Java and Distributed ComputingProf. Karen DanielsFall, 2000 Lecture 19 Advanced Java Concepts Multithreading (continued) Remote Method Invocation (RMI) [Deitel: Chapter 20] Fri. 11/3 – Mon. 11/6

  2. Homework Status HW# Assigned Due 1 Fri, 9/8 9/15, 9/18 2 Fri, 9/15 Fri, 9/22 3 Fri, 9/22 Fri, 9/29 4 Fri, 10/6 Fri, 10/13 5 Fri, 10/13 Fri, 10/20 6 Fri, 10/20 Fri, 10/27 7 Fri, 10/27 Fri, 11/3 & 11/6 Graded Submitted Pending

  3. Finishing our “Thread”... Code/Demos Monitors/Synchronization

  4. Java’s Monitor Concept • Java provides a monitor for each object and class • Unique lock for each object and class • Monitor controls access to synchronized code (method, block • block must specify object • Unsynchronized code is not regulated by monitor Figure Source: Operating Systems: Advanced Concepts

  5. The Class class • Objects of this class, called class descriptors, are automatically created by the JVM when a class (or interface) is loaded • Accessed by an Object’s getClass method • Can get the class’s name, superclass, interfaces • Can be locked for thread synchronization purposes • A class lock is a lock on a class Class object

  6. synchronized Static Methods • You can get a lock on a class (actually, the class’s Class object) by creating a synchronizedstatic method • Only one thread can execute a (any) synchronizedstatic method at a time

  7. Remote Method Invocation (RMI)

  8. Distributed Computing • High-Level Definition • Pieces of some computation run on separate processing units (potentially across a network) • Goal: as close to seamless distribution as possible • Minimize the code changes required to split apart an application such that it runs in a distributed environment Distributed Computing requires a modular design

  9. Distributed Objects • Distributed computing (without objects) has been around awhile, e.g.: • Socket-based interprocess communication (IPC) • Remote Procedure Calls (RPC) • Distributed Computing Environment (DCE) • But objects can be distributed as well • Common Object Request Broker Architecture (CORBA) • Microsoft OLE, COM/DCOM, Active-X, ??? • Java RMI

  10. Granularity of Distributed Computing • Coarse-grained distribution (more common by far) • Single objects representing a client and a server • Fine-grained distribution • Potentially every object (e.g., a delivery, an airplane) is distributed • There are key performance, modularity, flexibility trade-offs to make when deciding on an appropriate degree of granularity

  11. Client-Server Model • Servers are providers of a service • Data providers (files, databases, images, ...) • Computing providers • Clients are users of a service • Generally think of clients being “in front of the human user”

  12. RMI Example: Deitel Chapter 20Weather • Use the National Weather Service Web site: • http://iwin.nws.noaa.gov/iwin/us/traveler.html

  13. Chapter 20 Weather Example: TemperatureClient TemperatureServer RMI Server RMI Client RMI At-A-Glance • Objects in one Java Virtual Machine (JVM) invoke the methods of objects in another JVM • Invoking objects (clients) are often called local objects • Invoked objects (servers) are often called remote objects

  14. RMI - Not Just for Networks • Local and remote objects can be on the same machine (but in different processes) • Local and remote objects can be on different machines across a network • Note: a single Java program can contain both client and server objects • Similar to Remote Procedure Call (RPC)

  15. Java Interfaces: A Key Ingredient • If a class implements an interface, then an object of that class can be “cast” to that interface “type” • Can declare an “object reference” that uses an interface instead of a class type • When “client code” invokes an interface method on this reference, the JVM (at run-time) • determines which class (that implements the interface) the reference really refers to • operates on the object of that class • Polymorphism via interfaces! • “Client code” only needs to know about interface, not implementation!

  16. Remote Objects: Key Ideas • Remote interface describes server object’s behavior • Server object and its name are registered with a “lookup service”: rmiregistry • Client knows: • remote interface • name of server object • Client asks rmiregistry for server object (by name) • Client receives a remote interface reference: • proxy/“stub”/representative of remote server object • Client invokes methods of remote interface on the remote interface reference

  17. Chapter 20 Weather Example: Interface TemperatureServer TemperatureServerImpl TemperatureClient Implementation RMI Client RMI Server Remote Interfaces • The declaration of behavior (the interface) is different from implementation of behavior (class) • Built separately (often by different programmers) • Run separately (often on different machines) • Servers implement behavior and expose interfaces • Clients rely on interfaces to invoke servers

  18. RMI Registry • Associates server object instances with names • Runs on each server machine • Uses port 1099 as a default • Started by the rmiregistry utility program • Accessed by the client to get a proxy (stub) to the server object

  19. The Java RMI Mechanism • RMI is built on top of (uses) the JVM • Activates remote objects • Manages and monitors communication connections • Tracks invocable objects • Typically based on TCP sockets • Stream-based, IP address (DNS name), port number • Uses unicast (point-to-point) • Is being extended to use Internet Inter-ORB Protocol (IIOP) • Laying the foundation for greater CORBA compatibility • Most of this happens “behind the scenes”

  20. RMI Layered Architecture Client Program Server Program Stubs & Skeletons Stubs & Skeletons Remote Reference Layer Remote Reference Layer RMI System Transport Layer Based on http://developer.java.sun.com/developer/onlineTraining/rmi

  21. Using RMI • Define interfaces • Implement servers • Build clients that use the servers • Compile the pieces and run them

  22. Server Side • The server class implements a remote interface • A remote interface is one that extends the java.rmi.Remote interface • e.g., public interface myInterface extends Remote • java.rmi.Remote is a “tagging” interface (i.e., it has no methods, like the Serializable interface) • Each declared method throws java.rmi.RemoteException

  23. Server Side (continued) • A server class typically extends java.rmi.UnicastRemoteObject • Could alternatively invoke java.rmi.UnicastRemoteObject static exportObject() method • Either approach makes instances of the class known to the RMI mechanism • UnicastRemoteObject manages networking/communications details for server • also keeps JVM running while server “waits” for remote method calls • Constructor has to throw RemoteException • Generally also address security issues associated with downloadable code (more later)

  24. Server Side (concluded) • In main() (typically) • Create an instance of the class as needed • Register the instance with the RMI Registry • Use Naming.rebind(serverObjName, objRef) where serverObjName is a concatenation of the hostname (or IP address) and a name by which clients will find the server • Implement the server logic exactly as you would for any local class

  25. sunny.jpg sunny.jpg sunny.jpg National Weather Service Web Site Proxy/Stub traveler.html (Serializable) TemperatureServerImpl_Stub WeatherInfo Remote Interface WeatherItem TemperatureServer RMI RMI Registry Implementation TemperatureServerImpl TemperatureClient Remote method calls RMI Example: Deitel Chapter 20Interactions RMI Server RMI Client

  26. National Weather Service Web Site traveler.html Interface Proxy/Stub TemperatureServer.class TemperatureServerImpl_Stub.class Implementation TemperatureServerImpl.class RMI Example: Deitel Chapter 20Examine Server-Side Code WeatherInfo.class RMI Server

  27. Deitel & Deitel Chapter 20 RMI ExampleChanges The National Weather Service changed the format of their Traveler's Forecast web page slightly. Make the following changes to the file TemperatureServerImpl.java to fix the problem: Line 35 should be changed from String separator = "</PRE><HR> <BR><PRE>"; to String separator = "TAV12"; Line 58 should be changed from while ( !inputLine.equals( "" ) ) {to while ( inputLine.length() > 28 ) { Recompile the file and regenerate your stub file before running the server. From http://www.deitel.com

  28. Client Side • All a client JVM knows how to do is to make local method invocations • Client objects invoke server objects via “stubs” • A “stub” is a local proxy for a remote object • The stub knows how to deal with the RMI mechanism • The client code treats the proxy as if it were the remote object • Other than that, the client just has to know where to look for a remote object of a specific name

  29. Client Side (concluded) • Contact the registry to find the object • Install a Security Manager as needed • Find the remote object on the server () • Naming.lookup(serverObjName) • Returns a ref to a Remote (an object that implements a Remote interface) • Make sure that what you get is what you want (i.e., implements the desired interface) • if (ref instanceof myInterrface) { } • Use it as desired • Be sure to do the above in a try - catch (to get RemoteException)

  30. sunny.jpg sunny.jpg sunny.jpg RMI Example: Deitel Chapter 20Examine Client-Side Code WeatherItem.class TemperatureClient.class RMI Client

  31. National Weather Service Web Site traveler.html RMI Example: Deitel Chapter 20Examine National Weather Service .html TAV12 TRAVELERS FORECAST TABLE NATIONAL WEATHER SERVICE - WASHINGTON D. C. TEMPERATURES INDICATE DAYTIME HIGH..NIGHTTIME LOW B INDICATES TEMPERATURES BELOW ZERO FORECAST FORECAST MON....MAR 20 TUE....MAR 21 CITY WEA HI/LO WEA HI/LO ALBANY NY PTCLDY 50/32 MOCLDY 48/31 ANCHORAGE RNSNOW 39/30 RNSNOW 41/25 ATLANTA PTCLDY 61/42 PTCLDY 72/47

  32. Process #1: rmiregistry

  33. Process #2: server

  34. Process #3: client

  35. Compilation & Execution • Compilation: • Compile pieces as normal using javac • If client and server in separate directories, need class file provider (http or ftp server) • Compile the server (a second time) using the RMI compiler rmic • Produces a stub class for the client (could be made available via a download) • Execution: • 3 Processes: • rmiregistry • server • client

  36. sunny.jpg sunny.jpg sunny.jpg National Weather Service Web Site traveler.html WeatherInfo.class Proxy/Stub TemperatureServerImpl_Stub.class Remote Interface TemperatureServer.class WeatherItem.class Implementation TemperatureClient.class TemperatureServerImpl.class RMI Example: Deitel Chapter 20Class and File Dependencies RMI Server RMI Client

  37. Serialization & RMI • RMI uses serialization to pack/unpack arguments and return values (via a process known as marshalling) • All primitive types are serializable • User-defined classes must: • Implement/extend the Serializable interface • Have a public null constructor • Only have references to serializable objects • Static variables are not transmitted

  38. Ramifications of RMI Serialization • Local arguments and return values are passed by value • If it has to be serialized to travel from one process (JVM) to another, it is copied • Remote objects passed to or returned from a remote object’s methods • Actually get a reference to the stub for that remote object • Thus get a pass-by-reference semantics for this case

  39. What About Polymorphism? • Serialization only works for data, not methods • We occasionally want server types of mechanisms to invoke mechanisms we create using polymorphism (e.g., the paint() method of a JFrame) • RMI supports the dynamic loading of code

  40. Dynamic Loading of Code • Server side can accept unknown (at compile time) subclasses as arguments to method invocations • Requires such a class to create a codebase • Tells the JVM where to get the code from (just like moving applets around in the Web!) • Contains a URL that can be used to download the code • All RMI does is annotate the serialized data with the URL • Is used to transmit stubs around as well as customized code • The client does not have to receive stubs ahead of time

  41. Security Issues • RMI supports downloading code at runtime • E.g., getting stubs, remote polymorphism • RMI provides a SecurityManager to enable the use of downloadable code • Implements a rudimentary security policy (e.g., all code signed by Bill can write to this directory) • See system.getSecurityManager(), system. setSecurityManager(), and class RMISecurityManager for more details

  42. RMI Activation Framework • Mechanism for starting server-side objects from the client side • Activate remote objects when needed • Deactivate them when not needed • Useful when: • Have rarely used remote objects • Have many remote objects • To reconstitute remote services after a crash • Requires special code

  43. Some Notes About RMI • The distributed environment is more fragile (need to worry more about exceptions) • Network connections can go down • Remote machines can go down • Garbage collection • Remote references have a lease period • If no local and no remote references, becomes a candidate for GC • Invokes the unreferenced method of the Unreferenced interface (if implemented) vs finalize

  44. Some Notes About RMI (continued) • A remote object can be richer than the interface it presents to the outside world • Not all of its methods have to be exposed • A remote object can expose multiple sets of interfaces • E.g., an admin interface and a functional interface • Higher parts of inheritance hierarchies can be remoted • Base classes become servers • Java does not use a separate Interface Definition Language (IDL) • JAVA RMI is specific to Java • Java does not use an Object Request Broker (ORB) • Some typical ORB services are built into the RMI mechanism (e.g., naming, security)

  45. A Short RMI Quiz http://developer.java.sun.com/developer/Quizzes/rmi

  46. For More RMI Information... http://developer.java.sun.com/developer/onlineTraining/rmi - RMI Tutorial - RMI Exercises

More Related