使用属性文件中的 cron 表达式进行任务调度 [英] Task scheduling using cron expression from properties file
问题描述
我写了一个 cron 作业:
@Scheduled(cron="${process.virtual.account.start}")公共无效 ecomProcessVirAccOrderPaymentsScheduler() {LOGGER.info("开始--->" + this.getClass().getCanonicalName() + ".ecomProcessVirAccOrderPaymentsScheduler() 方法");schedulerJobHelper.ecomProcessVirAccOrderPaymentsScheduler();LOGGER.info("End --->" + this.getClass().getCanonicalName() + ".ecomProcessVirAccOrderPaymentsScheduler() 方法");}
我想从外部属性文件中获取与 @Scheduled
注释一起使用的 cron 属性.目前我正在从应用程序范围内的属性文件中获取它.我能够获取该值,但无法将它与 @Schedule
注释一起使用.
你使用的是哪个版本的 spring 框架?如果低于 3.0.1,这将不起作用.
Spring 3.0.0 中的错误报告,并已在 3.0 中修复.1.
因此,如果您使用的是 Spring 3.0.1 或更高版本,那么在 cron 表达式中必须执行以下操作
<属性名称=位置"><列表><value>classpath:ApplicationProps.properties</value></list></属性></bean>
之后在使用@Scheduled 方法时使用它,例如<块引用>
更新:如果您正在使用 spring boot 不需要做任何事情,下面的代码摘录应该可以工作.
@Scheduled(cron=${instructionSchedularTime}")公共无效负载(){}
注意:固定延迟和固定速率不能从占位符中获取属性值,因为它们需要长值.Cron 属性将参数作为字符串,因此您可以为此使用占位符.
I have written a cron job:
@Scheduled(cron="${process.virtual.account.start}")
public void ecomProcessVirAccOrderPaymentsScheduler() {
LOGGER.info("Start --->" + this.getClass().getCanonicalName() + ".ecomProcessVirAccOrderPaymentsScheduler() Method");
schedulerJobHelper.ecomProcessVirAccOrderPaymentsScheduler();
LOGGER.info("End --->" + this.getClass().getCanonicalName() + ".ecomProcessVirAccOrderPaymentsScheduler() Method");
}
I want to get the cron attribute used with @Scheduled
annotation to be populated from a external properties file. Currently I am fetching it from a property file inside the application scope.
I am able to fetch the value, but not able to use it with @Schedule
annotation.
Which version of spring framework are you using? This won't work if it is less than 3.0.1.
Bug Report here in Spring 3.0.0 and it has been fixed in 3.0.1.
So if you are using Spring 3.0.1 or greater then following things you have to do to use in cron expression
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:ApplicationProps.properties</value>
</list>
</property>
</bean>
After That Use it in using the @Scheduled method like
Update: In case if you are using spring boot no need to do anything, below code excerpt should work.
@Scheduled(cron="${instructionSchedularTime}")
public void load(){
}
Note: fixed delay and fixed-rate cann't take property value from placeholder because they take long value. Cron attribute take argument as String so you can use placeholder for that.
这篇关于使用属性文件中的 cron 表达式进行任务调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!