1 CMSC 433 – Programming Language Technologies and Paradigms Spring 2009 Java RMI 2 Distributed Computing • Programs that cooperate and communicate over a network – E-mail – Web server and web client – SETI @Home2 3 Key Features of Distrib. Comp. • Machines are not all the same – But all adhere to same communication protocol • Network is “slow” – Sending a message takes a lot of time • Network is unreliable – Machines may join and leave with no warning – Part of the network may fail 4 Distributing Computations • Connecting via sockets – E.g., project 1 – Custom protocols for each application • RPC/DCOM/CORBA/RMI – Make what looks like a normal function call – Function actually invoked on another machine – Arguments are marshalled for transport – Value is unmarshalled on return3 5 Remote Method Invocation • Easy way to get distributed computation • Have proxy for remote object – Calls to proxy get translated into network call – Implemented on top of sockets • Arguments and return values are passed over network – Java takes care of the details 6 A Simplified Example // runs on one mach. class ChatServerImpl implements ChatServer ... { public void say(String s) { System.out.println(s); } ... } class Chatter { // runs on another mach. public static void main(String args[]) { ChatServer c = // get remote object; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.print(“> “); c.say(br.readLine()); } } }4 Remote Objects • Objects implement a Remote interface • A remote interface extends java.rmi.Remote • All interface methods throw RemoteException • Constructor throws RemoteException • RemoteException means “something bad happened on the network” 7 8 Remote Interfaces5 9 Stubs • Client only sees the RemoteInterface – ConcreteObject can have other methods • Remote objects represented using stub – Stub sends arguments over network – Stub receives result back from network 10 Compiling Stubs with rmic* • RMI compiler – *Don’t need to use rmic anymore • Generates stub code for a class – For 1.1, also generates skeleton class • Stub on client side communicates with skeleton on remote side – Skeleton not needed for 1.2+ • Generates stubs for all methods declared in the class’ Remote interface – Other methods don’t get a stub6 11 Passing Arguments • To pass an argument to a remote method or return a result from a remote method, arg must be either – A primitive type (int, double, etc.), – Serializable (e.g., String), or – Remote (i.e., implement a sub-interface of Remote) • Primitives passed as you’d expect 12 Passing Serializable vs. Remote • Serializable objects passed by value – Same Serializable in different calls materializes different objects at receiver • Remote objects passed by reference – Same Remote object in different calls yields same stub object, which passes arguments back to same remote object7 13 Stub Code • Objects contain both data and code – When you receive a remote object, you need the stub for that remote object • Where does it come from? • Solution #1: All clients have stub code on their classpath – Or stub code for another class with same remote interface 14 Downloading Code • Solution #2: Provide a codebase where stub code for objects can be downloaded java -Djava.rmi.server.codebase=<url> ... – Specifies location of classes orig. from this jvm – URL can be, e.g., http:// or file:/8 15 Getting the First Remote Object • Can make objects available in RMI registry – Each object has a name (that you specify) – Registry listens on a port (1099 default) • Naming.lookup(url) gets object from reg. – e.g., Naming.lookup(“rmi://localhost/Chat”); – Use to get first reference to remote object – Don’t need to lookup objects returned by remote methods 16 Starting an RMI Registry • Method 1: Separate RMI registry process – Command rmiregistry • Run with stubs in classpath, or specify codebase – Listens on port 1099 by default • Method 2: Start in same JVM – LocateRegistry.createRegistry(int port) – Advantage: dies when your program dies • No registries lying around on machine9 Exporting the Remote Object • UnicastRemoteObject.exportObject(Remote, int) exports the remote object so that it can receive invocations of its remote methods from remote clients • The second argument specifies which TCP port to listen on for incoming remote invocation requests for the object. – The value zero specifies the use of an anonymous port. • Method returns a stub for the exported remote object 17 18 Advertising Remote Objects • Call Naming.{bind/unbind/rebind} to place objects in registry – E.g., Naming.bind(“rmi://localhost/Chat”); • Can bind/unbind/rebind name on localhost • Can lookup name on any host10 19 Example: RMI Chat Server • Server – Runs the chat room • Client – Participant in chat room – Receives messages from others in room • Connection – Uniquely identifies a client – Used to speak in chat room 20 Server interface Server extends Remote { Connection logon(String name, Client c) throws RemoteException; public void notifyWhoChanged() throws RemoteException; public Map<String,Client> who() throws RemoteException; }11 21 Connection interface Connection extends Remote { /** Say to everyone */ void say(String msg) throws RemoteException; / ** Say to one person */ void say(String who, String msg) throws RemoteException; String [] who() throws RemoteException; void logoff() throws RemoteException; } 22 Client interface Client extends Remote { void speak(String who, String msg) throws RemoteException; void whoChanged(String [] who) throws RemoteException; }12 23 Server’s Remote Object creation ServerImpl Server s = new ServerImpl(); Hosted Remote Objects s Server Object added to table because it implements extension of Remote interface 24 Remote Object registry ServerImpl Naming.rebind(“ChatServer”, s); Hosted Remote
View Full Document