KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: PullPushTest.java,v 1.5 2004/07/05 14:15:11 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.Message;
10 import org.jgroups.MessageListener;
11 import org.jgroups.blocks.PullPushAdapter;
12
13
14 /**
15  * Uses PullPush building block to send/receive messages. Reception is passive, e.g. the receiver's
16  * receive() method is invoked whenever a message is received. The receiver has to register a callback method
17  * when creating the channel.
18  * @author Bela Ban
19  */

20 public class PullPushTest implements MessageListener {
21     private Channel channel;
22     private PullPushAdapter adapter;
23
24     public void receive(Message msg) {
25         System.out.println("Received msg: " + msg);
26     }
27
28     public byte[] getState() { // only called if channel option GET_STATE_EVENTS is set to true
29
return null;
30     }
31
32     public void setState(byte[] state) {
33
34     }
35
36
37     public void start() throws Exception JavaDoc {
38
39         channel=new JChannel();
40         channel.connect("PullPushTest");
41         adapter=new PullPushAdapter(channel);
42         adapter.setListener(this);
43
44         for(int i=0; i < 10; i++) {
45             System.out.println("Sending msg #" + i);
46             adapter.send(new Message(null, null, "Hello world".getBytes()));
47             Thread.sleep(1000);
48         }
49
50         channel.close();
51     }
52
53
54     public static void main(String JavaDoc args[]) {
55         PullPushTest t=new PullPushTest();
56         try {
57             t.start();
58         }
59         catch(Exception JavaDoc e) {
60             System.err.println(e);
61         }
62     }
63
64 }
65
Popular Tags