Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 19 Advanced Java Concepts Multithreading (continued)

Similar presentations


Presentation on theme: "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 19 Advanced Java Concepts Multithreading (continued)"— Presentation transcript:

1 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

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

3 Finishing our “Thread”... Code/DemosMonitors/Synchronization

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

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 synchronized static method ä Only one thread can execute a (any) synchronized static 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 20 Weather ä Use the National Weather Service Web site: ä http://iwin.nws.noaa.gov/iwin/us/traveler.html

13 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 Chapter 20 Weather Example: RMI Server TemperatureServer RMI Client TemperatureClient

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 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 Chapter 20 Weather Example: RMI Server TemperatureServerImpl RMI Client TemperatureClient Interface Implementation TemperatureServer

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 Based on Based on http://developer.java.sun.com/developer/onlineTraining/rmi Client Program Server Program Stubs & Skeletons Remote Reference Layer Transport Layer RMISystem

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

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

27 Deitel & Deitel Chapter 20 RMI Example Changes 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 = " "; 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 RMI Example: Deitel Chapter 20 Examine Client-Side Code TemperatureClient.class WeatherItem.class sunny.jpg sunny.jpg sunny.jpg RMI Client

31

32 RMI Example: Deitel Chapter 20 Examine National Weather Service.html traveler.html National Weather Service Web Site TAV12 TRAVELERS FORECAST TABLE NATIONAL WEATHER SERVICE - WASHINGTON D. C. TEMPERATURES INDICATE DAYTIME HIGH..NIGHTTIME LOW B INDICATES TEMPERATURES BELOW ZERO FORECAST FORECAST FORECAST FORECAST MON....MAR 20 TUE....MAR 21 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

33 Process #1: rmiregistry

34 Process #2: server

35 Process #3: client

36

37 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

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

39 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

40 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

41 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

42 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

43 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

44 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

45 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

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

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

48 For More RMI Information... http://developer.java.sun.com/developer/onlineTraini ng/rmi - RMI Tutorial - RMI Exercises


Download ppt "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 19 Advanced Java Concepts Multithreading (continued)"

Similar presentations


Ads by Google