01: // $Id: PullPushTest.java,v 1.7 2005/05/30 16:15:12 belaban Exp $
02:
03: package org.jgroups.tests;
04:
05: import org.jgroups.Channel;
06: import org.jgroups.JChannel;
07: import org.jgroups.Message;
08: import org.jgroups.MessageListener;
09: import org.jgroups.blocks.PullPushAdapter;
10:
11: /**
12: * Uses PullPush building block to send/receive messages. Reception is passive, e.g. the receiver's
13: * receive() method is invoked whenever a message is received. The receiver has to register a callback method
14: * when creating the channel.
15: * @author Bela Ban
16: */
17: public class PullPushTest implements MessageListener {
18: private Channel channel;
19: private PullPushAdapter adapter;
20:
21: public void receive(Message msg) {
22: System.out.println("Received msg: " + msg);
23: }
24:
25: public byte[] getState() { // only called if channel option GET_STATE_EVENTS is set to true
26: return null;
27: }
28:
29: public void setState(byte[] state) {
30:
31: }
32:
33: public void start() throws Exception {
34:
35: channel = new JChannel();
36: channel.connect("PullPushTest");
37: adapter = new PullPushAdapter(channel);
38: adapter.setListener(this );
39:
40: for (int i = 0; i < 10; i++) {
41: System.out.println("Sending msg #" + i);
42: adapter.send(new Message(null, null, "Hello world"
43: .getBytes()));
44: Thread.sleep(1000);
45: }
46:
47: channel.close();
48: }
49:
50: public static void main(String args[]) {
51: PullPushTest t = new PullPushTest();
52: try {
53: t.start();
54: } catch (Exception e) {
55: System.err.println(e);
56: }
57: }
58:
59: }
|