如何有条件地启用或禁用 Spring 中的预定作业? [英] How to conditionally enable or disable scheduled jobs in Spring?
问题描述
我使用 @Scheduled
注释在 Spring 中使用 cron 样式模式定义计划作业.
I am defining scheduled jobs with cron style patterns in Spring, using the @Scheduled
annotation.
cron 模式存储在配置属性文件中.实际上有两个属性文件:一个是默认配置,另一个是依赖于环境(例如 dev、test、prod customer 1、prod customer 2 等)并覆盖一些默认值的配置文件配置.
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.
我在 spring 上下文中配置了一个属性占位符 bean,它允许我使用 ${}
样式占位符从我的属性文件中导入值.
I configured a property placeholder bean in my spring context which allows me to use ${}
style placeholders to import values from my properties files.
作业 bean 如下所示:
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
}
}
我的上下文 XML 的相关部分:
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>
我真的很喜欢这个.使用最少的 XML 非常简单和干净.
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.
因此,在我使用 Spring 管理它们之前,我手动创建了它们,并且在配置文件中有一个布尔参数和 cron 参数,用于指定是否必须启用作业:
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 * * ?
如何在 Spring 中使用此参数根据此配置参数有条件地创建或直接忽略 bean?
How can I use this parameter in Spring to conditionally create or just plainly ignore the bean, depending on this config parameter?
一个明显的解决方法是定义一个永远不会评估的 cron 模式,因此永远不会执行作业.但是还是会创建bean,配置会有点晦涩,所以我觉得一定有更好的解决方案.
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.
推荐答案
@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 中的预定作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!