Spring AMQP / RabbitMQ : Manually pull message(s) from RabbitMQ queue

In the previous article (click here to view the article), consumer application listen to the queue using a listener. Therefore whenever the queue receive a message, the listener who is listening to the queue will get notified. So that the Consumer application will automatically get the message with the help of the listener.

In this article,  the Consumer application will be developed in a way to pull the messages from the queue manually. that means the message(s) will be manually pulled whenever the client application needs them. (I am emphasizing it again that we are not going to use any queue Listener here)

The source code of the Producer application of the previous article (click here to go to the previous article) can be used as it is. But there will be a minor modification for the Consumer application.  The Consumer.java class should be changed as follows to manually retrieve/pull the messages from the defined queue.


package com.springbootdev.samples.rabbitmq.consumer.listener;
import com.springbootdev.samples.rabbitmq.consumer.model.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public class Consumer
private static final Logger LOGGER = LoggerFactory.getLogger(Consumer.class);
private static final String QUEUE_NAME = "orders_queue";
private RabbitTemplate rabbitTemplate;
public void pullMessage()
LOGGER.info("receiving the message ");
Object message = rabbitTemplate.receiveAndConvert(QUEUE_NAME);
if (message != null) {
Order order = (Order) message;
LOGGER.info(" received message [" + order.toString() + "] ");

view raw
hosted with ❤ by GitHub


You can see that the Consumer does not listen to the queue and receive messages automatically. It will use the receiveAndConvert() method of the RabbitTemplate to receive messages from the queue. We can set the queue name and messages will be pulled from that queue . The receiveAndConvert()  will use the message converter set in the RabbitTemplate to convert the received message into the desired POJO (Java Object) format. It will be retrieving the messages from the Order Queue based on FIFO principle.

The complete source code of this article can be found at GitHub.

You can download the source and just build and run the application.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s