Spring Boot RabbitMQ Send Message Example

Layla Jul 06, 2026

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.

Spring Boot RabbitMQ Tutorial | Send Messages to Queue with Annotations
Spring Boot RabbitMQ Tutorial | Send Messages to Queue with Annotations

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:

the easter bunny sayss with an image of a bunny in a basket on it
the easter bunny sayss with an image of a bunny in a basket on it

```xml org.springframework.boot spring-boot-starter-amqp ```

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:

an image of a rabbit with the words when it's time to leave
an image of a rabbit with the words when it's time to leave

``` 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

Stop ‘just following up’—to get responses ASAP, use these 'powerful messages,' says communication expert
Stop ‘just following up’—to get responses ASAP, use these 'powerful messages,' says communication expert

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.

19 Reasons I Didn't Reply to Your Online Dating Message - New York Cliché
19 Reasons I Didn't Reply to Your Online Dating Message - New York Cliché

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.

a small brown rabbit sitting on top of a white floor
a small brown rabbit sitting on top of a white floor

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.

some bunny loves you on a white background
some bunny loves you on a white background
Need
Need
123 Best Email Opening Lines & Sentences for Every Occasion
123 Best Email Opening Lines & Sentences for Every Occasion
How to Reply to LOL Text From Guys?
How to Reply to LOL Text From Guys?
r u a freak like me? 😛😛
r u a freak like me? 😛😛
Rabbit Template – Small
Rabbit Template – Small
How to Safelist Important Contacts in Gmail
How to Safelist Important Contacts in Gmail
Rabbit Quotes & Captions
Rabbit Quotes & Captions
there is a snowman with carrots on his nose and two rabbits in front of him
there is a snowman with carrots on his nose and two rabbits in front of him
the words it's a bunny thing written in black ink on a white background
the words it's a bunny thing written in black ink on a white background
\
\
a rabbit sitting on top of a blanket under a text that reads, some - bunny loves you
a rabbit sitting on top of a blanket under a text that reads, some - bunny loves you
Hop into Laughter with These Hare-larious Bunny Rabbit Puns! 🐰
Hop into Laughter with These Hare-larious Bunny Rabbit Puns! 🐰
the email marketing strategy to boost reader reponse
the email marketing strategy to boost reader reponse
Easter Bunny Letter Free Printable Templates (10 Cute Designs) | The Pink Craft Box
Easter Bunny Letter Free Printable Templates (10 Cute Designs) | The Pink Craft Box
Disposable Temporary Email to Send
Disposable Temporary Email to Send
15 Text Messages That Create Clightness Without Chasing
15 Text Messages That Create Clightness Without Chasing
How to Set Up Account Recovery Contacts on Mac, iPhone (A Guide)
How to Set Up Account Recovery Contacts on Mac, iPhone (A Guide)
Text Message Delivered But Not Received (Reasons) - Android Nature
Text Message Delivered But Not Received (Reasons) - Android Nature

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!