use of javax.jms.JMSException in project camel by apache.
the class JmsBinding method extractHeadersFromJms.
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(JmsConstants.JMSX_GROUP_ID, JmsMessageHelper.getStringProperty(jmsMessage, JmsConstants.JMSX_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 = jmsJmsKeyFormatStrategy.decodeKey(name);
map.put(key, value);
} catch (JMSException e) {
throw new RuntimeCamelException(name, e);
}
}
}
return map;
}
use of javax.jms.JMSException in project camel by apache.
the class JmsBinding method extractBodyFromJms.
/**
* 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 {
// if we are configured to not map the jms message then return it as body
if (!mapJmsMessage) {
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);
}
}
use of javax.jms.JMSException in project camel by apache.
the class JmsBinding method createJmsMessage.
protected Message createJmsMessage(Exchange exchange, Object body, Map<String, Object> headers, Session session, CamelContext context) throws JMSException {
JmsMessageType type = null;
// TODO: support some of these options?
/* // 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 && !allowNullBody) {
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 && !allowNullBody) {
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;
}
use of javax.jms.JMSException in project camel by apache.
the class EndpointMessageListener method onMessage.
@Override
public void onMessage(Message message, Session session) throws JMSException {
LOG.trace("onMessage START");
LOG.debug("{} consumer received JMS message: {}", endpoint, message);
boolean sendReply;
RuntimeCamelException rce;
try {
Object replyDestination = getReplyToDestination(message);
// we can only send back a reply if there was a reply destination configured
// and disableReplyTo hasn't been explicit enabled
sendReply = replyDestination != null && !disableReplyTo;
// we should also not send back reply to ourself if this destination and replyDestination is the same
Destination destination = JmsMessageHelper.getJMSDestination(message);
if (destination != null && sendReply && !endpoint.isReplyToSameDestinationAllowed() && destination.equals(replyDestination)) {
LOG.debug("JMSDestination and JMSReplyTo is the same, will skip sending a reply message to itself: {}", destination);
sendReply = false;
}
final Exchange exchange = createExchange(message, session, replyDestination);
if (eagerLoadingOfProperties) {
exchange.getIn().getHeaders();
}
String correlationId = message.getJMSCorrelationID();
if (correlationId != null) {
LOG.debug("Received Message has JMSCorrelationID [{}]", correlationId);
}
// process the exchange either asynchronously or synchronous
LOG.trace("onMessage.process START");
AsyncCallback callback = new EndpointMessageListenerAsyncCallback(message, exchange, endpoint, sendReply, replyDestination);
// async is by default false, which mean we by default will process the exchange synchronously
// to keep backwards compatible, as well ensure this consumer will pickup messages in order
// (eg to not consume the next message before the previous has been fully processed)
// but if end user explicit configure consumerAsync=true, then we can process the message
// asynchronously (unless endpoint has been configured synchronous, or we use transaction)
boolean forceSync = endpoint.isSynchronous() || endpoint.isTransacted();
if (forceSync || !isAsync()) {
// must process synchronous if transacted or configured to do so
if (LOG.isTraceEnabled()) {
LOG.trace("Processing exchange {} synchronously", exchange.getExchangeId());
}
try {
processor.process(exchange);
} catch (Exception e) {
exchange.setException(e);
} finally {
callback.done(true);
}
} else {
// process asynchronous using the async routing engine
if (LOG.isTraceEnabled()) {
LOG.trace("Processing exchange {} asynchronously", exchange.getExchangeId());
}
boolean sync = processor.process(exchange, callback);
if (!sync) {
// will be done async so return now
return;
}
}
// if we failed processed the exchange from the async callback task, then grab the exception
rce = exchange.getException(RuntimeCamelException.class);
} catch (Exception e) {
rce = wrapRuntimeCamelException(e);
}
// the JMS listener will use the error handler to handle the uncaught exception
if (rce != null) {
LOG.trace("onMessage END throwing exception: {}", rce.getMessage());
// on the JmsEndpoint to handle the exception
throw rce;
}
LOG.trace("onMessage END");
}
use of javax.jms.JMSException in project camel by apache.
the class ConsumeJmsObjectMessageTest method testConsumeObjectMessage.
@Test
public void testConsumeObjectMessage() throws Exception {
endpoint.expectedMessageCount(1);
jmsTemplate.setPubSubDomain(false);
jmsTemplate.send("test.object", new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
ObjectMessage msg = session.createObjectMessage();
MyUser user = new MyUser();
user.setName("Claus");
msg.setObject(user);
return msg;
}
});
endpoint.assertIsSatisfied();
assertCorrectObjectReceived();
}
Aggregations