Search Tutorials

Spring Boot + RabbitMQ Consume Message Simple Example | JavaInUse

Spring Boot + RabbitMQ Consume Message Simple Example

In a previous post we integrated RabbitMQ and Spring Boot to send a message to RabbitMQ. In this post we consuming the message using Spring Boot and RabbitMQ.
In a previous post we had seen how to get RabbitMQ up and running.
Spring Boot RabbitMQ Consume Message Tutorial

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

Lets Begin-

The project will be as follows-
Spring Boot RabbitMQ consume Eclipse Setup
Define the pom.xml as follows- Add the spring-boot-starter-amqp dependency.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""



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




Define the domain class Employee as follows-
package com.javainuse.model;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id", scope = Employee.class)
public class Employee {

	private String empName;
	private String empId;

	public String getEmpName() {
		return empName;

	public void setEmpName(String empName) {
		this.empName = empName;

	public String getEmpId() {
		return empId;

	public void setEmpId(String empId) {
		this.empId = empId;

	public String toString() {
		return "Employee [empName=" + empName + ", empId=" + empId + "]";

Define the RabbitMQConsumer class which consumes the message from RabbitMQ using RabbitListener.The RabbitMQ Listener listens to RabbitMQ Queue for any incoming messages. For the basic configuration we specify the the Queue/Topic Name (the name of the queue/topic where the message should be consumed)

package com.javainuse.service;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.javainuse.model.Employee;

public class RabbitMQConsumer {

	@RabbitListener(queues = "${javainuse.rabbitmq.queue}")
	public void recievedMessage(Employee employee) {
		System.out.println("Recieved Message From RabbitMQ: " + employee);
Next define th following properties in
Finally Define the Spring Boot Class with @SpringBootApplication annotation
package com.javainuse;

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

public class SpringBootHelloWorldApplication {

	public static void main(String[] args) {
				new Object[] { SpringBootHelloWorldApplication.class }, args);

We are done with the required Java code. Now lets start RabbitMQ. As we had explained in detail in the Getting started with RabbitMQ perform the steps to start the RabbitMQ.
Next start the Spring Boot RabbitMQProducer application we developed in previous tutorial. Application by running it as a Java Application. Hit the url as follows- http://localhost:8080/javainuse-rabbitmq/producer?empName=emp1&empId=emp001
Spring Boot RabbitMQ Example
This will trigger the message to be sent to the javainuse queue.
Next go to the RabbitMQ console-http://localhost:15672/
Spring Boot RabbitMQ Consoler
We can see in the Queues section, a queue name java gets created and it has one message.
Spring Boot RabbitMQ Queue
Next start the Spring Boot RabbitMQ Consumer application we just developed. It will consume the message from the queue named javainuse.queue.
Spring Boot RabbitMQ

Download Source Code

Download it -
Spring Boot + RabbitMQ Consume Hello World Example