KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > RpcDispatcherTest


1 // $Id: RpcDispatcherTest.java,v 1.8 2004/09/07 12:07:31 belaban Exp $
2

3
4 package org.jgroups.tests;
5
6
7 import org.jgroups.Channel;
8 import org.jgroups.JChannel;
9 import org.jgroups.blocks.GroupRequest;
10 import org.jgroups.blocks.RpcDispatcher;
11 import org.jgroups.util.RspList;
12 import org.jgroups.util.Util;
13
14
15 /**
16  * Example for RpcDispatcher (see also MessageDispatcher). A remote method (print()) is group-invoked
17  * periodically. The method is defined in each instance and is invoked whenever a remote method call
18  * is received. The callee (although in this example, each callee is also a caller (peer principle))
19  * has to define the public methods, and the caller uses one of the callRemoteMethods() methods to
20  * invoke a remote method. CallRemoteMethods uses the core reflection API to lookup and dispatch
21  * methods.
22  *
23  * @author Bela Ban
24  */

25 public class RpcDispatcherTest {
26     Channel channel;
27     RpcDispatcher disp;
28     RspList rsp_list;
29     String JavaDoc props=null;
30
31
32     public int print(int number) throws Exception JavaDoc {
33         System.out.println("print(" + number + ')');
34         return number * 2;
35     }
36
37
38     public void start(int num, long interval) throws Exception JavaDoc {
39         channel=new JChannel(props);
40         channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
41         disp=new RpcDispatcher(channel, null, null, this);
42         channel.connect("RpcDispatcherTestGroup");
43
44         for(int i=0; i < num; i++) {
45             Util.sleep(interval);
46             rsp_list=disp.callRemoteMethods(null, "print", new Object JavaDoc[]{new Integer JavaDoc(i)},
47                     new Class JavaDoc[]{int.class}, GroupRequest.GET_ALL, 0);
48             System.out.println("Responses: " + rsp_list);
49         }
50         System.out.println("Closing channel");
51         channel.close();
52         System.out.println("Closing channel: -- done");
53
54         System.out.println("Stopping dispatcher");
55         disp.stop();
56         System.out.println("Stopping dispatcher: -- done");
57     }
58
59
60     public static void main(String JavaDoc[] args) {
61         int num=10;
62         long interval=1000;
63         for(int i=0; i < args.length; i++) {
64             if(args[i].equals("-num")) {
65                 num=Integer.parseInt(args[++i]);
66                 continue;
67             }
68             if(args[i].equals("-interval")) {
69                 interval=Long.parseLong(args[++i]);
70                 continue;
71             }
72             help();
73             return;
74         }
75
76         try {
77             new RpcDispatcherTest().start(num, interval);
78         }
79         catch(Exception JavaDoc e) {
80             System.err.println(e);
81         }
82     }
83
84     private static void help() {
85         System.out.println("RpcDispatcherTest [-help] [-num <number of msgs>] [-interval <sleep in ms between calls>]");
86     }
87 }
88
Popular Tags