Synopsis
When declaring RabbitMQ queues by registering them as Spring Beans, those queues won't be initialized before the first use.
Note: Maybe this issue is more suitable for Spring AMQP project.
Sample Project
I've created a sample project to demonstrate this issue: https://github.com/alimate/spring-rabbit-auto-declare
Issue
I've defined a RabbitMQ queue:
@Configuration
class Queues {
@Bean
fun firstQ() = Queue("first.queue", true)
}
Since the autoStartup flag of the messaging container is true by default, I was expecting the queue to be declared at the application startup, but it wasn't until the first time I publish something to it!
Possible Resolution
In order to fix this issue, I had to write a custom initializer:
@Component
class RabbitInitializer(private val admin: AmqpAdmin) {
/**
* logger.
*/
private val log = logger<RabbitInitializer>()
/**
* When the application context is ready, initialize the RabbitMQ.
*/
@EventListener(ApplicationReadyEvent::class)
fun initializeQueues() {
log.debug("About to auto-declare all declarable rabbit components")
admin.initialize()
}
}
That's fine but the AbstractMessageListenerContainer message container has autoStartup and autoDeclare flags which I was expect to automatically declare those components.
Comment From: wilkinsona
I believe this is the expected behaviour with Spring AMQP. The RabbitAdmin instance is, due to its auto startup being enabled, registering a listener with the connection factory. When the connection factory creates the connection the call back is invoked and the queues are defined. If you want to discuss this behaviour with the Spring AMQP team you can find them on Gitter.
Comment From: alimate
@wilkinsona Thanks for your clarification.
Comment From: jbp198669
how do i get this off my comptuer
Comment From: jbp198669
at least i figured out what was going on :)
Comment From: metaruslan
I got the same question, but here is the javadoc from Declarable (Queue, Exchange and Binding implement it) interface:
Classes implementing this interface can be auto-declared with the broker during context initialization by an AmqpAdmin. Registration can be limited to specific AmqpAdmins. Since: 1.2 Author: Gary Russell
So it says "during context initialization", is it a javadoc "bug"?