1CMSC 433 – Programming LanguageTechnologies and ParadigmsSpring 2006Java RMI2Distributed Computing• Programs that cooperate and communicateover a network– E-mail– Web server and web client– SETI @Home23Key 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 fail4Different Approaches toDistributed Computation• Connecting via sockets– E.g., project 1, 5 (part II)– 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 return35Remote Method Invocation• Easy way to get distributed computation• Have stub for remote object– Calls to stub get translated into network call– Implemented on top of sockets• Arguments and return values are passedover network– Java takes care of the details6A Simple 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()); }} }47Remote Objects• Object should– Extend java.rmi.server.UnicastRemoteObject• Constructor declared to throw RemoteException– Implement a remote interface• A remote interface extends java.rmi.Remote• All methods in a remote interface throwRemoteException– “Something bad happened on the network”– Side note: actually, don’t need to extendUnicastRemoteObject, but it’s much easier8Remote Interfaces59Stubs• 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 network10Compiling Stubs with rmic• Generates stub code for a class– For 1.1, also generates skeleton class• Stub on client side communicates with skeleton onremote side– Skeleton not needed for 1.2+• And 1.2+ generates position-independent code• Use -v1.2 if you want• Generates stubs for all methods declared inthe class’ Remote interface– Other methods don’t get a stub611Passing Arguments• To pass an argument to a remote method– (Or return a result from a remote method)– It 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 expect12Passing Serializable vs. Remote• Serializable objects passed by value– Same Serializable in different calls materializesdifferent objects at receiver• Remote objects passed by reference– Same Remote object in different calls yieldssame stub object, which passes arguments backto same remote object713Stub Code• Objects contain both data and code– When you receive a remote object, you needthe stub for that remote object• Solution #1: All clients have stub code ontheir classpath– Or stub code for another class with sameremote interface14Downloading Code• Solution #2: Provide a code base wherestub code for objects can be downloadedjava -Djava.rmi.server.codebase=<url> ...– Specifies location of classes originating fromthis server– URL can be, e.g., http:// or file:/815Getting 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 byremote methods16Starting 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 machine917Advertising Remote Objects• Call Naming.{bind/unbind/rebind} to placeobjects in registry– E.g., Naming.bind(“rmi://localhost/Chat”);• Can bind/unbind/rebind name on localhost• Can lookup name on any host18Example: 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 room1019Serverinterface Server extends Remote {Connection logon(String name, Client c)throws RemoteException;}20Connectioninterface 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;}1121Clientinterface Client extends Remote {void said(String who, String msg)throws RemoteException; void whoChanged(String [] who)throws RemoteException;}22Server’s Remote Object creationServerImplServer s = new ServerImpl();HostedRemote ObjectssServerObject added to tablebecause it implementsextension of Remoteinterface1223Remote Object registryServerImplNaming.rebind(“ChatServer”, s);HostedRemote ObjectsChatServerServerImplStubsServer RMI Registry24Client’s Remote Object creationClientImplClient c = new ClientImpl();HostedRemote ObjectscClientClient object alsoimplements extensionof Remote interface1325Client looks up ServerServer s = (Server) Naming.lookup (“//host/ChatServer”);sServerImplStubHostedRemote ObjectsServerImplClient ServerRMI RegistryChatServerServerImplStublookupreturnsstub26After lookup finishedClientImplHostedRemote ObjectscsServerImplStubHostedRemote ObjectsServerImplClient Server1427Invokes remote Server methodConnection conn = s.logon(“Bill”, c);sServerImplStubClientStub codefor remotelogon callString “Bill”Stub for cMethod: logon… to server processlogonClientImplc28Receives remote callServer(Skeleton)code forremotelogon callString “Bill”Stub for cMethod: logon… from client processHostedRemote ObjectsServerImpl“Bill”ClientImplStub cunmarshalled
View Full Document