Lecture 5: RMI etc. Java Remote Method Invocation Invocation Semantics Distributed Events CDK: Chapter 5 TVS: Section 8.3 CDK Figure 5.7 The role of proxy and skeleton in remote method invocation client object A proxy for B Request Reply server remote skeleton object B & dispatcher for B s class Remote Communication reference module module servant Communication Remote reference module module 14-Feb-11 COMP28112 Lecture 5 2 Servant An instance of a class which is referenced remotely It exists within the server, which created it and the remote reference to it It does the real work of the method 14-Feb-11 COMP28112 Lecture 5 3 1
The Dispatcher Server has a dispatcher & a skeleton for each class of remote object The dispatcher receives the incoming message, and uses its method info to pass it to the right method in the skeleton. (Dispatcher and proxy both work from the remote interface to get same method info!) 14-Feb-11 COMP28112 Lecture 5 4 Skeleton Skeleton implements methods of the remote interface to unmarshall arguments and invoke the corresponding method in the servant. It then marshalls the result (or any exceptions thrown) into a reply message to the proxy. 14-Feb-11 COMP28112 Lecture 5 5 Remote Reference Module Responsible for translating between remote references and local ones Has a remote object table, recording: An entry for each remote object local to that process (each process has its own table!) An entry for each local proxy Kept up-to-date appropriately 14-Feb-11 COMP28112 Lecture 5 6 2
Proxy There is one proxy per remote object a process can reference The class of the proxy contains a method for each method in the remote interface which marshalls arguments and unmarshalls results for that method. 14-Feb-11 COMP28112 Lecture 5 7 Garbage Collection of Remote Objects Once references to objects can be remote, Garbage Collection is more difficult Garbage Collection is done locally to the process but remote objects are not removed Each server keeps a set of clients with nonlocal references Each client tells server when removing proxy 14-Feb-11 COMP28112 Lecture 5 8 Generating classes for proxies, dispatchers and skeletons Before JDK1.5, there was a separate tool to use to generate these: rmic With JDK1.5 (and later) you don t need to do this It is done using reflection That can also simplify other things. 14-Feb-11 COMP28112 Lecture 5 9 3
Reflection in Java Reflection allows a Java program to inspect and manipulate itself It can get an object representing a class whose name is a String It can then find out about this class s constructors, and methods, and their arguments 14-Feb-11 COMP28112 Lecture 5 10 Reflection continued It can then invoke these to construct objects and call their methods E.g. Class c = Class.forname( MyExClass ); Constructors [] cc = c.getdeclaredconstructors() ; etc. 14-Feb-11 COMP28112 Lecture 5 11 rmiregistry This is how a server makes a remote object available to clients A string is bound to the remote object, and the clients interrogate the registry using the string The client must know the server machine name and the port the registry is on (there is a default) 14-Feb-11 COMP28112 Lecture 5 12 4
Invocation Semantics This applies to both RPC and RMI For both normal method invocation and normal procedure call, we have exactly once semantics So what can we say in a distributed system where machines can crash and messages can be lost? 14-Feb-11 COMP28112 Lecture 5 13 Failures Request could be lost Reply could be lost Server could fail Client could fail 14-Feb-11 COMP28112 Lecture 5 14 Request Behaviour What does client do if it makes a request, but gets no reply (after long enough)? It could just fail (not usual behaviour) It could resend the request If after several resends, still no reply -> failure 14-Feb-11 COMP28112 Lecture 5 15 5
Reply Behaviour What should the server do on getting a request? If it doesn t know it is a duplicate, it just processes it (perhaps again) It could filter duplicates if it remembers requests, and then: Discard duplicate if it hasn t yet replied It could retransmit the reply (if it remembers it) 14-Feb-11 COMP28112 Lecture 5 16 CDK Figure 5.6 Invocation semantics Fault tolerance measures Invocation semantics Retransmit request message Duplicate filtering Re-execute procedure or retransmit reply No Not applicable Not applicable Maybe Yes No Re-execute procedure At-least-once Yes Yes Retransmit reply At-most-once 14-Feb-11 COMP28112 Lecture 5 17 Maybe semantics The invocation may or may not happen It is less effort but not generally satisfactory 14-Feb-11 COMP28112 Lecture 5 18 6
At-least-once semantics Unless the server crashes, this means that the invocation definitely happens But it might happen more than once! OK for idempotent operations, ones which Can be performed >1 time with no different effect to that when performed once Sun RPC does this 14-Feb-11 COMP28112 Lecture 5 19 At-most-once semantics Requires extra effort Operation won t be done more than once! Java RMI provides this 14-Feb-11 COMP28112 Lecture 5 20 Distributed Event-based Systems You can build a distributed application using RPC or RMI in a normal way Distributed event-based systems show another possibility Events are notified to those objects which subscribe to (i.e. register interest in) an object 14-Feb-11 COMP28112 Lecture 5 21 7
Events are Published There is a way in which objects which are prepared to notify events to subscribers can make these known s are sent asynchronously to all subscribers 14-Feb-11 COMP28112 Lecture 5 22 Dealing room system Dealer s computer External source Dealer s computer Dealer Dealer Information provider Dealer s computer Dealer Information provider External source Dealer s computer Dealer 14-Feb-11 COMP28112 Lecture 5 23 Next Lecture I will talk about the 2 nd lab exercise This starts next week, and will occupy 3 sessions! 14-Feb-11 COMP28112 Lecture 5 24 8