In Spring AMQP, the Queue and Exchange classes in org.springframework.amqp.core are Java descriptions of the two core RabbitMQ building blocks. An Exchange receives messages from producers and routes them, and a Queue stores the routed messages until a consumer processes them. Declaring them as Spring beans makes the application create the matching broker objects automatically.
A producer never writes to a queue directly. It publishes to an exchange with a routing key, and bindings decide which queues get a copy.
Queue classExchange interface and its typesRabbitMQ message flow has three parts:
In Spring AMQP these map to three types: Queue, Exchange (with implementations such as DirectExchange) and Binding. The default exchange, whose name is the empty string, is a special case: it routes a message to the queue whose name equals the routing key, which is why simple examples seem to publish straight to a queue.
Queue classA Queue holds messages in order and delivers them to consumers, normally first in, first out. The constructor takes a name and flags:
| Property | Meaning |
|---|---|
durable | The queue definition survives a broker restart |
exclusive | Used by one connection only and deleted when it closes |
autoDelete | Deleted when the last consumer disconnects |
QueueBuilder is more readable than the four-argument constructor and lets you set arguments such as message TTL, dead letter exchange and queue type. For replicated, fault-tolerant queues, RabbitMQ recommends quorum queues (QueueBuilder.durable("orders").quorum().build()); classic mirrored queues were removed in RabbitMQ 4.0.
Exchange interface and its typesExchange is the interface that DirectExchange, TopicExchange, FanoutExchange and HeadersExchange implement. The type decides the routing rule.
| Exchange type | Routes by | Typical use |
|---|---|---|
| Direct | Exact routing key match | Task queues, per-severity logs |
| Topic | Wildcard pattern (* one word, # zero or more) | orders.eu.created style event routing |
| Fanout | Ignores the key, copies to all bound queues | Broadcast, cache invalidation |
| Headers | Message header values | Routing on several attributes |
ExchangeBuilder creates them fluently:
Add spring-boot-starter-amqp. Beans of type Queue, Exchange and Binding are declared on the broker by RabbitAdmin when the first connection opens, and Spring Boot configures RabbitAdmin for you.
Every message published to orders.events with a key like orders.eu.created now lands in orders.billing.
The producer names an exchange and a routing key, and never mentions the queue. The listener subscribes to the queue by name. This decoupling is the main reason to use an exchange: you can add another queue and binding, for example for analytics, without changing the producer.
Not at the protocol level. Messages go to an exchange. Sending with an empty exchange name uses the default exchange, which delivers to the queue named by the routing key.
The message is dropped unless you enable publisher returns with the mandatory flag or configure an alternate exchange.
For important data, yes: use a durable queue and persistent messages. Spring AMQP messages are persistent by default.
RabbitAdmin declares the beans when the application connects, so an existing queue with different arguments causes a declaration error.
Queue describes where messages wait, Exchange and its subclasses describe how messages are routed, and Binding links the two. Declare them as beans, let RabbitAdmin create them, publish to exchanges with routing keys, and consume from queues with @RabbitListener.