KickJava   Java API By Example, From Geeks To Geeks.

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


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

25 public class RpcDispatcherSimpleTest {
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 RpcDispatcherSimpleTest().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