RabbitMQ in Spring Boot

1. Prerequisites

  • Java 17+
  • Spring Boot 3.x
  • RabbitMQ (running locally or via Docker)

For RabbitMQ Docker:

docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
  • 5672 → RabbitMQ port
  • 15672 → Management console

2. Add Dependencies

In pom.xml (for Maven):

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

3. Configure RabbitMQ

In application.yml:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

4. Create Configuration

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String QUEUE = "demo-queue";
    public static final String EXCHANGE = "demo-exchange";
    public static final String ROUTING_KEY = "demo-routingKey";

    @Bean
    Queue queue() {
        return new Queue(QUEUE, false);
    }

    @Bean
    DirectExchange exchange() {
        return new DirectExchange(EXCHANGE);
    }

    @Bean
    Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
}

5. Create a Message Producer

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

@Service
public class MessageProducer {

    private final RabbitTemplate rabbitTemplate;
    private final RabbitMQConfig config;

    public MessageProducer(RabbitTemplate rabbitTemplate, RabbitMQConfig config) {
        this.rabbitTemplate = rabbitTemplate;
        this.config = config;
    }

    public void sendMessage(String message) {
        rabbitTemplate.convertAndSend(
                config.EXCHANGE, 
                config.ROUTING_KEY, 
                message
        );
        System.out.println("Sent: " + message);
    }
}

6. Create a Message Listener

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

@Service
public class MessageListener {

    @RabbitListener(queues = RabbitMQConfig.QUEUE)
    public void receiveMessage(String message) {
        System.out.println("Received: " + message);
    }
}

7. Send a Message via Controller (Optional)

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {

    private final MessageProducer producer;

    public MessageController(MessageProducer producer) {
        this.producer = producer;
    }

    @GetMapping("/send")
    public String send(@RequestParam String msg) {
        producer.sendMessage(msg);
        return "Message sent: " + msg;
    }
}

8. Run the Application

  • Start RabbitMQ (docker run ... if needed)
  • Run Spring Boot (mvn spring-boot:run)
  • Access http://localhost:8080/send?msg=HelloRabbit

Console Output:

Sent: HelloRabbit
Received: HelloRabbit

✅ This is a basic working example of Spring Boot + RabbitMQ.
You can extend this by using fanout exchanges, topic exchanges, or JSON messages.

Leave a Reply