AMQP-141: create base class for converters

This commit is contained in:
Dave Syer
2011-03-31 14:30:19 +01:00
parent 05f72da069
commit 0a97bdfac8
6 changed files with 96 additions and 26 deletions

View File

@@ -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;
}

View File

@@ -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 {

View File

@@ -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);

View File

@@ -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 {

View File

@@ -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);
}

View File

@@ -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();