Home>Article>Java> Using Spring and AMQP to send and receive messages in java (Part 2)

Using Spring and AMQP to send and receive messages in java (Part 2)

怪我咯
怪我咯 Original
2017-06-25 10:02:27 2030browse

The previous article talked about using RabbitMQ to send messages, and this article will talk about receiving messages.
In traditional JMS, there are two ways to obtain information from the queue, the synchronous method using JmsTemplate and the asynchronous method using message-driven pojo. Spring AMQP also provides a similar way to obtain messages sent via AMQP.

Use RabbitTemplate to receive messages

The simplest method of receiving information provided by RabbitTemplate is the receive() method, through which you can obtain a Message object from the queue:

Message message = rabbit.receive("spittle.test.queue");

Or you can also obtain the default queue of messages through configuration. This is achieved by setting the queue attribute when configuring the template: