Ejemplo n.º 1
0
  public Map<String, Object> extractHeadersFromJms(Message jmsMessage, Exchange exchange) {
    Map<String, Object> map = new HashMap<String, Object>();
    if (jmsMessage != null) {
      // lets populate the standard JMS message headers
      try {
        map.put("JMSCorrelationID", jmsMessage.getJMSCorrelationID());
        map.put("JMSCorrelationIDAsBytes", JmsMessageHelper.getJMSCorrelationIDAsBytes(jmsMessage));
        map.put("JMSDeliveryMode", jmsMessage.getJMSDeliveryMode());
        map.put("JMSDestination", jmsMessage.getJMSDestination());
        map.put("JMSExpiration", jmsMessage.getJMSExpiration());
        map.put("JMSMessageID", jmsMessage.getJMSMessageID());
        map.put("JMSPriority", jmsMessage.getJMSPriority());
        map.put("JMSRedelivered", jmsMessage.getJMSRedelivered());
        map.put("JMSTimestamp", jmsMessage.getJMSTimestamp());

        map.put("JMSReplyTo", JmsMessageHelper.getJMSReplyTo(jmsMessage));
        map.put("JMSType", JmsMessageHelper.getJMSType(jmsMessage));

        // this works around a bug in the ActiveMQ property handling
        map.put(JMS_X_GROUP_ID, JmsMessageHelper.getStringProperty(jmsMessage, JMS_X_GROUP_ID));
        map.put("JMSXUserID", JmsMessageHelper.getStringProperty(jmsMessage, "JMSXUserID"));
      } catch (JMSException e) {
        throw new RuntimeCamelException(e);
      }

      Enumeration<?> names;
      try {
        names = jmsMessage.getPropertyNames();
      } catch (JMSException e) {
        throw new RuntimeCamelException(e);
      }
      while (names.hasMoreElements()) {
        String name = names.nextElement().toString();
        try {
          Object value = JmsMessageHelper.getProperty(jmsMessage, name);
          if (headerFilterStrategy != null
              && headerFilterStrategy.applyFilterToExternalHeaders(name, value, exchange)) {
            continue;
          }

          // must decode back from safe JMS header name to original header name
          // when storing on this Camel JmsMessage object.
          String key = jmsKeyFormatStrategy.decodeKey(name);
          map.put(key, value);
        } catch (JMSException e) {
          throw new RuntimeCamelException(name, e);
        }
      }
    }

    return map;
  }
Ejemplo n.º 2
0
  void processReply(Session session, Message answer) throws JMSException {
    // when in InOnly mode the JMSReplyTo is a bit complicated
    // we only want to set the JMSReplyTo on the answer if
    // there is a JMSReplyTo from the header/endpoint and
    // we have been told to preserveMessageQos

    Object jmsReplyTo = JmsMessageHelper.getJMSReplyTo(answer);
    if (endpoint.isDisableReplyTo()) {
      // honor disable reply to configuration
      LOG.trace("ReplyTo is disabled on endpoint: {}", endpoint);
      JmsMessageHelper.setJMSReplyTo(answer, null);
    } else {
      // if the binding did not create the reply to then we have to try to create it here
      if (jmsReplyTo == null) {
        // prefer reply to from header over endpoint configured
        jmsReplyTo = exchange.getIn().getHeader("JMSReplyTo", String.class);
        if (jmsReplyTo == null) {
          jmsReplyTo = endpoint.getReplyTo();
        }
      }
    }

    // we must honor these special flags to preserve QoS
    // as we are not OUT capable and thus do not expect a reply, and therefore
    // the consumer of this message should not return a reply so we remove it
    // unless we use preserveMessageQos=true to tell that we still want to use JMSReplyTo
    if (jmsReplyTo != null
        && !(endpoint.isPreserveMessageQos() || endpoint.isExplicitQosEnabled())) {
      // log at debug what we are doing, as higher level may cause noise in production logs
      // this behavior is also documented at the camel website
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            "Disabling JMSReplyTo: {} for destination: {}. Use preserveMessageQos=true to force Camel to keep the JMSReplyTo on endpoint: {}",
            new Object[] {jmsReplyTo, to, endpoint});
      }
      jmsReplyTo = null;
    }

    // the reply to is a String, so we need to look up its Destination instance
    // and if needed create the destination using the session if needed to
    if (jmsReplyTo != null && jmsReplyTo instanceof String) {
      String replyTo = (String) jmsReplyTo;
      // we need to null it as we use the String to resolve it as a Destination instance
      jmsReplyTo = resolveOrCreateDestination(replyTo, session);
    }

    // set the JMSReplyTo on the answer if we are to use it
    Destination replyTo = null;
    String replyToOverride = endpoint.getConfiguration().getReplyToOverride();
    if (replyToOverride != null) {
      replyTo = resolveOrCreateDestination(replyToOverride, session);
    } else if (jmsReplyTo instanceof Destination) {
      replyTo = (Destination) jmsReplyTo;
    }
    if (replyTo != null) {
      LOG.debug("Using JMSReplyTo destination: {}", replyTo);
      JmsMessageHelper.setJMSReplyTo(answer, replyTo);
    } else {
      // do not use JMSReplyTo
      log.trace("Not using JMSReplyTo");
      JmsMessageHelper.setJMSReplyTo(answer, null);
    }

    LOG.trace("Created javax.jms.Message: {}", answer);
  }