java - 如何在 Spring 将@Scheduled(cron) 与SpEL 一起使用?

标签 java spring annotations cron

我有一个我希望 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)

第二次编辑: 似乎 spring 正在尝试将以下字符串解析为实际 cron 表达式本身的 cron 表达式“#{scheduler['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));
    }

可能无法在注释中使用 spEL 外部化 cron 配置。

备选方案是使用 XML 或使用 cron 表达式。

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

关于java - 如何在 Spring 将@Scheduled(cron) 与SpEL 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10394716/

相关文章:

java - 在 Spring 获取 org.springframework.orm.ObjectOptimisticLockingFailureException

java - 'configure' 和 'configureGlobal' 方法有什么区别?

Django 注释和 values() : extra field in 'group by' causes unexpected results

spring - 在缓存中找不到但不缓存结果时,如何使@Cacheable 返回null?

java - 线程共享的 ReentrantLock 似乎不支持锁定

java - 需要将 String 数组传递给 Set

java - Sonar 想要关闭 Stream

java - 如何跨不同 JVM 保留对象标识

java - EJB 不是 "visible"给 EJB 管理器。不能使用 CDI 或 JNDI 来引用它

java - 无法在 junit 5 中使用 @ContextConfiguration 创建用于测试的 bean