diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java index aecd106b..0e982875 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java @@ -355,7 +355,7 @@ public class RabbitTemplate extends RabbitAccessor implements RabbitOperations, } public void convertAndSend(String exchange, String routingKey, final Object object, CorrelationData corrationData) throws AmqpException { - send(exchange, routingKey, getRequiredMessageConverter().toMessage(object, new MessageProperties()), corrationData); + send(exchange, routingKey, convertMessageIfNecessary(object), corrationData); } public void convertAndSend(Object message, MessagePostProcessor messagePostProcessor) throws AmqpException { @@ -380,7 +380,7 @@ public class RabbitTemplate extends RabbitAccessor implements RabbitOperations, public void convertAndSend(String exchange, String routingKey, final Object message, final MessagePostProcessor messagePostProcessor, CorrelationData correlationData) throws AmqpException { - Message messageToSend = getRequiredMessageConverter().toMessage(message, new MessageProperties()); + Message messageToSend = convertMessageIfNecessary(message); messageToSend = messagePostProcessor.postProcessMessage(messageToSend); send(exchange, routingKey, messageToSend, correlationData); } @@ -464,8 +464,7 @@ public class RabbitTemplate extends RabbitAccessor implements RabbitOperations, public Object convertSendAndReceive(final String exchange, final String routingKey, final Object message, final MessagePostProcessor messagePostProcessor) throws AmqpException { - MessageProperties messageProperties = new MessageProperties(); - Message requestMessage = getRequiredMessageConverter().toMessage(message, messageProperties); + Message requestMessage = convertMessageIfNecessary(message); if (messagePostProcessor != null) { requestMessage = messagePostProcessor.postProcessMessage(requestMessage); } @@ -476,6 +475,13 @@ public class RabbitTemplate extends RabbitAccessor implements RabbitOperations, return this.getRequiredMessageConverter().fromMessage(replyMessage); } + protected Message convertMessageIfNecessary(final Object object) { + if (object instanceof Message) { + return (Message) object; + } + return getRequiredMessageConverter().toMessage(object, new MessageProperties()); + } + /** * Send a message and wait for a reply. * diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateTests.java new file mode 100644 index 00000000..aeb8d1ec --- /dev/null +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateTests.java @@ -0,0 +1,66 @@ +/* + * Copyright 2002-2012 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.rabbit.core; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +import org.junit.Test; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.core.MessageProperties; +import org.springframework.amqp.support.converter.SimpleMessageConverter; +import org.springframework.amqp.utils.SerializationUtils; + +/** + * @author Gary Russell + * @since 1.0.1 + * + */ +public class RabbitTemplateTests { + + @Test + public void testConvertbytes() { + RabbitTemplate template = new RabbitTemplate(); + byte[] payload = "Hello, world!".getBytes(); + Message message = template.convertMessageIfNecessary(payload); + assertSame(payload, message.getBody()); + } + + @Test + public void testConvertString() throws Exception { + RabbitTemplate template = new RabbitTemplate(); + String payload = "Hello, world!"; + Message message = template.convertMessageIfNecessary(payload); + assertEquals(payload, new String(message.getBody(), SimpleMessageConverter.DEFAULT_CHARSET)); + } + + @Test + public void testConvertSerializable() throws Exception { + RabbitTemplate template = new RabbitTemplate(); + Long payload = 43L; + Message message = template.convertMessageIfNecessary(payload); + assertEquals(payload, SerializationUtils.deserialize(message.getBody())); + } + + @Test + public void testConvertMessage() throws Exception { + RabbitTemplate template = new RabbitTemplate(); + Message input = new Message("Hello, world!".getBytes(), new MessageProperties()); + Message message = template.convertMessageIfNecessary(input); + assertSame(input, message); + } + +}