如何在Spring中有条件地启用或禁用计划作业?

编程入门 行业动态 更新时间:2024-10-10 00:22:49
本文介绍了如何在Spring中有条件地启用或禁用计划作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 @Scheduled 注释在Spring中使用cron样式的patten定义计划作业。

cron模式存储在配置属性文件中。实际上有两个属性文件:一个默认配置和一个与环境相关的配置文件配置(例如dev,test,prod客户1,prod客户2等),并覆盖某些默认值。

我在我的spring上下文中配置了一个属性占位符bean,它允许我使用 $ {} 样式占位符从属性文件中导入值。 / p>

作业bean类似如下:

@Component public class ImagesPurgeJob implements Job { private Logger logger = Logger.getLogger(this.getClass()); @Override @Transactional(readOnly = true) @Scheduled(cron =$ {jobs.mediafiles.imagesPurgeJob.schedule}) public void execute (){ // Do something //可以使用DAO或其他自动装配的bean } }

我的上下文XML的相关部分:

通过注释启用计划任务配置 - > < task:annotation-driven /> <! - 加载配置文件并允许'$ {}'样式占位符 - > < bean class =org.springframework.context.support.PropertySourcesPlaceholderConfigurer> < property name =locations> < list> < value> classpath:config / default-config.properties< / value> < value> classpath:config / environment-config.properties< / value> < / list> < / property> < property name =ignoreUnresolvablePlaceholdersvalue =true/> < property name =ignoreResourceNotFoundvalue =false/> < / bean>

我真的很喜欢这个。

但是,我还有一个要求:在某些情况下,其中一些工作可以完全禁用。

所以,在我使用Spring管理它们之前,我们手动创建它们,并且有一个布尔参数以及配置文件中的cron参数,用于指定是否必须启用该作业: / p>

jobs.mediafiles.imagesPurgeJob.enable = true或false jobs.mediafiles.imagesPurgeJob.schedule = 0 0 0 / 12 * *?

如何在Spring中使用此参数有条件地创建或只是简单地忽略bean config参数?

一个显而易见的解决方法是定义一个永远不会评估的cron模式,因此作业不会被执行。但是bean仍然会被创建,配置会有点模糊,所以我觉得必须有一个更好的解决方案。

任何想法?

解决方案

@Component public class ImagesPurgeJob implements Job { 私人记录器logger = Logger.getLogger(this.getClass()); @Value($ {jobs.mediafiles.imagesPurgeJob.enable}) private boolean imagesPurgeJobEnable; @Override @Transactional(readOnly = true) @Scheduled(cron =$ {jobs.mediafiles.imagesPurgeJob.schedule}) public void execute (){ //做某事 //可以使用DAO或其他自动装配的bean if(imagesPurgeJobEnable){ here ... } }

p>

I am defining scheduled jobs with cron style pattenrs in Spring, using the @Scheduled annotation.

The cron pattern is stored in a config properties file. Actually there are two properties files : one default config, and one profile config that is environment dependent (e.g. dev, test, prod customer 1, prod customer 2 etc.) and overrides some of the default values.

I configured a property placeholder bean in my spring context which allows me to use ${} style placeholders to import values from my properties files.

The job beans looks like this :

@Component public class ImagesPurgeJob implements Job { private Logger logger = Logger.getLogger(this.getClass()); @Override @Transactional(readOnly=true) @Scheduled(cron = "${jobs.mediafiles.imagesPurgeJob.schedule}") public void execute() { //Do something //can use DAO or other autowired beans here } }

Relevant parts of my context XML :

<!-- Enable configuration of scheduled tasks via annotations --> <task:annotation-driven/> <!-- Load configuration files and allow '${}' style placeholders --> <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config/default-config.properties</value> <value>classpath:config/environment-config.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="ignoreResourceNotFound" value="false"/> </bean>

I really like this. It's quite simple and clean with minimal XML.

However I have one more requirement : some of these jobs can be totally disabled in some cases.

So, before I used Spring to manage them I created them manually and there is a boolean parameter along with the cron parameter in the config files, to specify if the job has to be enabled or not :

jobs.mediafiles.imagesPurgeJob.enable=true or false jobs.mediafiles.imagesPurgeJob.schedule=0 0 0/12 * * ?

How can I use this parameter in Spring to conditionally create or just plainly ignore the bean, depending on this config parameter ?

One obvious workaround would be to define a cron pattern that would never evaluate, so the job is never executed. But the bean would still be created and the config would be a bit obscure, so I feel there must be a better solution.

Any idea ?

解决方案

@Component public class ImagesPurgeJob implements Job { private Logger logger = Logger.getLogger(this.getClass()); @Value("${jobs.mediafiles.imagesPurgeJob.enable}") private boolean imagesPurgeJobEnable; @Override @Transactional(readOnly=true) @Scheduled(cron = "${jobs.mediafiles.imagesPurgeJob.schedule}") public void execute() { //Do something //can use DAO or other autowired beans here if(imagesPurgeJobEnable){ Do your conditional job here... } }

}

更多推荐

如何在Spring中有条件地启用或禁用计划作业?

本文发布于:2023-11-23 00:43:18,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   中有   条件   计划   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!