簡體   English   中英

如何在春天使用@Scheduled(cron)和SpEL?

[英]How to use @Scheduled(cron) with SpEL in spring?

我有一個方法,我想要春天安排 - 為此我使用@Scheduled注釋 - 更確切地說,我使用的是cron表達式。 我的cron表達式位於名為scheduler.properties的屬性文件中。 當我用它作為占位符@Scheduled(cron="${cron}") - 一切都很好; 但我想使用SpEL( @Scheduled(cron="#{scheduler['cron']}") ),它不起作用 - 拋出以下異常: java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 1 in #{scheduler['cron']})

我在這做錯了什么?

編輯:這是我的屬性文件中的cron表達式: cron=0 0/1 * * * ?

這是我得到的堆棧跟蹤: java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 1 in #{scheduler['cron']}) at org.springframework.scheduling.support.CronSequenceGenerator.parse(CronSequenceGenerator.java:233) at org.springframework.scheduling.support.CronSequenceGenerator.<init>(CronSequenceGenerator.java:81) at org.springframework.scheduling.support.CronTrigger.<init>(CronTrigger.java:54) at org.springframework.scheduling.support.CronTrigger.<init>(CronTrigger.java:44) at org.springframework.scheduling.config.ScheduledTaskRegistrar.afterPropertiesSet(ScheduledTaskRegistrar.java:188) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:209) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:1) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324) at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:929) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 1 in #{scheduler['cron']}) at org.springframework.scheduling.support.CronSequenceGenerator.parse(CronSequenceGenerator.java:233) at org.springframework.scheduling.support.CronSequenceGenerator.<init>(CronSequenceGenerator.java:81) at org.springframework.scheduling.support.CronTrigger.<init>(CronTrigger.java:54) at org.springframework.scheduling.support.CronTrigger.<init>(CronTrigger.java:44) at org.springframework.scheduling.config.ScheduledTaskRegistrar.afterPropertiesSet(ScheduledTaskRegistrar.java:188) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:209) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:1) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324) at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:929) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722)

第二個編輯:看起來Spring試圖解析下面的字符串,因為cron experssion“#{scheduler ['cron']}”暗示了實際的cron表達式本身。

根據錯誤消息,屬性文件中的cron表達式的值不正確。

它不符合預期的語法。

該值應包含六個字段,看起來像這樣。

* 10 * * * *

這是拋出此異常的代碼

/**
 * Parse the given pattern expression.
 */
private void parse(String expression) throws IllegalArgumentException {
    String[] fields = StringUtils.tokenizeToStringArray(expression, " ");
    if (fields.length != 6) {
        throw new IllegalArgumentException(String.format(""
                + "cron expression must consist of 6 fields (found %d in %s)", fields.length, expression));
    }

可能無法在Annotation中使用spEL外部化cron配置。

替代方案是使用XML或使用cron表達式。

http://forum.springsource.org/showthread.php?91203-Scheduled-and-externalization-of-configuration-for-fixedDelay-and-fixedRate-problem

始終在屬性文件中指定如下:注意頻率之間的空格。

每天早上9點運行刷新工作

job.cron.rate = 0 0 9 * * *

示例模式:

* "0 0 * * * *" = the top of every hour of every day.
* "*/10 * * * * *" = every ten seconds.
* "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
* "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.
* "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
* "0 0 0 25 12 ?" = every Christmas Day at midnight

在代碼中使用它:

@Scheduled(cron = "${job.cron.rate}")    
public void perform() throws InterruptedException {
}

我有一個類似的問題,並通過使用context:property-placeholder讀取屬性文件來解決它

<util:properties id="applicationProps" location="/WEB-INF/classes/properties/application.properties" /> **<context:property-placeholder properties-ref="applicationProps" />**

希望它可以幫助別人!!

有用。 我花了幾天時間搞清楚......但這確實有效。

  1. 您應該像為JAVA_HOME等那樣設置環境變量。
  2. 關閉IDE。

export cron_scheduler_expression="0 19 21 * * *"

然后重新啟動IDE,Eclipse或NetBeans,無論您使用什么。

@Scheduled(cron = "${cron_scheduler_expression}")
public void runSchedulerTask(){

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM