连接到队列或在 spring-rabbitmq 中不存在时创建

Connecting to queue or creating on non-existence in spring-rabbitmq

我正在使用 Spring 通过 spring-rabbitmq 启动。我的连接工厂配置在 application.properties 中,看起来不错。

我的目标是:期间开始检查是否存在特定名称的队列,如果不存在则创建这样的队列。我不确定如何处理它。我应该在配置 class 中创建什么 bean?根据我的阅读,它应该是 RabbitAdmin,但我不确定。你能帮助我吗?

一切都在Reference Manual:

中描述得很清楚

The AMQP specification describes how the protocol can be used to configure Queues, Exchanges and Bindings on the broker. These operations which are portable from the 0.8 specification and higher are present in the AmqpAdmin interface in the org.springframework.amqp.core package.

还有:

When the CachingConnectionFactory cache mode is CHANNEL (the default), the RabbitAdmin implementation does automatic lazy declaration of Queues, Exchanges and Bindings declared in the same ApplicationContext.

因此,您应该在您的应用程序上下文中声明 QueueExchangeBinding bean,并且 AmqpAdmin 将关注它们在目标 Broker 上的定义。

必须注意的是,根据AMQP协议,如果实体已经存在于Broker上,声明只是静默和幂等的。

因此,在您的情况下,您无需担心队列的存在,只需在应用程序上下文中将它们的声明作为 bean 提供即可。