next up previous contents
Next: DistributedHashtable Up: RpcDispatcher Previous: RpcDispatcher   Contents

Example

The code below shows an example:

  public class RpcDispatcherTest {
      Channel            channel;
      RpcDispatcher      disp;
      RspList            rsp_list;
      String             props; // set by application

      public int print(int number) throws Exception {
          return number * 2;
      }

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

          for(int i=0; i < 10; i++) {
              Util.sleep(100);
              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 RpcDispatcher defines method print() which will be called subsequently. The entry point start() method creates a channel and an RpcDispatcher which is layered on top. Method callRemoteMethods() then 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 building 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.



Bela Ban 2002-11-16