To create a custom RabbitMQ configuration in Spring Boot, add spring-boot-starter-amqp, set connection details in application.yml, and declare your own Queue, Exchange and Binding beans in a @Configuration class. Add a RabbitTemplate with a JSON message converter for publishing and a listener container factory for consuming. Spring Boot auto-configures the rest and backs off for any bean you define yourself.
The examples target Spring Boot 3.x and later on Java 17 or newer. Class names for the JSON converter can change between Spring AMQP major versions, so check the release notes of the version you use.
The starter brings in Spring AMQP and the RabbitMQ Java client. Maven:
Gradle:
Most connection settings need no code. Put them in application.yml and keep secrets in environment variables:
The guest account can only connect from localhost on a default broker, so use a dedicated user in any shared environment. For TLS, set spring.rabbitmq.ssl.enabled=true and the related ssl.* properties.
Spring AMQP's RabbitAdmin is auto-configured. When the application connects, it declares every Queue, Exchange and Binding bean on the broker, so you describe the topology as beans.
Choose the exchange type by routing need:
| Exchange type | Routing behaviour | Typical use |
|---|---|---|
DirectExchange | Exact match on routing key | Task queues, command routing |
TopicExchange | Pattern match such as orders.* | Event streams with categories |
FanoutExchange | Copies to all bound queues | Broadcast notifications |
HeadersExchange | Matches on message headers | Rare, attribute-based routing |
Declaring a queue again with different arguments than the existing one fails with a PRECONDITION_FAILED error. Delete the old queue or use a new name when you change arguments such as the dead-letter settings.
Spring Boot builds a CachingConnectionFactory from the properties. You only need your own bean for settings the properties do not cover. Defining one disables the auto-configured factory.
Publisher confirms tell you whether the broker accepted a message, which matters when losing a message is not acceptable. Prefer the properties spring.rabbitmq.publisher-confirm-type and spring.rabbitmq.publisher-returns when you do not need a custom bean at all.
By default messages are converted with Java serialization for Serializable objects. JSON is easier to debug and works across languages. Define a converter bean and Spring Boot applies it to the auto-configured RabbitTemplate and to listener containers.
Then send through RabbitTemplate:
For restricted deserialization, configure the converter's trusted packages rather than allowing every class.
Annotate a method with @RabbitListener. The container factory controls concurrency, acknowledgement and error handling.
If the properties are not enough, build a factory from Spring Boot's configurer so that your properties still apply:
Select it with @RabbitListener(queues = "...", containerFactory = "customListenerFactory"). Setting defaultRequeueRejected to false sends a failing message to the dead-letter queue instead of looping forever.
Use a real broker in tests. The Testcontainers RabbitMQContainer class together with @ServiceConnection (Spring Boot 3.1 and later) wires the connection properties automatically, so the same topology beans are exercised. Assert on the queue contents with rabbitTemplate.receiveAndConvert.
No. Spring Boot creates a CachingConnectionFactory from spring.rabbitmq.*. Define your own only for settings the properties cannot express.
RabbitAdmin declares them lazily when the first connection opens. If nothing connects, nothing is declared. Check that the beans are in a scanned @Configuration class and that the broker user has configure permission on the virtual host.
Enable listener retry, set a dead-letter exchange on the queue, and make rejected messages not requeue. Otherwise an exception requeues the message and the listener fails again immediately.
Use JSON. It is readable in the management UI, language neutral, and avoids deserialization risks of Java serialization.
Add the AMQP starter, keep connection settings in application.yml, and describe queues, exchanges and bindings as beans so RabbitAdmin declares them. Add a JSON message converter, a dead-letter queue and listener retry for resilience. Define a custom ConnectionFactory or listener container factory only when properties are not enough.