diff --git a/spring-integration-jms/src/main/java/org/springframework/integration/jms/JmsDestinationPollingSource.java b/spring-integration-jms/src/main/java/org/springframework/integration/jms/JmsDestinationPollingSource.java index 747c40b253..b7b7831d2c 100644 --- a/spring-integration-jms/src/main/java/org/springframework/integration/jms/JmsDestinationPollingSource.java +++ b/spring-integration-jms/src/main/java/org/springframework/integration/jms/JmsDestinationPollingSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import org.springframework.integration.context.IntegrationObjectSupport; import org.springframework.integration.core.MessageSource; import org.springframework.integration.support.MessageBuilder; import org.springframework.jms.core.JmsTemplate; -import org.springframework.jms.support.converter.MessageConverter; import org.springframework.util.Assert; /** @@ -34,7 +33,7 @@ import org.springframework.util.Assert; * only recommended for very low message volume. Otherwise, the * {@link JmsMessageDrivenEndpoint} that uses Spring's MessageListener container * support is a better option. - * + * * @author Mark Fisher * @author Oleg Zhurakousky */ @@ -51,6 +50,8 @@ public class JmsDestinationPollingSource extends IntegrationObjectSupport implem private volatile JmsHeaderMapper headerMapper = new DefaultJmsHeaderMapper(); + private volatile boolean extractPayload = true; + public JmsDestinationPollingSource(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } @@ -66,6 +67,16 @@ public class JmsDestinationPollingSource extends IntegrationObjectSupport implem this.destinationName = destinationName; } + /** + * The flag to indicate if we should extract {@code body} from JMS Message, + * or use the received JMS Message as {@link Message} {@code payload}. + * @param extractPayload the boolean flag. Defaults to {@code true}. + * @since 3.0.7 + */ + public void setExtractPayload(boolean extractPayload) { + this.extractPayload = extractPayload; + } + @Override public String getComponentType() { return "jms:inbound-channel-adapter"; @@ -73,6 +84,7 @@ public class JmsDestinationPollingSource extends IntegrationObjectSupport implem /** * Specify a JMS Message Selector expression to use when receiving Messages. + * @param messageSelector The message selector. */ public void setMessageSelector(String messageSelector) { this.messageSelector = messageSelector; @@ -83,30 +95,32 @@ public class JmsDestinationPollingSource extends IntegrationObjectSupport implem } /** - * Will receive a JMS {@link javax.jms.Message} converting and returning it as + * Will receive a JMS {@link javax.jms.Message} converting and returning it as * a Spring Integration {@link Message}. This method will also use the current * {@link JmsHeaderMapper} instance to map JMS properties to the MessageHeaders. */ + @Override @SuppressWarnings("unchecked") public Message receive() { - Message convertedMessage = null; - javax.jms.Message jmsMessage = this.doReceiveJmsMessage(); + javax.jms.Message jmsMessage = doReceiveJmsMessage(); if (jmsMessage == null) { return null; } try { // Map headers Map mappedHeaders = this.headerMapper.toHeaders(jmsMessage); - MessageConverter converter = this.jmsTemplate.getMessageConverter(); - Object convertedObject = converter.fromMessage(jmsMessage); - MessageBuilder builder = (convertedObject instanceof Message) - ? MessageBuilder.fromMessage((Message) convertedObject) : MessageBuilder.withPayload(convertedObject); - convertedMessage = builder.copyHeadersIfAbsent(mappedHeaders).build(); + Object object = jmsMessage; + if (this.extractPayload) { + object = this.jmsTemplate.getMessageConverter().fromMessage(jmsMessage); + } + MessageBuilder builder = (object instanceof Message) ? + MessageBuilder.fromMessage((Message) object) : + MessageBuilder.withPayload(object); + return builder.copyHeadersIfAbsent(mappedHeaders).build(); } catch (Exception e) { throw new MessagingException(e.getMessage(), e); } - return convertedMessage; } private javax.jms.Message doReceiveJmsMessage() { diff --git a/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsInboundChannelAdapterParser.java b/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsInboundChannelAdapterParser.java index a9decccdd7..05e9cf7270 100644 --- a/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsInboundChannelAdapterParser.java +++ b/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsInboundChannelAdapterParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,13 +29,16 @@ import org.springframework.util.StringUtils; * Parser for the <inbound-channel-adapter/> element of the 'jms' namespace. * * @author Mark Fisher + * @author Artem Bilan */ public class JmsInboundChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser { + @Override protected boolean shouldGenerateId() { return false; } + @Override protected boolean shouldGenerateIdAsFallback() { return true; } @@ -84,6 +87,7 @@ public class JmsInboundChannelAdapterParser extends AbstractPollingInboundChanne builder.addPropertyReference(JmsAdapterParserUtils.HEADER_MAPPER_PROPERTY, headerMapper); } IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "selector", "messageSelector"); + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload"); return builder.getBeanDefinition(); } diff --git a/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/jmsInboundWithConnectionFactoryAndDestination.xml b/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/jmsInboundWithConnectionFactoryAndDestination.xml index d15818e15f..b1739d8db6 100644 --- a/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/jmsInboundWithConnectionFactoryAndDestination.xml +++ b/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/jmsInboundWithConnectionFactoryAndDestination.xml @@ -10,9 +10,14 @@ http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd"> - + destination="testDestination" + extract-payload="false"/> + +