From 0a97bdfac8e0785f3bd0298f56f6f7dced72e16e Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 31 Mar 2011 14:30:19 +0100 Subject: [PATCH] AMQP-141: create base class for converters --- .../converter/AbstractMessageConverter.java | 71 +++++++++++++++++++ .../converter/JsonMessageConverter.java | 6 +- .../MarshallingMessageConverter.java | 5 +- .../converter/SerializerMessageConverter.java | 4 +- .../converter/SimpleMessageConverter.java | 19 +---- .../SimpleMessageConverterTests.java | 17 +++++ 6 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/AbstractMessageConverter.java diff --git a/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/AbstractMessageConverter.java b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/AbstractMessageConverter.java new file mode 100644 index 00000000..0e5fa877 --- /dev/null +++ b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/AbstractMessageConverter.java @@ -0,0 +1,71 @@ +/* + * 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.amqp.support.converter; + +import java.util.UUID; + +import org.springframework.amqp.core.Message; +import org.springframework.amqp.core.MessageProperties; + +/** + * Convenient base class for {@link MessageConverter} implementations. + * @author Dave Syer + * + */ +public abstract class AbstractMessageConverter implements MessageConverter { + + private boolean createMessageIds = false; + + /** + * Flag to indicate that new messages should have unique identifiers added to their properties before sending. + * Default false. + * @param createMessageIds the flag value to set + */ + public void setCreateMessageIds(boolean createMessageIds) { + this.createMessageIds = createMessageIds; + } + + /** + * Flag to indicate that new messages should have unique identifiers added to their properties before sending. + * @return the flag value + */ + protected boolean isCreateMessageIds() { + return createMessageIds; + } + + public final Message toMessage(Object object, MessageProperties messageProperties) + throws MessageConversionException { + if (messageProperties==null) { + messageProperties = new MessageProperties(); + } + Message message = createMessage(object, messageProperties); + messageProperties = message.getMessageProperties(); + if (this.createMessageIds && messageProperties.getMessageId()==null) { + messageProperties.setMessageId(UUID.randomUUID().toString()); + } + return message; + } + + /** + * Crate a message from the payload object and message properties provided. The message id will be added to the + * properties if necessary later. + * + * @param object the payload + * @param messageProperties the message properties (headers) + * @return a message + */ + abstract protected Message createMessage(Object object, MessageProperties messageProperties); + + abstract public Object fromMessage(Message message) throws MessageConversionException; + +} \ No newline at end of file diff --git a/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/JsonMessageConverter.java b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/JsonMessageConverter.java index a566c485..5db93bc6 100644 --- a/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/JsonMessageConverter.java +++ b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/JsonMessageConverter.java @@ -33,9 +33,7 @@ import org.springframework.amqp.core.MessageProperties; * @author James Carr * @author Dave Syer */ -public class JsonMessageConverter implements MessageConverter { - - // TODO create composite MessageConverter with key/value pairs of content-type/converter. introduce base class +public class JsonMessageConverter extends AbstractMessageConverter { private static Log log = LogFactory.getLog(JsonMessageConverter.class); @@ -125,7 +123,7 @@ public class JsonMessageConverter implements MessageConverter { return jsonObjectMapper.readValue(contentAsString, targetClass); } - public Message toMessage(Object objectToConvert, MessageProperties messageProperties) + protected Message createMessage(Object objectToConvert, MessageProperties messageProperties) throws MessageConversionException { byte[] bytes = null; try { diff --git a/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java index 578f6820..1ff31c1f 100644 --- a/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java +++ b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java @@ -17,7 +17,6 @@ package org.springframework.amqp.support.converter; import java.io.ByteArrayInputStream; - import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -42,7 +41,7 @@ import org.springframework.util.Assert; * @see org.springframework.amqp.rabbit.core.RabbitTemplate#convertAndSend * @see org.springframework.amqp.rabbit.core.RabbitTemplate#receiveAndConvert */ -public class MarshallingMessageConverter implements MessageConverter, InitializingBean { +public class MarshallingMessageConverter extends AbstractMessageConverter implements InitializingBean { private Marshaller marshaller; @@ -118,7 +117,7 @@ public class MarshallingMessageConverter implements MessageConverter, Initializi /** * Marshals the given object to a {@link Message}. */ - public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { + protected Message createMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(bos); diff --git a/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/SerializerMessageConverter.java b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/SerializerMessageConverter.java index 195308cb..46664ad3 100644 --- a/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/SerializerMessageConverter.java +++ b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/SerializerMessageConverter.java @@ -33,7 +33,7 @@ import org.springframework.core.serializer.Serializer; * * @author Dave Syer */ -public class SerializerMessageConverter implements MessageConverter { +public class SerializerMessageConverter extends AbstractMessageConverter { public static final String DEFAULT_CHARSET = "UTF-8"; @@ -117,7 +117,7 @@ public class SerializerMessageConverter implements MessageConverter { /** * Creates an AMQP Message from the provided Object. */ - public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { + protected Message createMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { byte[] bytes = null; if (object instanceof String) { try { diff --git a/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/SimpleMessageConverter.java b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/SimpleMessageConverter.java index 7a9ad851..6e3ee239 100644 --- a/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/SimpleMessageConverter.java +++ b/spring-amqp-core/src/main/java/org/springframework/amqp/support/converter/SimpleMessageConverter.java @@ -22,7 +22,6 @@ import java.io.InputStream; import java.io.ObjectInputStream; import java.io.Serializable; import java.io.UnsupportedEncodingException; -import java.util.UUID; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageProperties; @@ -40,7 +39,7 @@ import org.springframework.util.ClassUtils; * @author Mark Fisher * @author Oleg Zhurakousky */ -public class SimpleMessageConverter implements MessageConverter, BeanClassLoaderAware { +public class SimpleMessageConverter extends AbstractMessageConverter implements BeanClassLoaderAware { public static final String DEFAULT_CHARSET = "UTF-8"; @@ -50,21 +49,10 @@ public class SimpleMessageConverter implements MessageConverter, BeanClassLoader private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); - private boolean createMessageIds = false; - public void setBeanClassLoader(ClassLoader beanClassLoader) { this.beanClassLoader = beanClassLoader; } - /** - * Flag to indicate that new messages should have unique identifiers added to their properties before sending. - * Default false. - * @param createMessageIds the flag value to set - */ - public void setCreateMessageIds(boolean createMessageIds) { - this.createMessageIds = createMessageIds; - } - /** * Set the codebase URL to download classes from if not found locally. Can consists of multiple URLs, separated by * spaces. @@ -129,7 +117,7 @@ public class SimpleMessageConverter implements MessageConverter, BeanClassLoader /** * Creates an AMQP Message from the provided Object. */ - public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { + protected Message createMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { byte[] bytes = null; if (object instanceof byte[]) { bytes = (byte[]) object; @@ -158,9 +146,6 @@ public class SimpleMessageConverter implements MessageConverter, BeanClassLoader if (bytes != null) { messageProperties.setContentLength(bytes.length); } - if (this.createMessageIds && messageProperties.getMessageId()==null) { - messageProperties.setMessageId(UUID.randomUUID().toString()); - } return new Message(bytes, messageProperties); } diff --git a/spring-amqp-core/src/test/java/org/springframework/amqp/support/converter/SimpleMessageConverterTests.java b/spring-amqp-core/src/test/java/org/springframework/amqp/support/converter/SimpleMessageConverterTests.java index 1b2c4511..598e71c3 100644 --- a/spring-amqp-core/src/test/java/org/springframework/amqp/support/converter/SimpleMessageConverterTests.java +++ b/spring-amqp-core/src/test/java/org/springframework/amqp/support/converter/SimpleMessageConverterTests.java @@ -17,6 +17,8 @@ package org.springframework.amqp.support.converter; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -43,6 +45,21 @@ public class SimpleMessageConverterTests { assertEquals("test", new String((byte[]) result, "UTF-8")); } + @Test + public void noMessageIdByDefault() throws Exception { + SimpleMessageConverter converter = new SimpleMessageConverter(); + Message message = converter.toMessage("foo", null); + assertNull(message.getMessageProperties().getMessageId()); + } + + @Test + public void optionalMessageId() throws Exception { + SimpleMessageConverter converter = new SimpleMessageConverter(); + converter.setCreateMessageIds(true); + Message message = converter.toMessage("foo", null); + assertNotNull(message.getMessageProperties().getMessageId()); + } + @Test public void messageToString() { SimpleMessageConverter converter = new SimpleMessageConverter();