Exemple #1
0
  /**
   * Creates a JMS message from the Camel exchange and message
   *
   * @param exchange the current exchange
   * @param camelMessage the body to make a javax.jms.Message as
   * @param session the JMS session used to create the message
   * @param cause optional exception occurred that should be sent as reply instead of a regular body
   * @return a newly created JMS Message instance containing the
   * @throws JMSException if the message could not be created
   */
  public Message makeJmsMessage(
      Exchange exchange, org.apache.camel.Message camelMessage, Session session, Exception cause)
      throws JMSException {
    Message answer = null;

    boolean alwaysCopy = endpoint != null && endpoint.getConfiguration().isAlwaysCopyMessage();
    boolean force = endpoint != null && endpoint.getConfiguration().isForceSendOriginalMessage();
    if (!alwaysCopy && camelMessage instanceof JmsMessage) {
      JmsMessage jmsMessage = (JmsMessage) camelMessage;
      if (!jmsMessage.shouldCreateNewMessage() || force) {
        answer = jmsMessage.getJmsMessage();

        if (!force) {
          // answer must match endpoint type
          JmsMessageType type =
              endpoint != null ? endpoint.getConfiguration().getJmsMessageType() : null;
          if (type != null && answer != null) {
            if (type == JmsMessageType.Text) {
              answer = answer instanceof TextMessage ? answer : null;
            } else if (type == JmsMessageType.Bytes) {
              answer = answer instanceof BytesMessage ? answer : null;
            } else if (type == JmsMessageType.Map) {
              answer = answer instanceof MapMessage ? answer : null;
            } else if (type == JmsMessageType.Object) {
              answer = answer instanceof ObjectMessage ? answer : null;
            } else if (type == JmsMessageType.Stream) {
              answer = answer instanceof StreamMessage ? answer : null;
            }
          }
        }
      }
    }

    if (answer == null) {
      if (cause != null) {
        // an exception occurred so send it as response
        LOG.debug("Will create JmsMessage with caused exception: {}", cause);
        // create jms message containing the caused exception
        answer = createJmsMessage(cause, session);
      } else {
        ObjectHelper.notNull(camelMessage, "message");
        // create regular jms message using the camel message body
        answer =
            createJmsMessage(
                exchange,
                camelMessage.getBody(),
                camelMessage.getHeaders(),
                session,
                exchange.getContext());
        appendJmsProperties(answer, exchange, camelMessage);
      }
    }

    if (answer != null && messageCreatedStrategy != null) {
      messageCreatedStrategy.onMessageCreated(answer, session, exchange, null);
    }
    return answer;
  }
Exemple #2
0
  /**
   * Creates a JMS message from the Camel exchange and message
   *
   * @param exchange the current exchange
   * @param camelMessage the body to make a javax.jms.Message as
   * @param session the JMS session used to create the message
   * @param cause optional exception occurred that should be sent as reply instead of a regular body
   * @return a newly created JMS Message instance containing the
   * @throws JMSException if the message could not be created
   */
  public Message makeJmsMessage(
      Exchange exchange, org.apache.camel.Message camelMessage, Session session, Exception cause)
      throws JMSException {
    Message answer = null;

    boolean alwaysCopy = endpoint != null && endpoint.getConfiguration().isAlwaysCopyMessage();
    boolean force = endpoint != null && endpoint.getConfiguration().isForceSendOriginalMessage();
    if (!alwaysCopy && camelMessage instanceof JmsMessage) {
      JmsMessage jmsMessage = (JmsMessage) camelMessage;
      if (!jmsMessage.shouldCreateNewMessage() || force) {
        answer = jmsMessage.getJmsMessage();
      }
    }

    if (answer == null) {
      if (cause != null) {
        // an exception occurred so send it as response
        LOG.debug("Will create JmsMessage with caused exception: {}", cause);
        // create jms message containing the caused exception
        answer = createJmsMessage(cause, session);
      } else {
        ObjectHelper.notNull(camelMessage, "message");
        // create regular jms message using the camel message body
        answer =
            createJmsMessage(
                exchange,
                camelMessage.getBody(),
                camelMessage.getHeaders(),
                session,
                exchange.getContext());
        appendJmsProperties(answer, exchange, camelMessage);
      }
    }

    return answer;
  }