Spring AMQP 随笔 2 Converting

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值