且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在运行时将队列动态添加到Rabbit侦听器

更新时间:2023-02-01 21:22:37

  • 注入( @Autowired 或其他方式) RabbitListenerEndpointRegistry .

    获取对侦听器容器的引用(使用注释上的 id 属性为其提供已知的ID)( registry.getListenerContainer(id)).

    Get a reference to the listener container (use the id attribute on the annotation to give it a known id) (registry.getListenerContainer(id)).

    将容器投射到 AbstractMessageListenerContainer 上,然后调用 addQueues() addQueueNames().

    Cast the container to an AbstractMessageListenerContainer and call addQueues() or addQueueNames().

    请注意,动态添加队列时,使用 DirectMessageListenerContainer 效率更高;使用 SimpleMessageListenerContainer ,使用者将停止并重新启动.使用直接容器,每个队列都有自己的使用者.

    Note that is more efficient to use a DirectMessageListenerContainer when adding queues dynamically; with a SimpleMessageListenerContainer the consumer(s) are stopped and restarted. With the direct container, each queue gets its own consumer(s).

    请参见选择容器.