public void start() throws Exception { int c; channel = new JChannel(); channel.connect("PullPushTestMux"); adapter = new PullPushAdapter(channel); adapter.setListener(this); listeners = new MyListener[10]; for (int i = 0; i < listeners.length; i++) { listeners[i] = new MyListener(i, adapter); } while ((c = choice()) != 'q') { c -= 48; if (c < 0 || c > 9) { System.err.println("Choose between 0 and 9"); continue; } if (c == 0) adapter.send(new Message(null, null, "Message from default message listener")); else listeners[c].sendMessage(); } channel.close(); System.exit(0); }
MyListener(int id, PullPushAdapter ad) { this.id = new Integer(id); this.ad = ad; ad.registerListener(this.id, this); }
void sendMessage() throws Exception { Message msg = new Message(null, null, "Message from " + id); ad.send(id, msg); }