Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Spring AMQP with Spring Boot Tutorial

Introduction

Spring AMQP provides support for AMQP-based messaging solutions. It simplifies the integration of messaging systems into Spring applications. In this tutorial, we will explore how to set up Spring AMQP with Spring Boot to send and receive messages using RabbitMQ.

Prerequisites

Before we start, ensure you have the following installed:

  • Java Development Kit (JDK) 8 or higher
  • Apache Maven
  • RabbitMQ server
  • Basic knowledge of Spring Boot

Setting Up the Project

We will use Spring Initializr to set up our Spring Boot project. Follow these steps:

  1. Visit Spring Initializr.
  2. Select the following options:
    • Project: Maven Project
    • Language: Java
    • Spring Boot: 2.x.x
  3. Add the following dependencies:
    • Spring Web
    • Spring AMQP
  4. Click on the "Generate" button to download the project.

Unzip the downloaded project and open it in your favorite IDE.

Configuring RabbitMQ

Ensure your RabbitMQ server is up and running. You can start it using Docker with the following command:

docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management

This command will run RabbitMQ with the management plugin enabled, accessible at http://localhost:15672 (default username: guest, password: guest).

Creating a Message Producer

We will create a simple message producer that sends messages to a RabbitMQ queue. Create a new class named MessageProducer.java in the src/main/java/com/example/demo directory:

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String message) {
        rabbitTemplate.convertAndSend("myQueue", message);
    }
}
                

Here, we inject RabbitTemplate, which is used to send messages to a specified queue.

Creating a Message Consumer

Next, we will create a message consumer that receives messages from the RabbitMQ queue. Create a new class named MessageConsumer.java:

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

@Component
public class MessageConsumer {

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

The @RabbitListener annotation indicates that the method receiveMessage will be invoked whenever a message is received on the specified queue.

Configuring RabbitMQ in Application Properties

Open the src/main/resources/application.properties file and add the following configurations:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
                

These properties define the connection settings for RabbitMQ.

Testing the Application

We can now test our application. Create a simple REST controller to trigger message sending. Create a new class named MessageController.java:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {

    @Autowired
    private MessageProducer messageProducer;

    @PostMapping("/send")
    public void sendMessage(@RequestBody String message) {
        messageProducer.sendMessage(message);
    }
}
                

This controller has a POST endpoint /send that accepts a message and sends it to the RabbitMQ queue.

Run your Spring Boot application and send a message by executing the following command using curl:

curl -X POST -d "Hello, RabbitMQ!" http://localhost:8080/send

You should see the message printed in the console of your application.

Conclusion

In this tutorial, we covered the basics of integrating Spring AMQP with Spring Boot. We created a message producer and consumer, configured RabbitMQ, and tested message sending and receiving. This setup can be expanded for more complex messaging scenarios including message persistence, error handling, and more.

For further reading, consider exploring the official Spring AMQP documentation and RabbitMQ tutorials.