Introduction
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.
Prerequisites
Java 11 or higher
RabbitMQ installed and running locally
Setup
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:
rabbitmq-server
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 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
// 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.
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
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.
@Configuration
public class RabbitMQConfig {
@Bean
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:
@Configuration
public class RabbitMQConfig {
@Value("${spring.rabbitmq.host}")
private String host;
@Value("${spring.rabbitmq.port}")
private int port;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
return connectionFactory;
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
}
@Bean
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.
@RestController
public class RabbitMQController {
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private Queue queue;
@GetMapping("/sendMessage")
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.
@Component
public class RabbitMQListener {
@RabbitListener(queues = "myQueue")
public void processMessage(String message) {
System.out.println("Received Message: " + message);
}
}
Testing
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.
Conclusion
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.
References
I hope this helps, you!!
More such articles:
https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.