/** * Extracts the body from the JMS message * * @param exchange the exchange * @param message the message to extract its body * @return the body, can be <tt>null</tt> */ public Object extractBodyFromJms(Exchange exchange, Message message) { try { // is a custom message converter configured on endpoint then use it instead of doing the // extraction // based on message type if (endpoint != null && endpoint.getMessageConverter() != null) { if (LOG.isTraceEnabled()) { LOG.trace( "Extracting body using a custom MessageConverter: {} from JMS message: {}", endpoint.getMessageConverter(), message); } return endpoint.getMessageConverter().fromMessage(message); } // if we are configured to not map the jms message then return it as body if (endpoint != null && !endpoint.getConfiguration().isMapJmsMessage()) { LOG.trace("Option map JMS message is false so using JMS message as body: {}", message); return message; } if (message instanceof ObjectMessage) { LOG.trace("Extracting body as a ObjectMessage from JMS message: {}", message); ObjectMessage objectMessage = (ObjectMessage) message; Object payload = objectMessage.getObject(); if (payload instanceof DefaultExchangeHolder) { DefaultExchangeHolder holder = (DefaultExchangeHolder) payload; DefaultExchangeHolder.unmarshal(exchange, holder); return exchange.getIn().getBody(); } else { return objectMessage.getObject(); } } else if (message instanceof TextMessage) { LOG.trace("Extracting body as a TextMessage from JMS message: {}", message); TextMessage textMessage = (TextMessage) message; return textMessage.getText(); } else if (message instanceof MapMessage) { LOG.trace("Extracting body as a MapMessage from JMS message: {}", message); return createMapFromMapMessage((MapMessage) message); } else if (message instanceof BytesMessage) { LOG.trace("Extracting body as a BytesMessage from JMS message: {}", message); return createByteArrayFromBytesMessage((BytesMessage) message); } else if (message instanceof StreamMessage) { LOG.trace("Extracting body as a StreamMessage from JMS message: {}", message); return message; } else { return null; } } catch (JMSException e) { throw new RuntimeCamelException( "Failed to extract body due to: " + e + ". Message: " + message, e); } }
protected Message createJmsMessage( Exchange exchange, Object body, Map<String, Object> headers, Session session, CamelContext context) throws JMSException { JmsMessageType type = null; // special for transferExchange if (endpoint != null && endpoint.isTransferExchange()) { LOG.trace("Option transferExchange=true so we use JmsMessageType: Object"); Serializable holder = DefaultExchangeHolder.marshal(exchange); Message answer = session.createObjectMessage(holder); // ensure default delivery mode is used by default answer.setJMSDeliveryMode(Message.DEFAULT_DELIVERY_MODE); return answer; } // use a custom message converter if (endpoint != null && endpoint.getMessageConverter() != null) { if (LOG.isTraceEnabled()) { LOG.trace( "Creating JmsMessage using a custom MessageConverter: {} with body: {}", endpoint.getMessageConverter(), body); } return endpoint.getMessageConverter().toMessage(body, session); } // check if header have a type set, if so we force to use it if (headers.containsKey(JmsConstants.JMS_MESSAGE_TYPE)) { type = context .getTypeConverter() .convertTo(JmsMessageType.class, headers.get(JmsConstants.JMS_MESSAGE_TYPE)); } else if (endpoint != null && endpoint.getConfiguration().getJmsMessageType() != null) { // force a specific type from the endpoint configuration type = endpoint.getConfiguration().getJmsMessageType(); } else { type = getJMSMessageTypeForBody(exchange, body, headers, session, context); } // create the JmsMessage based on the type if (type != null) { if (body == null && (endpoint != null && !endpoint.getConfiguration().isAllowNullBody())) { throw new JMSException( "Cannot send message as message body is null, and option allowNullBody is false."); } LOG.trace("Using JmsMessageType: {}", type); Message answer = createJmsMessageForType(exchange, body, headers, session, context, type); // ensure default delivery mode is used by default answer.setJMSDeliveryMode(Message.DEFAULT_DELIVERY_MODE); return answer; } // check for null body if (body == null && (endpoint != null && !endpoint.getConfiguration().isAllowNullBody())) { throw new JMSException( "Cannot send message as message body is null, and option allowNullBody is false."); } // warn if the body could not be mapped if (body != null && LOG.isWarnEnabled()) { LOG.warn( "Cannot determine specific JmsMessage type to use from body class." + " Will use generic JmsMessage." + " Body class: " + ObjectHelper.classCanonicalName(body) + ". If you want to send a POJO then your class might need to implement java.io.Serializable" + ", or you can force a specific type by setting the jmsMessageType option on the JMS endpoint."); } // return a default message Message answer = session.createMessage(); // ensure default delivery mode is used by default answer.setJMSDeliveryMode(Message.DEFAULT_DELIVERY_MODE); return answer; }