01: package org.jgroups.tests;
02:
03: import org.jgroups.Channel;
04: import org.jgroups.JChannel;
05: import org.jgroups.blocks.GroupRequest;
06: import org.jgroups.blocks.RpcDispatcher;
07: import org.jgroups.util.RspList;
08: import org.jgroups.util.Util;
09:
10: /**
11: * Example for RpcDispatcher (see also MessageDispatcher). A remote method (print()) is group-invoked
12: * periodically. The method is defined in each instance and is invoked whenever a remote method call
13: * is received. The callee (although in this example, each callee is also a caller (peer principle))
14: * has to define the public methods, and the caller uses one of the callRemoteMethods() methods to
15: * invoke a remote method. CallRemoteMethods uses the core reflection API to lookup and dispatch
16: * methods.
17: *
18: * @author Bela Ban
19: * @version $Id: RpcDispatcherSimpleTest.java,v 1.1.2.1 2006/12/04 13:46:38 belaban Exp $
20: */
21: public class RpcDispatcherSimpleTest {
22: Channel channel;
23: RpcDispatcher disp;
24: RspList rsp_list;
25:
26: public int print(int number) throws Exception {
27: System.out.println("print(" + number + ')');
28: return number * 2;
29: }
30:
31: public void start(String props, int num, long interval)
32: throws Exception {
33: channel = new JChannel(props);
34: channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
35: disp = new RpcDispatcher(channel, null, null, this );
36: channel.connect("RpcDispatcherTestGroup");
37:
38: for (int i = 0; i < num; i++) {
39: Util.sleep(interval);
40: rsp_list = disp.callRemoteMethods(null, "print",
41: new Object[] { new Integer(i) },
42: new Class[] { int.class }, GroupRequest.GET_ALL, 0);
43: System.out.println("Responses:\n" + rsp_list);
44: }
45: System.out.println("Closing channel");
46: channel.close();
47: System.out.println("Closing channel: -- done");
48:
49: System.out.println("Stopping dispatcher");
50: disp.stop();
51: System.out.println("Stopping dispatcher: -- done");
52: }
53:
54: public static void main(String[] args) {
55: int num = 10;
56: long interval = 1000;
57: String props = null;
58: for (int i = 0; i < args.length; i++) {
59: if (args[i].equals("-num")) {
60: num = Integer.parseInt(args[++i]);
61: continue;
62: }
63: if (args[i].equals("-interval")) {
64: interval = Long.parseLong(args[++i]);
65: continue;
66: }
67: if (args[i].equals("-props")) {
68: props = args[++i];
69: continue;
70: }
71: help();
72: return;
73: }
74:
75: try {
76: new RpcDispatcherSimpleTest().start(props, num, interval);
77: } catch (Exception e) {
78: System.err.println(e);
79: }
80: }
81:
82: private static void help() {
83: System.out
84: .println("RpcDispatcherTest [-help] [-props <properties>] [-num <number of msgs>] [-interval <sleep in ms between calls>]");
85: }
86: }
|