Search Tutorials

Spring Boot + RabbitMQ Tutorial - Configure Listeners to consume messages | JavaInUse

Spring Boot + RabbitMQ Tutorial - Configure Listeners to consume messages | JavaInUse

In a previous tutorial we had implemented a Spring Boot + RabbitMQ example to send publish message to RabbitMQ Queue. In this tutorial we will be implementing a Spring Boot + RabbitMQ example to consume message from a RabbitMQ Queue. In next tutorial we will be exploring the various RabbitMQ Exchange types and implementing them using Spring Boot.
Spring Boot + RabbitMQ Application to consumer message

RabbitMQ - Table Of Contents

What is Messaging? Getting Started with RabbitMQ - Install and start RabbitMQ. Spring Boot + RabbitMQ Publish Message Example Spring Boot + RabbitMQ Tutorial - Configure Listeners to consume messages using MessageListenerContainer Spring Boot + RabbitMQ Consume Message Example using RabbitListener Spring Boot + RabbitMQ Tutorial - Implement Exchange Types Spring Boot + RabbitMQ Tutorial - Retry and Error Handling Example Spring Cloud Stream - RabbitMQ Publish Message Example Spring Cloud Stream - RabbitMQ Consume Message Example Pivotal Cloud Foundry Tutorial - Deploying Spring Boot + RabbitMQ Application to PCF


This tutorial is explained in the below Youtube Video.

Let's Begin

The Maven project will be as follows-
Spring Boot + RabbitMQ Listener Tutorial
The pom.xml will have the following dependencies-
<project xmlns="" xmlns:xsi=""

		<relativePath /> <!-- lookup parent from repository -->



We will first be creating a listener class which implements the AMQP MessageListener interface. This class is responsible for getting the message from the RabbitMQ queue.
package com.javainuse.service;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Service;

public class RabbitMQListner implements MessageListener {

	public void onMessage(Message message) {
		System.out.println("Consuming Message - " + new String(message.getBody()));

Next we will be creating the Spring Boot Configuration class for RabbitMQ. We will
  • Create Queue bean using which we create a RabbitMQ named javainuse.input-queue. This will be a non durable queue. Do not misunderstand a non durable queue to be a temporary queue. Durability property is related to how long a message will be stored in the queue. For example for in RabbitMQ restart messages in non durable queue will be lost while those in durable queue will not be lost.
  • Create MessageListenerContainer where we configure the RabbitMQConnections. Spring MessageListenerContainer is a replacement for a Message-Driven EJB. A connection is set up with the AMQ topic/queue, it gets messages from that topic/queue and feeds them to your MessageListener. We will be making use of the default connectionfactory. If we do not wish to use the default connectionfactory we can can create our own CachingConnectionFactory and use it.
package com.javainuse.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.javainuse.service.RabbitMQListner;

public class RabbitMQConfig {

	String queueName;

	String username;

	private String password;

	Queue queue() {
		return new Queue(queueName, false);
    //create MessageListenerContainer using default connection factory
	MessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory ) {
		SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
		simpleMessageListenerContainer.setMessageListener(new RabbitMQListner());
		return simpleMessageListenerContainer;

    //create custom connection factory
	ConnectionFactory connectionFactory() {
		CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory("localhost");
		return cachingConnectionFactory;
    //create MessageListenerContainer using custom connection factory
	MessageListenerContainer messageListenerContainer() {
		SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
		simpleMessageListenerContainer.setMessageListener(new RabbitMQListner());
		return simpleMessageListenerContainer;


In the define the following-
Finally create the bootstrap class which makes use of the SpringBootApplication Annotation
package com.javainuse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootRabbitMQApplication {

	public static void main(String[] args) {, args);
In a previous tutorial we have shown how to install RabbitMQ and get started. Once you have followed implemented this tutorial go to localhost:15672
RabbitMQ Console
Use the username and password as guest.If we now go to the queues section, currently there are no queues
RabbitMQ Console queues
Start the Spring Boot Application. If we now again go to the RabbitMQ Management Console Queues section we can see that a queue named javainuse.input-queue has been created.
RabbitMQ Console create queue
Now select the queue and publish a message.
RabbitMQ Console publish message
If we now go to the eclipse console we can see that our application has consumed the RabbitMQ message and printed its content.
Spring Boot RabbitMQ consumer message listener

Download Source Code

Download it -
Spring Boot + RabbitMQ Listeners to consume message