0
0
Spring Bootframework~5 mins

RabbitMQ integration basics in Spring Boot

Choose your learning style9 modes available
Introduction

RabbitMQ helps different parts of an app talk to each other by sending messages safely and quickly.

When you want to send data from one part of your app to another without waiting.
When you need to handle tasks in the background, like sending emails after a user signs up.
When your app has many parts that need to work together but should not block each other.
When you want to make your app more reliable by saving messages until they are processed.
Syntax
Spring Boot
1. Define a queue, exchange, and binding in a configuration class.
2. Use @RabbitListener to receive messages.
3. Use RabbitTemplate to send messages.

Queues hold messages until they are processed.

Exchanges route messages to queues based on rules.

Examples
This creates a simple queue named 'myQueue' that does not survive server restarts.
Spring Boot
@Bean
public Queue myQueue() {
    return new Queue("myQueue", false);
}
This method listens for messages on 'myQueue' and prints them when received.
Spring Boot
@RabbitListener(queues = "myQueue")
public void receiveMessage(String message) {
    System.out.println("Received: " + message);
}
This sends the message 'Hello RabbitMQ!' to the 'myQueue' queue.
Spring Boot
rabbitTemplate.convertAndSend("myQueue", "Hello RabbitMQ!");
Sample Program

This Spring Boot app creates a queue named 'myQueue'. When it starts, it sends a message 'Hello RabbitMQ!' to the queue. It also listens to the same queue and prints any received messages.

Spring Boot
package com.example.rabbitmqdemo;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableRabbit
public class RabbitmqDemoApplication implements CommandLineRunner {

    private final RabbitTemplate rabbitTemplate;

    public RabbitmqDemoApplication(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public static void main(String[] args) {
        SpringApplication.run(RabbitmqDemoApplication.class, args);
    }

    @Bean
    public Queue myQueue() {
        return new Queue("myQueue", false);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        rabbitTemplate.convertAndSend("myQueue", "Hello RabbitMQ!");
    }

    @RabbitListener(queues = "myQueue")
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}
OutputSuccess
Important Notes

Make sure RabbitMQ server is running before starting the app.

Queues can be durable to survive restarts by setting the second parameter to true.

Use @RabbitListener to handle messages asynchronously without blocking your app.

Summary

RabbitMQ helps apps send and receive messages safely and quickly.

Define queues and use RabbitTemplate to send messages.

Use @RabbitListener to receive and process messages asynchronously.