Title: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000
1UMass 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
2Homework 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
3Finishing our Thread...
- Code/Demos
- Monitors/Synchronization
4Javas 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
5The 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 Objects getClass method
- Can get the classs name, superclass, interfaces
- Can be locked for thread synchronization purposes
- A class lock is a lock on a class Class object
6synchronized Static Methods
- You can get a lock on a class (actually, the
classs Class object) by creating a synchronized
static method - Only one thread can execute a (any) synchronized
static method at a time
7Remote Method Invocation (RMI)
8Distributed 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
9Distributed 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
10Granularity 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
11Client-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
12RMI Example Deitel Chapter 20Weather
- Use the National Weather Service Web site
- http//iwin.nws.noaa.gov/iwin/us/traveler.html
13RMI 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
14RMI - 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)
15Java 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!
16Remote Objects Key Ideas
- Remote interface describes server objects
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
17Remote 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
18RMI 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
19The 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
20RMI 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/o
nlineTraining/rmi
21Using RMI
- Define interfaces
- Implement servers
- Build clients that use the servers
- Compile the pieces and run them
22Server 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.RemoteExcepti
on
23Server Side (continued)
- A server class typically extends
java.rmi.UnicastRemoteObject - Could alternatively invoke java.rmi.UnicastRemoteO
bject static exportObject() method - Either approach makes instances of the class
known to the RMI mechanism - UnicastRemoteObject manages networking/communicati
ons 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)
24Server 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
25RMI Example Deitel Chapter 20Interactions
RMI Server
RMI Client
26RMI Example Deitel Chapter 20Examine
Server-Side Code
WeatherInfo.class
RMI Server
27Deitel 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 "lt/PREgtltHRgt ltBRgtltPREgt" to String
separator "TAV12" Line 58 should be changed
from while ( !inputLine.equals( "" ) )
to while ( inputLine.length() gt 28 )
Recompile the file and regenerate your stub
file before running the server.
From http//www.deitel.com
28Client 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
29Client 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)
30RMI Example Deitel Chapter 20Examine
Client-Side Code
WeatherItem.class
TemperatureClient.class
RMI Client
31(No Transcript)
32RMI 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
33Process 1 rmiregistry
34Process 2 server
35Process 3 client
36(No Transcript)
37Compilation 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
38RMI Example Deitel Chapter 20Class and File
Dependencies
RMI Server
RMI Client
39Serialization 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
40Ramifications 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 objects methods - Actually get a reference to the stub for that
remote object - Thus get a pass-by-reference semantics for this
case
41What 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
42Dynamic 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
43Security 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
44RMI 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
45Some 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
46Some 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)
47A Short RMI Quiz
- http//developer.java.sun.com/developer/Quizzes/rm
i
48For More RMI Information...
- http//developer.java.sun.com/developer/onlineTrai
ning/rmi - - RMI Tutorial
- - RMI Exercises