public void afterPropertiesSet() throws Exception {

    List<JetstreamTopic> publishingTopics = m_address.getChannelJetstreamTopics();

    for (JetstreamTopic topic : publishingTopics) {

      MessageService.getInstance().prepareToPublish(topic);
    }
  }
  /** @param topic */
  private void subscribe(JetstreamTopic topic) {
    try {

      LOGGER.info(
          "Subscribing to Topic - " + topic.getTopicName() + " on Outbound Messaging Channel");

      MessageService.getInstance().subscribe(topic, this);
    } catch (MessageServiceException e) {
      m_errors.registerError(e);
      LOGGER.error("Error Subscribing for Topic - " + topic.getTopicName() + e.getMessage());

    } catch (Exception e) {
      m_errors.registerError(e);
      LOGGER.error("Error Subscribing for Topic - " + topic.getTopicName() + e.getMessage());
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see com.ebay.jetstream.event.channel.ChannelOperations#close()
   */
  public void close() throws EventException {

    LOGGER.info("Closing Outbound Messaging Chanel");

    if (!m_channelOpened.get()) return;

    super.close();

    Management.removeBeanOrFolder(getBeanName(), this);

    m_channelOpened.set(false);

    if (getAdviceListener() != null) {
      List<JetstreamTopic> publishingTopics = m_address.getChannelJetstreamTopics();

      for (JetstreamTopic topic : publishingTopics) {

        try {
          MessageService.getInstance()
              .unsubscribe(
                  new JetstreamTopic(topic.getRootContext() + "/InternalStateAdvisory"), this);
        } catch (MessageServiceException e) {
          m_errors.registerError(e);
          String errMsg = "Error Unsubscribing for Topic - " + topic.getTopicName();
          errMsg += " - ";
          errMsg += e.getMessage();

          LOGGER.error(errMsg);

        } catch (Throwable e) {
          m_errors.registerError(e);
          LOGGER.error("Error unSubscribing for Topic - " + topic.getTopicName() + e.getMessage());
        }
      }
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see com.ebay.jetstream.event.EventSink#sendEvent(com.ebay.jetstream.event.JetstreamEvent)
   */
  public void sendEvent(JetstreamEvent event) throws EventException {

    incrementEventRecievedCounter();

    List<JetstreamTopic> publishingTopics = m_address.getChannelJetstreamTopics();

    String[] forwardingTopics = event.getForwardingTopics();

    for (JetstreamTopic topic : publishingTopics) {

      if (forwardingTopics != null) {
        boolean bFound = false;
        String strSeekName = topic.getTopicName();
        for (String strTopic : forwardingTopics) {
          if (strTopic.equals(strSeekName)) {
            bFound = true;
            break;
          }
        }
        if (!bFound) continue;
      }

      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(
            "eventSource="
                + getBeanName()
                + "&eventId = "
                + event.getEventId()
                + "&topic="
                + topic);
      }

      Any any = null;
      if (isConstructEventHolder()) {
        JetstreamEvent holderEvent = constructTEventHolder(event);
        any = new Any(holderEvent);
      } else {
        any = new Any(event);
      }

      any.setPriority(JetstreamMessage.HI_PRIORITY);

      if (event.containsKey(affinityKey)) {

        Object affkey = event.get(affinityKey);

        if (affkey == null) {
          incrementEventDroppedCounter();
          if (LOGGER.isDebugEnabled()) LOGGER.debug(" Affinity Key passed in is null");
          return;
        }

        any.setAffinityKey(affkey);

        if (LOGGER.isDebugEnabled()) {

          LOGGER.debug(
              "sending on topic - "
                  + topic.getTopicName()
                  + " "
                  + ": Affinity Key = "
                  + event.get(affinityKey).toString()
                  + " EVENT - "
                  + event.toString());
        }
      }

      // we assume that caller will not set affinity key and bcast flag at the same time
      // if affinity key is set it will override bcast

      if (event.getMetaData(bcastMsg) != null) {

        any.setBroadcastMessage(Boolean.parseBoolean((String) event.getMetaData(bcastMsg)));

        if (LOGGER.isDebugEnabled()) {

          LOGGER.debug(
              "sending on topic - "
                  + topic.getTopicName()
                  + " "
                  + ": BroadCast Key = "
                  + event.getMetaData(bcastMsg).toString()
                  + " EVENT - "
                  + event.toString());
        }
      }
      try {
        MessageService.getInstance().publish(topic, any);
        setLastEvent(event);
        incrementEventSentCounter();
      } catch (MessageServiceException mse) {

        m_errors.registerError(mse);

        String errMsg = "Error Publishing Message for Topic - " + topic.getTopicName(); // NOPMD
        errMsg += " - ";
        errMsg += mse.getMessage();

        if (LOGGER.isWarnEnabled()) {
          LOGGER.warn(errMsg);
        }
        sendToAdviceListener(event, RetryEventCode.UNKNOWN, errMsg);

        incrementEventDroppedCounter();

        if (mse.getError() == MessageServiceException.BUFFER_FULL && !m_alarmRaised.get()) {
          setTopicAlarmState(topic.getRootContext());
          if (areAllContextsInAlarmCondition()) {
            getAlarmListener().alarm(ChannelAlarm.OVERRUN);
            postAlert("MessageService Buffer full condition - ", AlertStrength.YELLOW);
            LOGGER.info("Outbound Messaging Channel raising alarm");
            m_alarmRaised.set(true);
            incrementAlarmsRaisedCounter();
          }
        }

      } catch (Throwable t) {
        m_errors.registerError(t);
        String errMsg = "Error Publishing Message for Topic - " + topic.getTopicName();
        errMsg += " - ";
        errMsg += t.getMessage();
        sendToAdviceListener(event, RetryEventCode.UNKNOWN, errMsg);
        incrementEventDroppedCounter();
        if (LOGGER.isDebugEnabled()) LOGGER.debug(errMsg);
      }
    }
  }