AMQP-219 RabbitTemplate Conversion With Message
Previously, convertAndSend(), convertSendAndReceive() methods "converted" an Object that was already a Message to a Message with a null body. Now, if the object is already a Message, no conversion is performed.
This commit is contained in:
committed by
Oleg Zhurakousky
parent
6b8dd72b98
commit
7f19f4abdb
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user