INT-1271, removed DefaultMessageConverter and tests related to it

This commit is contained in:
Oleg Zhurakousky
2010-09-01 03:06:35 +00:00
parent 53bc3ebf18
commit c504e4dcd8
6 changed files with 47 additions and 208 deletions

View File

@@ -34,7 +34,7 @@ import org.springframework.util.Assert;
* @author Oleg Zhurakousky
*/
public abstract class AbstractJmsTemplateBasedAdapter extends IntegrationObjectSupport {
private volatile boolean extractPayload = true;
private volatile ConnectionFactory connectionFactory;
@@ -200,7 +200,7 @@ public abstract class AbstractJmsTemplateBasedAdapter extends IntegrationObjectS
if (this.messageConverter != null) {
this.jmsTemplate.setMessageConverter(this.messageConverter);
}
this.configureMessageConverter(this.jmsTemplate);
//this.configureMessageConverter(this.jmsTemplate);
this.initialized = true;
}
}
@@ -221,12 +221,13 @@ public abstract class AbstractJmsTemplateBasedAdapter extends IntegrationObjectS
return jmsTemplate;
}
protected void configureMessageConverter(JmsTemplate jmsTemplate) {
MessageConverter converter = jmsTemplate.getMessageConverter();
if (converter == null || !(converter instanceof DefaultMessageConverter)) {
DefaultMessageConverter dmc = new DefaultMessageConverter(converter);
dmc.setExtractIntegrationMessagePayload(this.extractPayload);
jmsTemplate.setMessageConverter(dmc);
}
// protected void configureMessageConverter(JmsTemplate jmsTemplate) {
// MessageConverter converter = jmsTemplate.getMessageConverter();
// if (converter == null) {
// jmsTemplate.setMessageConverter(new SimpleMessageConverter());
// }
// }
protected boolean shouldExtractPayload() {
return extractPayload;
}
}

View File

@@ -31,6 +31,7 @@ import org.springframework.integration.gateway.AbstractMessagingGateway;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.SimpleMessageConverter;
import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.jms.support.destination.DynamicDestinationResolver;
import org.springframework.util.Assert;
@@ -50,7 +51,7 @@ public class ChannelPublishingJmsMessageListener extends AbstractMessagingGatewa
private volatile boolean expectReply;
private volatile MessageConverter messageConverter;
private volatile MessageConverter messageConverter = new SimpleMessageConverter();
private volatile boolean extractRequestPayload = true;
@@ -204,24 +205,20 @@ public class ChannelPublishingJmsMessageListener extends AbstractMessagingGatewa
this.extractReplyPayload = extractReplyPayload;
}
public final void onInit() throws Exception {
if (!(this.messageConverter instanceof DefaultMessageConverter)) {
DefaultMessageConverter hmmc = new DefaultMessageConverter(this.messageConverter);
hmmc.setExtractJmsMessageBody(this.extractRequestPayload);
hmmc.setExtractIntegrationMessagePayload(this.extractReplyPayload);
this.messageConverter = hmmc;
}
super.onInit();
}
@SuppressWarnings("unchecked")
public void onMessage(javax.jms.Message jmsMessage, Session session) throws JMSException {
Object object = this.messageConverter.fromMessage(jmsMessage);
Object result = jmsMessage;
if (this.extractRequestPayload) {
result = this.messageConverter.fromMessage(jmsMessage);
if (logger.isDebugEnabled()) {
logger.debug("converted JMS Message [" + jmsMessage + "] to integration Message payload [" + result + "]");
}
}
Map<String, Object> headers = (Map<String, Object>) headerMapper.toHeaders(jmsMessage);
Message<?> requestMessage = (object instanceof Message<?>) ?
MessageBuilder.fromMessage((Message<?>) object).copyHeaders(headers).build() :
MessageBuilder.withPayload(object).copyHeaders(headers).build();
Message<?> requestMessage = (result instanceof Message<?>) ?
MessageBuilder.fromMessage((Message<?>) result).copyHeaders(headers).build() :
MessageBuilder.withPayload(result).copyHeaders(headers).build();
if (!this.expectReply) {
this.send(requestMessage);
}
@@ -231,7 +228,11 @@ public class ChannelPublishingJmsMessageListener extends AbstractMessagingGatewa
Destination destination = this.getReplyDestination(jmsMessage, session);
if (destination != null){
// convert SI Message to JMS Message
javax.jms.Message jmsReply = this.messageConverter.toMessage(replyMessage, session);
Object replyResult = replyMessage;
if (this.extractReplyPayload){
replyResult = replyMessage.getPayload();
}
javax.jms.Message jmsReply = this.messageConverter.toMessage(replyResult, session);
// map SI Message Headers to JMS Message Properties/Headers
headerMapper.fromHeaders(replyMessage.getHeaders(), jmsReply);

View File

@@ -1,144 +0,0 @@
/*
* Copyright 2002-2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.jms;
import javax.jms.JMSException;
import javax.jms.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.Message;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.SimpleMessageConverter;
/**
* A {@link MessageConverter} implementation that is capable of delegating to
* an existing converter instance. The default MessageConverter implementation
* is {@link SimpleMessageConverter}.
*
* <p>If 'extractJmsMessageBody' is <code>true</code> (the default), the body
* of each received JMS Message will be converted. Otherwise, the JMS Message
* itself will be returned.
*
* <p>If 'extractIntegrationMessagePayload' is <code>true</code> (the default),
* the payload of each outbound Spring Integration Message will be passed to
* the MessageConverter to produce the body of the JMS Message. Otherwise, the
* Spring Integration Message itself will become the body of the JMS Message.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class DefaultMessageConverter implements MessageConverter {
private final Log logger = LogFactory.getLog(this.getClass());
private final MessageConverter converter;
private volatile boolean extractJmsMessageBody = true;
private volatile boolean extractIntegrationMessagePayload = true;
/**
* Create a DefaultMessageConverter instance that will rely on the
* default {@link SimpleMessageConverter}.
*/
public DefaultMessageConverter() {
this(null);
}
/**
* Create a DefaultMessageConverter instance that will delegate to
* the provided {@link MessageConverter} instance.
*/
public DefaultMessageConverter(MessageConverter converter) {
this.converter = (converter != null ? converter : new SimpleMessageConverter());
}
/**
* Specify whether the inbound JMS Message's body should be extracted
* during the conversion process. Otherwise, the raw JMS Message itself
* will be returned to be used as the payload.
*
* <p>The default value is <code>true</code>.
*/
public void setExtractJmsMessageBody(boolean extractJmsMessageBody) {
this.extractJmsMessageBody = extractJmsMessageBody;
}
/**
* Specify whether the outbound integration Message's payload should be
* extracted prior to conversion into a JMS Message. Otherwise, the
* integration Message itself will be passed to the converter.
*
* <p>Typically, this setting should be determined by the expectations of
* the target system. If the target system is not capable of understanding
* a Spring Integration Message, then set this to <code>true</code>.
* On the other hand, if the system is not only capable of understanding a
* Spring Integration Message but actually expected to rely upon Spring
* Integration Message Header values, then this must be set to
* <code>false</code> to ensure that the actual Message will be passed
* along with its Serializable headers.
*
* <p>The default value is <code>true</code>.
*/
public void setExtractIntegrationMessagePayload(boolean extractIntegrationMessagePayload) {
this.extractIntegrationMessagePayload = extractIntegrationMessagePayload;
}
/**
* Converts from a JMS {@link javax.jms.Message} to an integration Message payload.
* If the 'extractJmsMessageBody' property is <code>false</code>, the JMS Message itself
* will be returned to be used as the payload.
*/
public Object fromMessage(javax.jms.Message jmsMessage) throws JMSException, MessageConversionException {
Object result = null;
if (this.extractJmsMessageBody) {
result = this.converter.fromMessage(jmsMessage);
if (logger.isDebugEnabled()) {
logger.debug("converted JMS Message [" + jmsMessage + "] to integration Message payload [" + result + "]");
}
}
else {
result = jmsMessage;
if (logger.isDebugEnabled()) {
logger.debug("returning JMS Message [" + jmsMessage + "] as the Message payload.");
}
}
return result;
}
/**
* Converts from an integration Message to a JMS {@link javax.jms.Message}.
*/
public javax.jms.Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
javax.jms.Message jmsMessage = null;
if (object instanceof Message<?>) {
if (this.extractIntegrationMessagePayload) {
object = ((Message<?>) object).getPayload();
}
}
jmsMessage = this.converter.toMessage(object, session);
if (logger.isDebugEnabled()) {
logger.debug("converted [" + object + "] to JMS Message [" + jmsMessage + "]");
}
return jmsMessage;
}
}

View File

@@ -77,7 +77,7 @@ public class JmsOutboundGateway extends AbstractReplyProducingMessageHandler {
private ConnectionFactory connectionFactory;
private volatile MessageConverter messageConverter;
private volatile MessageConverter messageConverter = new SimpleMessageConverter();
private volatile JmsHeaderMapper headerMapper = new DefaultJmsHeaderMapper();
@@ -288,12 +288,6 @@ public class JmsOutboundGateway extends AbstractReplyProducingMessageHandler {
Assert.notNull(this.connectionFactory, "connectionFactory must not be null");
Assert.isTrue(this.requestDestination != null || this.requestDestinationName != null,
"Either a 'requestDestination' or 'requestDestinationName' is required.");
if (this.messageConverter == null) {
DefaultMessageConverter hmmc = new DefaultMessageConverter(null);
hmmc.setExtractIntegrationMessagePayload(this.extractRequestPayload);
hmmc.setExtractJmsMessageBody(this.extractReplyPayload);
this.messageConverter = hmmc;
}
this.initialized = true;
}
}
@@ -310,7 +304,14 @@ public class JmsOutboundGateway extends AbstractReplyProducingMessageHandler {
throw new MessageTimeoutException(message,
"failed to receive JMS response within timeout of: " + this.receiveTimeout + "ms");
}
return this.messageConverter.fromMessage(jmsReply);
Object result = jmsReply;
if (this.extractRequestPayload) {
result = this.messageConverter.fromMessage(jmsReply);
if (logger.isDebugEnabled()) {
logger.debug("converted JMS Message [" + jmsReply + "] to integration Message payload [" + result + "]");
}
}
return result;
}
catch (JMSException e) {
throw new MessageHandlingException(requestMessage, e);
@@ -326,7 +327,11 @@ public class JmsOutboundGateway extends AbstractReplyProducingMessageHandler {
try {
session = createSession(connection);
// convert to JMS Message
javax.jms.Message jmsRequest = this.messageConverter.toMessage(requestMessage, session);
Object objectToSend = requestMessage;
if (this.extractReplyPayload){
objectToSend = requestMessage.getPayload();
}
javax.jms.Message jmsRequest = this.messageConverter.toMessage(objectToSend, session);
// map headers
headerMapper.fromHeaders(requestMessage.getHeaders(), jmsRequest);
// create JMS Producer and send

View File

@@ -64,7 +64,11 @@ public class JmsSendingMessageHandler extends AbstractJmsTemplateBasedAdapter im
throw new IllegalArgumentException("message must not be null");
}
final Message<?> messageToSend = MessageHistory.write(message, this);
this.getJmsTemplate().convertAndSend(messageToSend, new MessagePostProcessor() {
Object objectToSend = messageToSend;
if (this.shouldExtractPayload()){
objectToSend = messageToSend.getPayload();
}
this.getJmsTemplate().convertAndSend(objectToSend, new MessagePostProcessor() {
public javax.jms.Message postProcessMessage(javax.jms.Message jmsMessage)
throws JMSException {
getHeaderMapper().fromHeaders(messageToSend.getHeaders(), jmsMessage);

View File

@@ -16,15 +16,11 @@
package org.springframework.integration.jms;
import static org.junit.Assert.assertEquals;
import javax.jms.InvalidDestinationException;
import javax.jms.JMSException;
import javax.jms.Session;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
@@ -33,7 +29,6 @@ import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.SimpleMessageConverter;
/**
* @author Mark Fisher
@@ -56,28 +51,6 @@ public class ChannelPublishingJmsMessageListenerTests {
listener.onMessage(jmsMessage, session);
}
@Test
public void defaultHeaderMappingMessageConverter() {
ChannelPublishingJmsMessageListener listener = new ChannelPublishingJmsMessageListener();
listener.afterPropertiesSet();
Object converter = new DirectFieldAccessor(listener).getPropertyValue("messageConverter");
assertEquals(DefaultMessageConverter.class, converter.getClass());
Object wrappedConverter = new DirectFieldAccessor(converter).getPropertyValue("converter");
assertEquals(SimpleMessageConverter.class, wrappedConverter.getClass());
}
@Test
public void customMessageConverterDecoratedForHeaderMapping() {
ChannelPublishingJmsMessageListener listener = new ChannelPublishingJmsMessageListener();
MessageConverter originalConverter = new TestMessageConverter();
listener.setMessageConverter(originalConverter);
listener.afterPropertiesSet();
Object converter = new DirectFieldAccessor(listener).getPropertyValue("messageConverter");
assertEquals(DefaultMessageConverter.class, converter.getClass());
Object wrappedConverter = new DirectFieldAccessor(converter).getPropertyValue("converter");
assertEquals(originalConverter, wrappedConverter);
}
private void startBackgroundReplier(final PollableChannel channel) {
new SimpleAsyncTaskExecutor().execute(new Runnable() {
public void run() {
@@ -88,7 +61,6 @@ public class ChannelPublishingJmsMessageListenerTests {
});
}
private static class TestMessageConverter implements MessageConverter {
public Object fromMessage(javax.jms.Message message) throws JMSException, MessageConversionException {