public void onUpdates(List<StockPriceEmitter.Update> updates) {
    for (StockPriceEmitter.Update update : updates) {
      // Create the channel name using the stock symbol
      String channelName = "/stock/" + update.getSymbol().toLowerCase(Locale.ENGLISH);

      // Initialize the channel, making it persistent and lazy
      bayeuxServer.createChannelIfAbsent(
          channelName,
          new ConfigurableServerChannel.Initializer() {
            public void configureChannel(ConfigurableServerChannel channel) {
              channel.setPersistent(true);
              channel.setLazy(true);
            }
          });

      // Convert the Update business object to a CometD-friendly format
      Map<String, Object> data = new HashMap<String, Object>(4);
      data.put("symbol", update.getSymbol());
      data.put("oldValue", update.getOldValue());
      data.put("newValue", update.getNewValue());
      System.out.println("===========================");
      // Publish to all subscribers
      ServerChannel channel = bayeuxServer.getChannel(channelName);
      channel.publish(sender, data);
    }
  }
 @PreDestroy
 public void destroy() throws Exception {
   Oort oort = counter.getOort();
   oort.deobserveChannel(CHANNEL);
   BayeuxServer bayeuxServer = oort.getBayeuxServer();
   bayeuxServer.getChannel(CHANNEL).setPersistent(false);
   bayeuxServer.removeListener(this);
   counter.stop();
 }
Beispiel #3
0
 @Override
 protected void doStop() throws Exception {
   oort.deobserveChannel(broadcastChannelName);
   BayeuxServer bayeuxServer = oort.getBayeuxServer();
   bayeuxServer.getChannel(resultChannelName).removeListener(this);
   bayeuxServer.getChannel(broadcastChannelName).removeListener(this);
   bayeuxServer.getChannel(forwardChannelName).removeListener(this);
   session.disconnect();
   logger.debug("Stopped {}", this);
 }
Beispiel #4
0
 @Override
 protected void doStart() throws Exception {
   session.handshake();
   BayeuxServer bayeuxServer = oort.getBayeuxServer();
   bayeuxServer.createChannelIfAbsent(forwardChannelName).getReference().addListener(this);
   bayeuxServer.createChannelIfAbsent(broadcastChannelName).getReference().addListener(this);
   bayeuxServer.createChannelIfAbsent(resultChannelName).getReference().addListener(this);
   oort.observeChannel(broadcastChannelName);
   logger.debug("Started {}", this);
 }
 @PostConstruct
 public void construct() throws Exception {
   counter.start();
   Oort oort = counter.getOort();
   BayeuxServer bayeuxServer = oort.getBayeuxServer();
   bayeuxServer.addListener(this);
   bayeuxServer.createChannelIfAbsent(
       CHANNEL, new ConfigurableServerChannel.Initializer.Persistent());
   oort.observeChannel(CHANNEL);
 }
Beispiel #6
0
 @Override
 protected void doStop() throws Exception {
   oort.deobserveChannel(broadcastChannelName);
   BayeuxServer bayeuxServer = oort.getBayeuxServer();
   ServerChannel channel = bayeuxServer.getChannel(resultChannelName);
   if (channel != null) {
     channel.removeListener(this);
   }
   channel = bayeuxServer.getChannel(broadcastChannelName);
   if (channel != null) {
     channel.removeListener(this);
   }
   channel = bayeuxServer.getChannel(forwardChannelName);
   if (channel != null) {
     channel.removeListener(this);
   }
   session.disconnect();
   if (logger.isDebugEnabled()) {
     logger.debug("Stopped {}", this);
   }
 }