0

我是使用 RabbitMQ 的 Spring AMQP 的新手。我知道这个问题解决了一个更广泛的问题。我想将对象列表存储到可以发送给消费者的消息中。谁能提供一个简单的解决方案来解决这个问题?

我知道序列化可以成为一种解决方案,但对于我正在使用的简单应用程序来说,这将是一种过度杀伤力。消息本质上是异步的。还有其他方法吗?

4

2 回答 2

0

AMQP 只支持一种类型的消息——二进制(字节)——这意味着你必须序列化你的对象。

不过,您可以选择序列化的类型——例如 JSON、XML 或 Java 序列化。

于 2012-07-29T21:24:00.200 回答
0

您可以使用MessageConverter,请参阅 Spring RabbitMQ 文档: http ://static.springsource.org/spring-amqp/docs/1.1.x/reference/htmlsingle/#d4e293

有一个JsonMessageConverter可能是您正在寻找的。

我建议不要使用标准的 Java 序列化和 . SimpleMessageConverter,因为那样你的实现将绑定到 Java,这会破坏 AMQPs 协议概念的整个想法。

从文档中:

With AMQP being a wire-level protocol, it would be unfortunate to lose
much of that advantage with such restrictions.

这是取自文档的代码示例:

<bean class="org.springframework.amqp.rabbit.core.RabbitTemplate">
    <property name="connectionFactory" ref="rabbitConnectionFactory"/>
    <property name="messageConverter">
        <bean class="org.springframework.amqp.support.converter.JsonMessageConverter"/>
    </property>
</bean>

设置完成后,应该可以将列表对象序列化/反序列化为 JSON 并返回。

于 2013-05-19T16:01:37.310 回答