next up previous contents
Next: MethodCall Up: PullPushAdapter Previous: PullPushAdapter

Example

        import JavaGroups.*;

        public class PullPushTest implements MessageListener {
            Channel          channel;
            PullPushAdapter  adapter;
            byte[]           data=new String("Hello world").getBytes();

            public void Receive(Message msg) {
                System.out.println("Received msg: " + msg);
            }

            public void Start() throws Exception {
                channel=new JChannel();
                channel.Connect("PullPushTest");
                adapter=new PullPushAdapter(channel);
                adapter.SetListener(this);
        
                for(int i=0; i < 10; i++) {
                    System.out.println("Sending msg #" + i);
                    channel.Send(new Message(null, null, data));
                    Thread.currentThread().sleep(1000);
                }
                adapter.Stop();
                channel.Close();
            }


            public static void main(String args[]) {
                try {
                    new PullPushTest().Start();
                }
                catch(Exception e) {
                    System.err.println(e);
                }
            }
        }

Contrary to using channels (using pull-style message reception), no separate thread has to be allocated to receive messages. 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.




1999-08-19