0. 想要写代码
中心思想:消息转换
4.1.8. Message Converters
The AmqpTemplate
also defines several methods for sending and receiving messages that delegate to a MessageConverter
.
The MessageConverter provides a single method for each direction: one for converting to a Message and another for converting from a Message.
Notice that, when converting to a Message, you can also provide properties in addition to the object. The object parameter typically(一般地) corresponds to(对应) the Message body.
public interface MessageConverter {
Message toMessage(Object object, MessageProperties messageProperties)
throws MessageConversionException;
Object fromMessage(Message message) throws MessageConversionException;
}
The relevant Message-sending methods on the AmqpTemplate are simpler than the methods we discussed previously, because they do not require the Message instance.
Instead, the MessageConverter is responsible for “creating” each Message by converting the provided object to the byte array for the Message body and then adding any provided MessageProperties.
void convertAndSend(Object message) throws AmqpException;
void convertAndSend(String routingKey, Object message) throws AmqpException;
void convertAndSend(String exchange, String routingKey, Object message)
throws AmqpException;
void convertAndSend(Object message, MessagePostProcessor messagePostProcessor)
throws AmqpException;
void convertAndSend(String routingKey, Object message,
MessagePostProcessor messagePostProcessor) throws AmqpException;
void convertAndSend(String exchange, String routingKey, Object message,
MessagePostProcessor messagePostProcessor) throws AmqpException;
On the receiving side, there are only two methods: one that accepts the queue name and one that relies on the template’s “queue” property having been set.
Object receiveAndConvert() throws AmqpException;
Object receiveAndConvert(String queueName) throws AmqpException;
SimpleMessageConverter
The default implementation of the MessageConverter strategy is called SimpleMessageConverter
.
This is the converter that is used by an instance of RabbitTemplate.
It handles text-based content, serialized Java objects, and byte arrays.
Converting From a Message
If the content type of the input Message begins with “text” (for example, “text/plain”),
it also checks for the content-encoding
property to determine the charset to be used when converting the Message body byte array to a Java String.
If no content-encoding
property had been set on the input Message, it uses the UTF-8 charset by default.
If you need to override that default setting, you can configure an instance of SimpleMessageConverter
, set its defaultCharset
property, and inject that into a RabbitTemplate
instance.
If the content-type
property value of the input Message is set to “application/x-java-serialized-object”,
the SimpleMessageConverter
tries to deserialize (rehydrate) the byte array into a Java object. While that might be useful for simple prototyping,
we do not recommend relying on Java serialization, since it leads to tight(紧的) coupling(耦合) between the producer and the consumer.
Of course, it also rules out usage of non-Java systems on either side. With AMQP being a wire-level protocol, it would be unfortunate(不幸的) to lose much of that advantage with such restrictions.
In the next two sections, we explore some alternatives for passing rich domain object content without relying on Java serialization.
For all other content-types
, the SimpleMessageConverter
returns the Message body content directly as a byte array.
Converting To a Message
When converting to a Message from an arbitrary(任意) Java Object, the SimpleMessageConverter likewise(同样地) deals with byte arrays, strings, and serializable instances.
It converts each of these to bytes (in the case of byte arrays, there is nothing to convert),
and it sets the content-type
property accordingly.
If the Object to be converted does not match one of those types, the Message body is null.