01: package demo.events;
02:
03: /**
04: * @authors Joerg v. Frantzius, Rainer Lischetzki, Gerald Brose 1997
05: *
06: * A simple demo for using the event channel as a pull consumer
07: * of events
08: *
09: */
10:
11: import org.omg.CosNaming.*;
12: import org.omg.CosEventChannelAdmin.*;
13: import org.omg.CosEventComm.*;
14: import org.omg.CORBA.Any;
15:
16: public class PullConsumerDemo extends PullConsumerPOA {
17: public PullConsumerDemo(org.omg.CORBA.ORB orb) {
18: _this _object(orb);
19: }
20:
21: public void disconnect_pull_consumer() {
22: System.out.println("Consumer disconnected");
23: }
24:
25: static public void main(String argv[]) {
26: EventChannel ecs = null;
27: ConsumerAdmin ca;
28: ProxyPullSupplier pps;
29: PullConsumer pullConsumer;
30: Any received = null;
31: org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(argv, null);
32:
33: // binding the event channel reference
34: try {
35: org.omg.PortableServer.POA poa = org.omg.PortableServer.POAHelper
36: .narrow(orb.resolve_initial_references("RootPOA"));
37:
38: poa.the_POAManager().activate();
39: NamingContextExt nc = NamingContextExtHelper.narrow(orb
40: .resolve_initial_references("NameService"));
41: ecs = EventChannelHelper.narrow(nc.resolve(nc
42: .to_name("eventchannel.example")));
43: } catch (Exception e) {
44: e.printStackTrace();
45: }
46:
47: // registering ourselves
48:
49: pullConsumer = (org.omg.CosEventComm.PullConsumer) (new PullConsumerDemo(
50: orb))._this ();
51: ca = ecs.for_consumers();
52: pps = ca.obtain_pull_supplier();
53: try {
54: pps.connect_pull_consumer((PullConsumer) pullConsumer);
55: } catch (Exception e) {
56: e.printStackTrace();
57: }
58:
59: System.out.println("TestPullConsumer registered.");
60:
61: // pulling events
62: int i = 0;
63: while (i < 10) {
64: System.out.println("pulling event " + i);
65: org.omg.CORBA.BooleanHolder bh = new org.omg.CORBA.BooleanHolder();
66: try {
67: received = pps.try_pull(bh);
68: // received = pps.pull();
69: if (bh.value) {
70: System.out.println("received " + (i++) + " : "
71: + received.extract_string());
72: } else {
73: // we did not get any real any, so we continue
74: // polling after a short nap
75: Thread.currentThread().sleep(2000);
76: }
77: } catch (Exception e) {
78: e.printStackTrace();
79: }
80: }
81: pps.disconnect_pull_supplier();
82: }
83: }
|