Spring Boot, a popular Java-based framework, simplifies the development of microservices and standalone applications. RabbitMQ, an open-source message-broker software, facilitates asynchronous communication between these services. In this guide, we'll explore how to send messages using Spring Boot and RabbitMQ, demonstrating the power of this integration.

Before we dive into the examples, ensure you have both Spring Boot and RabbitMQ set up in your project. If you're using Spring Boot 2.x, you can add the RabbitMQ dependency to your `pom.xml` file:

```xml
Configuring RabbitMQ in Spring Boot
After adding the dependency, configure RabbitMQ in your `application.properties` or `application.yml` file. Here's an example of `application.properties` configuration:

``` spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ```
Now, let's create a simple Spring Boot application that sends messages to RabbitMQ.
Creating a Simple Producer

A producer is an application that sends messages to RabbitMQ. Create a new class `RabbitMQProducer` with the following content:
```java import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class RabbitMQProducer { @Autowired private RabbitTemplate rabbitTemplate; @GetMapping("/send") public String sendMessage(@RequestParam("message") String message) { rabbitTemplate.convertAndSend("myQueue", message); return "Message sent: " + message; } } ```
In this example, we're using the `RabbitTemplate` to send a message to the queue named `myQueue`. The `sendMessage` method accepts a message parameter and sends it to RabbitMQ.
To test the producer, run your Spring Boot application and send a GET request to `http://localhost:8080/send?message=Hello%20RabbitMQ`. You should see the message "Message sent: Hello RabbitMQ" in the response.

Sending Messages with Properties
Sometimes, you might want to send additional metadata along with your message. RabbitMQ supports this through message properties. Update the `RabbitMQProducer` class as follows:
```java import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class RabbitMQProducer { @Autowired private RabbitTemplate rabbitTemplate; @GetMapping("/sendWithProperties") public String sendMessageWithProperties(@RequestParam("message") String message, @RequestParam("priority") int priority) { MessageProperties messageProperties = new MessageProperties(); messageProperties.setPriority(priority); Message msg = new Message(message.getBytes(), messageProperties); rabbitTemplate.send("myQueue", msg); return "Message sent with priority " + priority + ": " + message; } } ```
Now, when you send a GET request to `http://localhost:8080/sendWithProperties?message=Hello%20RabbitMQ&priority=5`, the message will be sent with a priority of 5.

Sending Messages to Different Queues
In some cases, you might want to send messages to different queues based on their content. To achieve this, you can use RabbitMQ's direct exchange pattern.



















First, create a new queue and exchange in your `application.properties` file:
``` spring.rabbitmq.queue.myOtherQueue=myOtherQueue spring.rabbitmq.exchange.myExchange=myExchange spring.rabbitmq.binding.myBinding.destination=myOtherQueue spring.rabbitmq.binding.myBinding.exchange=myExchange spring.rabbitmq.binding.myBinding.routingKey=myRoutingKey ```
Then, update the `RabbitMQProducer` class to send messages to the new queue:
```java import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class RabbitMQProducer { @Autowired private RabbitTemplate rabbitTemplate; // ... other send methods ... @GetMapping("/sendToOtherQueue") public String sendMessageToOtherQueue(@RequestParam("message") String message) { rabbitTemplate.convertAndSend("myExchange", "myRoutingKey", message); return "Message sent to myOtherQueue: " + message; } } ```
Now, when you send a GET request to `http://localhost:8080/sendToOtherQueue?message=Hello%20Other%20Queue`, the message will be sent to the `myOtherQueue` queue.
That's it! You've now learned how to send messages using Spring Boot and RabbitMQ, covering various scenarios like sending messages with properties and sending messages to different queues. Happy coding!