next up previous contents
Next: DistributedHashtable Up: Building blocks Previous: MessageDispatcher

   
RpcDispatcher

This class is derived from MessageDispatcher and is the application-level equivalent of RpcProtocol (see 4.3.2 for more details.) It allows one to invoke remote methods in all (or single) group members and optionally wait for the return value(s). An application would typically create a channel and layer the RpcDispatcher builing block on top of it, which allows it to dispatch remote method invocations and at the same time be invoked by other members. The code below shows an example:

        public class RpcDispatcherTest {
            Channel            channel;
            RpcDispatcher      disp;
            RspList            rsp_list;


            public int Print(int number) {
                System.out.println("Print(" + number + ")");
                return number * 2;
            }


            public void Start() throws Exception {
                channel=new JChannel();
                disp=new RpcDispatcher(channel, null, null, this);
                channel.Connect("RpcDispatcherTestGroup");

                for(int i=0; i < 10; i++) {
                    Util.Sleep(1000);
                    rsp_list=disp.CallRemoteMethods(null, "Print", new Integer(i), 
                                                    GroupRequest.GET_ALL, 0);
                    System.out.println("Responses: " +rsp_list);
                }
                channel.Close();
                disp.Stop();
            }


            public static void main(String[] args) {
                try {
                    new RpcDispatcherTest().Start();
                }
                catch(Exception e) {
                    System.err.println(e);
                }
            }
        }

Class RpcDipatcher defines method Print which will be invoked subsequently. The entry point Start method creates a channel and an RpcDispatcher which is layered on top of the channel. Method CallRemoteMethods invokes the remote Print method in all group members (also in the caller). When all responses have been received, the call returns and the responses are printed.

As can be seen, the RpcDispatcher builidng block reduces the amount of code that needs to be written to implement RPC-based group communication applications by providing a higher abstraction level between the application and the primitive channels.




1999-12-13