public class PullPushTest implements MessageListener {
private Channel channel;
private PullPushAdapter conv;
public void Receive(Message msg) {
System.out.println("Received msg: " + msg);
}
public void Start() throws Exception {
channel=new EnsChannel("PullPushTest", null);
conv=new PullPushAdapter(channel, 1);
conv.AddListener(this);
channel.Connect(1000);
for(int i=0; i < 10; i++) {
channel.Cast(new String("Hello world").getBytes());
Thread.currentThread().sleep(1000);
}
channel.Disconnect();
channel.Destroy();
}
public static void main(String args[]) {
PullPushTest t=new PullPushTest();
try {t.Start();}
catch(Exception e) {System.err.println(e);}
}
}
Contrary to using channels (using pull-style message reception), here no separate thread has to be allocated to receive message. Instead, a PullPushAdapter is layered on top of the channel and a reference to the client object added. This causes the client's Receive method to be called whenever a message has been received by the PullPushAdapter. Note that compared to the pull-style example, push-style message reception is considerably easier (no separate thread management) and requires less code to program.
For an example showing the use of the pull-style see section 2.2.1.