KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: MessageDispatcherTest.java,v 1.11 2004/07/29 09:08:52 belaban Exp $
2

3 package org.jgroups.tests;
4
5
6 import org.jgroups.Channel;
7 import org.jgroups.JChannel;
8 import org.jgroups.Message;
9 import org.jgroups.blocks.GroupRequest;
10 import org.jgroups.blocks.MessageDispatcher;
11 import org.jgroups.blocks.RequestHandler;
12 import org.jgroups.util.RspList;
13 import org.jgroups.util.Util;
14
15
16 /**
17  * Example for MessageDispatcher (see also RpcDispatcherTest). Message is periodically broadcast to all
18  * members; handle() method is invoked whenever a message is received.
19  */

20 public class MessageDispatcherTest implements RequestHandler {
21     Channel channel;
22     MessageDispatcher disp;
23     RspList rsp_list;
24     String JavaDoc props=null;
25
26
27     public void start() throws Exception JavaDoc {
28         channel=new JChannel(props);
29         //channel.setOpt(Channel.LOCAL, Boolean.FALSE);
30
// disp=new MessageDispatcher(channel, null, null, this);
31
disp=new MessageDispatcher(channel, null, null, this,
32                 false, // deadlock detection is disabled
33
true); // concurrent processing is enabled
34
channel.connect("MessageDispatcherTestGroup");
35
36 // for(int i=0; i < 10; i++) {
37
// //Util.sleep(1000);
38
// System.out.println("Casting message #" +i);
39
// rsp_list=disp.castMessage(null,
40
// new Message(null, null, new String("Number #" +i).getBytes()),
41
// GroupRequest.GET_ALL, 0);
42
// System.out.println("Responses:\n" + rsp_list);
43
// }
44

45         MyThread t1=new MyThread("one"), t2=new MyThread("two");
46         t1.start();
47         t2.start();
48         t1.join();
49         t2.join();
50
51         System.out.println("** Disconnecting channel");
52         channel.disconnect();
53         System.out.println("** Disconnecting channel -- done");
54
55         System.out.println("** Closing channel");
56         channel.close();
57         System.out.println("** Closing channel -- done");
58
59         System.out.println("** disp.stop()");
60         disp.stop();
61         System.out.println("** disp.stop() -- done");
62
63         //Util.printThreads();
64
//Util.sleep(2000);
65
//Util.printThreads();
66
}
67
68
69     class MyThread extends Thread JavaDoc {
70         public MyThread(String JavaDoc name) {
71             setName(name);
72         }
73
74         public void run() {
75             for(int i=0; i < 10; i++) {
76                 System.out.println('[' + getName() + "] casting message #" +i);
77                 rsp_list=disp.castMessage(null,
78                         new Message(null, null, '[' + getName() + "] number #" + i),
79                         GroupRequest.GET_ALL, 0);
80                 System.out.println('[' + getName() + "] responses:\n" + rsp_list);
81             }
82         }
83     }
84
85
86     public Object JavaDoc handle(Message msg) {
87         System.out.println("handle(): " + msg.getObject());
88         Util.sleepRandom(2000);
89         return "Success !";
90     }
91
92
93     public static void main(String JavaDoc[] args) {
94
95         try {
96             new MessageDispatcherTest().start();
97         }
98         catch(Exception JavaDoc e) {
99             System.err.println(e);
100         }
101     }
102 }
103
Popular Tags