如何在启动时以编程方式禁用Spring @JmsListener [英] How to disable Spring @JmsListener programmatically on startup

查看:84
本文介绍了如何在启动时以编程方式禁用Spring @JmsListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring应用程序,该应用程序带有以Spring的@JmsListener注释的方法.该应用程序部署在多个节点上.在某些特定的节点上,我需要禁用JMS侦听器,以便它不会将消息拉出队列.

I have a Spring application that has methods annotated with Spring's @JmsListener. The application is deployed on multiple nodes. On some specific nodes I need to disable the JMS listener so that it is not pulling messages off the queue.

在应用程序启动后,似乎有一种方法可以停止侦听器.但是,这似乎使打开窗口和在禁用代码运行之间(应用程序实例可能消耗消息的时间)之间的简短窗口保持打开状态.因此,有一种方法可以在应用程序启动期间禁用侦听器.

There appears to be a way to stop the listener after the application has started up. But this appears to leave open the brief window between startup and when the disable code runs where the application instance may consume messages. So instead is there a way to disable the listener during application startup.

推荐答案

您需要自定义由注释创建的侦听器容器定义.

You need to customize the listener container definitions created by the annotation.

添加侦听器容器工厂@Bean(请参见文档),并将autoStartup属性设置为false.

Add a listener container factory @Bean (see the documentation) and set the autoStartup property to false.

setAutoStartup(false);

然后可以通过通过JmsListenerEndpointRegistry bean获取引用来根据需要启动每个容器.容器本身不是bean,而是来自其javadoc ...

You can then start each container as needed by getting a reference via the JmsListenerEndpointRegistry bean. The containers are not beans themselves - from its javadoc...

...
* <p>Contrary to {@link MessageListenerContainer}s created manually, listener
* containers managed by registry are not beans in the application context and
* are not candidates for autowiring. Use {@link #getListenerContainers()} if
* you need to access this registry's listener containers for management purposes.
* If you need to access to a specific message listener container, use
* {@link #getListenerContainer(String)} with the id of the endpoint.
...

这篇关于如何在启动时以编程方式禁用Spring @JmsListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆