java RabbitAdmin声明
我正在使用Spring AMQP在不同的服务之间设置远程处理,如here所述。然而,当我在配置上设置reply-timeout
时,第一个请求总是失败,因为声明队列、交换和绑定所用的时间超过了超时时间:
The RabbitAdmin component can declare exchanges, queues and bindings on startup. It does this lazily, through a ConnectionListener, so if the broker is not present on startup it doesn't matter. The first time a Connection is used (e.g. by sending a message) the listener will fire and the admin features will be applied.
有没有什么方法可以在启动时立即声明,而不是在第一次发布事件时声明,以防止第一个请求总是失败
# 1 楼答案
如果使用注释声明队列:
然后在应用程序启动时使用以下命令手动调用
RabbitAdmin.initialize()
:# 2 楼答案
正如我们通过描述和
RabbitAdmin
中的代码所看到的,最后一个只是将ConnectionListener
填充到提供的ConnectionFactory
从
ConnectionFactory.createConnection()
调用ConnectionListener.onCreate
例如,你可以处理
ContextRefreshedEvent
,然后急切地执行voidconnectionFactory.createConnection()
从另一方面来看
RabbitAdmin
有initialize()
公共方法用于相同的目的更新
实际上
ListenerContainer
在它的start()
上也是这样。您必须在有侦听器的应用程序中声明队列、交换和绑定,并使其autoStartup = true
。老实说,listener应用程序负责真正的代理实体sending
应用程序应该只与exchangeName
和routingKey
打交道