RabbitMQ + Spring Boot Example

RabbitMQ + Spring Boot Example

Play this article


Message Queues are used to handle the communication between different systems in a distributed environment. RabbitMQ is one of the most widely used message brokers. It is an open-source message broker that implements the Advanced Message Queuing Protocol (AMQP). Spring Boot is a popular Java framework that provides an easy way to build production-grade Spring-based applications. In this tutorial, we will look at how to use RabbitMQ with Spring Boot.


  • Java 11 or higher

  • RabbitMQ installed and running locally


Setting up RabbitMQ

To install RabbitMQ, follow the instructions for your operating system on the official website rabbitmq.com/download.html.

After installing RabbitMQ, start the RabbitMQ server by running the following command in your terminal or command prompt:


By default, RabbitMQ runs on port 5672. You can access the RabbitMQ management console at localhost:15672 with the default credentials guest/guest.

Setting up a Spring Boot project

The easiest way to create a new Spring Boot project is by using the Spring Initializr. Go to start.spring.io and configure your project as follows:

  • Project type: Maven or Gradle

  • Language: Java

  • Spring Boot: Latest version

  • Group: com.example

  • Artifact: rabbitmq-example

  • Dependencies: Web, AMQP

Click the Generate button to download the project. Import the project into your IDE of choice.

Adding RabbitMQ Dependency

To integrate RabbitMQ with Spring Boot, we need to add the following dependencies to our pom.xml or build.gradle file:

<!-- pom.xml -->
// build.gradle
implementation 'org.springframework.boot:spring-boot-starter-amqp'

Configuring RabbitMQ Connection

In your Spring Boot application, you need to configure the connection details for RabbitMQ. This can be done using properties in the application.properties file.


Creating a Queue

Next, we need to create a queue to which messages can be sent. This can be done using the Queue class in the org.springframework.amqp.core package.

public class RabbitMQConfig {

    public Queue queue() {
        return new Queue("myQueue", false);

Sending and Receiving Messages

In this section, we will implement an example application that sends and receives messages using RabbitMQ.

Configuring RabbitMQ Connection

First, we need to configure the connection to the RabbitMQ instance. Create a new configuration class called RabbitMQConfig in the com.example.rabbitmqexample.config package and add the following code:

public class RabbitMQConfig {

    private String host;

    private int port;

    private String username;

    private String password;

    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        return connectionFactory;

    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);

    public Queue exampleQueue() {
        return new Queue("example.queue", true);


This configuration class defines the RabbitMQ connection parameters and creates a RabbitTemplate bean, which provides us with a simple API for sending messages to RabbitMQ. It also defines a Queue bean, which represents the message queue that we will use in our example application.

Sending a Message

To send a message to the queue, we can use the RabbitTemplate class in the org.springframework.amqp.rabbit.core package.

public class RabbitMQController {

    private RabbitTemplate rabbitTemplate;

    private Queue queue;

    public String sendMessage() {
        String message = "Hello RabbitMQ!";
        rabbitTemplate.convertAndSend(queue.getName(), message);
        return "Message sent to the RabbitMQ Successfully";

Receiving a Message

To receive messages from the queue, we can use the @RabbitListener annotation on a method in a Spring Bean.

public class RabbitMQListener {

    @RabbitListener(queues = "myQueue")
    public void processMessage(String message) {
        System.out.println("Received Message: " + message);


Start the RabbitMQ server and run the Spring Boot application. To send a message, navigate to http://localhost:8080/sendMessage. The message will be added to the queue, and the RabbitMQListener will receive and print the message.


In this tutorial, we looked at how to use RabbitMQ with Spring Boot. We covered the basics of sending and receiving messages, and how to configure the connection details for RabbitMQ in a Spring Boot application. With this knowledge, you should be able to start using RabbitMQ in your own Spring Boot applications.


I hope this helps, you!!

More such articles:





If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!