简体   繁体   中英

Store/Serialize List as Message Spring AMQP

I am new to Spring AMQP which uses RabbitMQ. I understand that this question addresses a broader issue. I want to store a list of objects into message which can be sent to consumers. Can anyone provide an easy solution to this problem?

I know Serialization can be a solution, but that will be an overkill for the simple application that I am using. The messages would be asynchronous in nature. Is there any other approach?

AMQP only supports one type of message - binary (bytes) - and this means you must serialize your object.

You do get to choose the type of serialization, though - for example JSON, XML or Java serialization.

You can use a MessageConverter , see the Spring RabbitMQ documentation: http://static.springsource.org/spring-amqp/docs/1.1.x/reference/htmlsingle/#d4e293

There is a JsonMessageConverter which is probably what you are looking for.

I recommend not to use standard Java serialization and the SimpleMessageConverter , because then your implementation will be bound to Java and that would defeat the whole idea of AMQPs protocol concept.

From the documentation:

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

Here is a code example taken from the documentation:

<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>

Once you have set this up, it should be possible to serialize/deserialize a list object into JSON and back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM