English 中文(简体)
如何在春天使用与SpEL相配的(cron)?
原标题:How to use @Scheduled(cron) with SpEL in spring?

I have a method that I want spring to schedule - for that matter I m using the @Scheduled annotation - and to be more exact, I m using a cron expression. My cron expression is in a property file that is called scheduler.properties. When I m using it as a placeholder @Scheduled(cron="${cron}") - everything works great; but I want to use SpEL ( @Scheduled(cron="#{scheduler[ cron ]}") ) , and it does t work - throws the following exception:java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 1 in #{scheduler[ cron ]})

我在这里做了什么错误呢?

EDIT: Here is my cron expression from the properties file: cron=0 0/1 * * * ?

Here is the stack trace that I get: 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)

SECOND EDIT: It seems that spring is trying to parse the following string as the cron experssion "#{scheduler[ cron ]}" insraed of the actual cron expression itself.

最佳回答

根据错误信息,贵方财产档案中的陈词价值是不正确的。

它不符合预期的辛迪加。

The value should contain six fields and look something like this.

* 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));
    }

也许不可能在说明中采用间谍法来外包创作组合。

这些替代办法是使用XML或使用 cr语。

问题回答

Always specify like this in property file: Notice the space in between frequency.

run refresh job every day a 9am

页: 1

例:

* "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 {
}

我有一个类似的问题,并通过阅读财产档案来解决这个问题:财产占有人

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

希望能帮助人们!

它行之有效。 我花了几天时间,这确实奏效。

  1. You should set environment variable like you do for JAVA_HOME etc.
  2. Close your IDE.

export cron_scheduler_expression="0 19 21 * * *"

Then restart your IDE, Eclipse or NetBeans whatever you are using.

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

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签