gpt4 book ai didi

java - SpringBoot应用程序启动后启动@RabbitListener

转载 作者:行者123 更新时间:2023-12-01 16:36:34 26 4
gpt4 key购买 nike

我们在 Spring boot 应用程序中实现了rabbitMq。这就是我们定义监听器的方式。但是每当我们启动应用程序并且队列中有消息时,这些消息的消耗就会在应用程序完全启动之前开始。

@Component
@RabbitListener(queues = "hello")
public class RabbitMqReceiver {

我想仅在服务器启动后启动监听器。

最佳答案

您可以将监听器的 autoStartup 属性设置为 false,这样它就不会自动启动。然后,您可以在准备好后启动(并稍后停止)它。

使用RabbitListenerEndpointRegistry获取对监听器容器的引用。

@RabbitListener(id = "foo". ..., autoStartup = "false")

...

@Autowired
RabbitListenerEndpointRegistry registry;

...

registry.getListenerContainer("foo").start();

参见the reference manual .

Container Management Containers created for annotations are not registered with the application context. You can obtain a collection of all containers by invoking getListenerContainers() on the RabbitListenerEndpointRegistry bean. You can then iterate over this collection, for example, to stop or start all containers or invoke the Lifecycle methods on the registry itself, which will invoke the operations on each container.

You can also get a reference to an individual container by using its id, using getListenerContainer(String id) — for example, registry.getListenerContainer("multi") for the container created by the snippet above.

关于java - SpringBoot应用程序启动后启动@RabbitListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61936722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com