Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

In Spring AMQP with Spring Boot, how do you declare a durable queue "invoices", a topic exchange "billing" and a binding for routing key "invoice.#", and when are they actually created on the broker?

We declare three beans: a Queue built with QueueBuilder.durable("invoices").build(), a TopicExchange named billing, and a Binding from BindingBuilder.bind(queue).to(exchange).with("invoice.#"). Spring Boot auto-configures a RabbitAdmin, which declares all such beans on the broker when the first connection is opened, not when the beans are created. Declarations are idempotent when the arguments match, but redeclaring an existing queue with different arguments fails with PRECONDITION_FAILED.

Why this question is asked

Declaring topology in code is how most Spring AMQP applications start, and it hides a subtle detail: the beans are only descriptions, and the broker learns about them later, at a moment many developers cannot name. That timing explains surprises such as queues that appear only after the first message is sent, and startup failures after a queue's arguments change. Interviewers ask this to check both the API and the lifecycle behind it. In this answer, we will recap what a declaration is, write the three beans, follow RabbitAdmin as it declares them, see what idempotent declaration means and when it fails, show the annotation-based alternative, and discuss who should own topology in a larger organisation.

1A Quick Recap: What Declaring Means

In AMQP, the protocol RabbitMQ speaks, a client declares a queue or an exchange by sending a command such as queue.declare with a name and a set of properties. If the object does not exist, the broker creates it. If it exists with the same properties, nothing happens and the command succeeds. So a declaration is a statement of what we expect to exist, safe to repeat.

A durable queue survives a broker restart, because its definition is stored on disk. Whether its messages survive too depends on them being published as persistent, which Spring AMQP does by default. A binding connects the exchange to the queue with a pattern, here invoice.#, which on a topic exchange matches invoice followed by any number of words.

2Declaring the Three Beans

Spring AMQP gives us classes for each object and builders that make the arguments readable.

Java
@Configuration
class BillingTopology {
@Bean
Queue invoicesQueue() {
return QueueBuilder.durable("invoices")
.quorum() // replicated queue type, x-queue-type=quorum
.deadLetterExchange("billing.dlx")
.build();
}
@Bean
TopicExchange billingExchange() {
return new TopicExchange("billing"); // durable, not auto-delete by default
}
@Bean
Binding invoicesBinding(Queue invoicesQueue, TopicExchange billingExchange) {
return BindingBuilder.bind(invoicesQueue).to(billingExchange).with("invoice.#");
}
}

The quorum and dead-letter lines are optional extras that show how arguments are added; the question only needs QueueBuilder.durable("invoices").build(). With these beans, a message published to billing with routing key invoice.paid.eu lands in the invoices queue, while order.created does not. Notice that creating these objects does not contact the broker at all. They are plain Java objects in the application context.

3When RabbitAdmin Declares Them

Spring Boot auto-configures a RabbitAdmin, the component that turns these beans into AMQP declarations. RabbitAdmin registers itself as a listener on the connection factory. Whenever a connection is opened, the first time and again after every reconnect, it collects all Queue, Exchange and Binding beans in the context and declares them on the broker.

So the real moment depends on when the first connection opens. In an application with a @RabbitListener, the listener container starts at startup and opens a connection, so the topology appears during startup. In an application that only publishes, nothing connects until the first rabbitTemplate.convertAndSend, so the queue appears only then. The reconnect behaviour is valuable: if a broker node is replaced and the queue is lost, it is declared again automatically.

Step throughDeclaration happens on connection
ApplicationRabbitAdminBroker
context starts: Queue, Exchange, Binding beans created
listener container opens first connection
connection created event
exchange.declare billing (topic, durable)
queue.declare invoices (durable, quorum)
queue.bind invoices to billing with invoice.#
declare-ok

7 messages in order. Play them, or step through them one at a time.

Nothing reaches the broker when the beans are created; it happens when a connection opens.

4Idempotent Declarations and PRECONDITION_FAILED

Declaring the same queue again with the same properties succeeds silently, which is why every instance of a service can declare its topology at startup. But a queue's properties cannot be changed by redeclaring it. If the invoices queue already exists as a classic queue and our new code declares it as a quorum queue, the broker refuses with a channel error.

Java
// Existing on the broker: invoices, classic queue
// New code declares: invoices, x-queue-type=quorum
//
// -> channel error; protocol method: #method<channel.close>(reply-code=406,
// reply-text=PRECONDITION_FAILED - inequivalent arg 'x-queue-type' for queue 'invoices'
// in vhost '/': received 'quorum' but current is 'classic', ...)

Reply code 406 is PRECONDITION_FAILED. To change such an argument we either create a queue with a new name and migrate consumers, or delete and recreate the queue during a planned window after draining it. Settings that may change over time, such as TTL, length limits and dead-letter exchanges, are better applied with policies on the broker, which can be updated without redeclaring the queue.

5Declaring From the Listener, and Who Owns Topology

For small services, the listener annotation can declare everything at once. The @QueueBinding attribute describes the queue, the exchange and the key, and RabbitAdmin declares them the same way.

Java
@RabbitListener(bindings = @QueueBinding(
value = @org.springframework.amqp.rabbit.annotation.Queue(value = "invoices", durable = "true"),
exchange = @Exchange(value = "billing", type = ExchangeTypes.TOPIC),
key = "invoice.#"))
void onInvoice(Invoice invoice) { ... }

In many organisations, though, topology is managed as infrastructure: definitions in Git applied by Terraform's RabbitMQ provider, the Kubernetes messaging topology operator, or broker definition files. Then applications receive only the permissions to read and write, not to configure, and their declaration beans are switched off or kept for local development. This avoids two services declaring the same queue with different arguments and gives the platform team one place to review queue types, limits and dead-lettering.

6Follow-up questions

What happens if one declaration fails at startup?

RabbitAdmin logs the failure, and by default the exception propagates, so the listener container keeps retrying its startup and the problem is visible in the logs. RabbitAdmin has an ignoreDeclarationExceptions option that logs and continues with the remaining declarations, which can be useful when topology is managed elsewhere, but it can also hide a real mismatch, so check your version's default and be deliberate about it.

Is a durable queue enough to keep messages across a restart?

No. Durability keeps the queue definition. The messages also need to be persistent, which is Spring AMQP's default delivery mode, and for protection against a node failure the queue should be a replicated quorum queue.

7Conclusion

In this answer, we declared a durable invoices queue, a billing topic exchange and an invoice.# binding as Spring beans with QueueBuilder, TopicExchange and BindingBuilder. We saw that the beans are only descriptions: Boot's auto-configured RabbitAdmin declares them when a connection opens, at startup if a listener connects, otherwise on first use, and again after reconnects. Declarations are idempotent when arguments match, and a changed argument fails with PRECONDITION_FAILED, which is why changeable settings belong in policies. @QueueBinding offers a compact alternative, and many organisations manage topology as infrastructure code instead.

What to say in the interviewAbout 90 seconds aloud

Open with

I declare three beans. A Queue, using QueueBuilder.durable with the name invoices and build. A TopicExchange called billing.

Then, if they want more

And a Binding, using BindingBuilder.bind the queue to the exchange with the pattern invoice.hash, so it gets every routing key starting with invoice. These beans are just descriptions; creating them does not talk to the broker.

Spring Boot auto-configures a RabbitAdmin, which registers as a connection listener, and when a connection is opened it declares every queue, exchange and binding bean on the broker. It does that again after every reconnect, which is nice if a node loses the queue.

So if the app has a RabbitListener, the declarations happen during startup because the listener container connects; if it only publishes, they happen on the first send. Declarations are idempotent as long as the arguments are the same, so many instances can declare the same queue.

But if the queue already exists with different arguments, for example classic instead of quorum, the broker closes the channel with PRECONDITION_FAILED, inequivalent arg, and I would need a new queue or a planned migration. That is why I put changeable things like TTL and length limits into policies.

The listener can also declare its own topology with QueueBinding in the annotation, and in larger companies topology is often owned by infrastructure code rather than applications.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
RabbitMQ documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In Spring AMQP with Spring Boot, how do you declare a durable queue "invoices", a topic...": https://scrivio-interview-library.netlify.app/spring-boot/rabbitmq-and-jms/b2d216692f/