2012-05-25 36 views
0

我是使用RabbitMQ的Spring AMQP的新手。我明白這個問題涉及更廣泛的問題。我想將對象列表存儲到可發送給消費者的消息中。任何人都可以提供這個問題的簡單解決方案?存儲/序列化列表爲消息彈簧AMQP

我知道序列化可以是一個解決方案,但這對於我正在使用的簡單應用程序來說是一種矯枉過正。這些消息本質上是異步的。還有其他方法嗎?

回答

0

AMQP只支持一種類型的消息 - 二進制(字節) - 這意味着您必須序列化您的對象。

雖然您可以選擇序列化類型,例如JSON,XML或Java序列化。

0

可以使用MessageConverter,看到春RabbitMQ的文檔: http://static.springsource.org/spring-amqp/docs/1.1.x/reference/htmlsingle/#d4e293

有一個JsonMessageConverter這可能是你在找什麼。

我建議不要使用標準的Java序列化和SimpleMessageConverter,因爲那樣你的實現將被綁定到Java,並且這將打破AMQP協議概念的整個想法。

從文檔:

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並返回。