春天cron和正常cron? [英] Spring cron vs normal cron?

查看:401
本文介绍了春天cron和正常cron?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要一个myTask,我想要一个cron作业在旧的Java / Spring / Hibernate项目中工作,所以我决定使用spring调度器。 .doStuff在每月的第一个星期日12:00运行。



在我的application-context.xml中,我已经配置了我的任务调度器:

 < task:scheduled-tasks scheduler =MyTaskScheduler> 
< task:scheduled ref =myTaskmethod =doStuffcron =0 0 12 1/1 SUN#1 */> <! - 本月的每一个Sundy - >
< / task:scheduled-tasks>

< task:scheduler id =MyTaskSchedulerpool-size =10/>

问题cron表达式本身是: 0 0 12? 1/1 SUN#1 *



myTask 是一个bean, doStuff 在从单元测试运行时工作得很好。



当我构建和部署时,我从spring获得bootime异常:

  Caused by:java.lang.IllegalArgumentException:cron表达式必须包含6个字段(在0 0 12?1中找到7个,在SUN#1 *中找到7个)
at org.springframework.scheduling.support.CronSequenceGenerator.parse (CronSequenceGenerator.java:81)
at org.springframework.scheduling.support.CronTrigger。< init>(CronSequenceGenerator.java:81)
at org.springframework.scheduling.support.CronSequenceGenerator。 init>(CronTrigger.java:54)
在org.springframework.scheduling.config.ScheduledTaskRegistrar中的org.springframework.scheduling.support.CronTrigger< init>(CronTrigger.java:44)
。 afterPropertiesSet(ScheduledTaskRegistrar.java:129)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory。 initializeBean(AbstractAutowireCapableBeanFactory.java:1417)

由于我第一次使用cron表达式,我的第一个假设是我做错了,但我使用 cronmaker 双重检查,它给了我相同的结果。



所有文档说:cron表达式是一个由六个或七个子表达式(字段)组成的字符串。 1



尽管我试图敲掉第七个元素(年),因为它不在任何示例,并得到一个不同的错误消息:

  org.springframework.beans .factory.BeanCreationException:创建名为org.springframework.scheduling.config.ScheduledTaskRegistrar#0的bean时出错:调用init方法失败;嵌套异常为java.lang.NumberFormatException:对于输入字符串:0#1

...做org.springframework.scheduling支持不同风味的cron从一切吗? 特定于春季的文档 'cron表达式'。



如何让这个cron表达式在此上下文中正常工作?



目前我的解决方案是简化这个表达式,仅仅在每个星期天运行,并且将一些Java逻辑计算这个月的哪个星期天,看看是否有效 - 但是这种做法违背了配置方法的目的,似乎是一个反模式。

解决方案

Spring计划任务与cron表达式的格式不同。



它们的格式与UNIX cron表达式不同。



只有6个字段:第二,分钟,小时,日期,月,日(星期六)。
星号(*)表示匹配任何。
* / X表示每X(见示例)。
一周的数字天对我来说不起作用。此外,MON-FRI更容易阅读。
以下是一些示例表达式:



0 0 18 * * MON-FRI表示每个工作日下午6:00。



0 0 * / 1 * * *表示小时内每小时。



0 0 * / 8 * * *表示每小时的每8小时。



0 0 12 1 * *表示每月第一天下午12:00 。



在这里您可以找到一些额外的信息:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator。 html



也许你会发现spring文档很有用:
http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/ scheduling.html


I'm trying to get a cron job working within a legacy Java/Spring/Hibernate project, so I decided to use the spring scheduler.

I want myTask.doStuff to run at 12:00 on the first Sunday of every month.

In my application-context.xml I've configured my task scheduler like:

<task:scheduled-tasks scheduler="MyTaskScheduler">
    <task:scheduled ref="myTask" method="doStuff" cron="0 0 12 ? 1/1 SUN#1 *"/> <!-- Every first Sundy of the month -->
</task:scheduled-tasks>

<task:scheduler id="MyTaskScheduler" pool-size="10"/>

with the problem cron expression itself being the: 0 0 12 ? 1/1 SUN#1 *

and myTask is a bean, which has a method called doStuff that works perfectly when run from unit tests.

When I build and deploy I get a bootime exception from spring:

Caused by: java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 7 in 0 0 12 ? 1/1 SUN#1 *)
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:129)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)

Given that i'm using cron expressions for the first time, my first assumption was that I was doing something wrong, but I double checked using cronmaker and it gave me the same result.

All the documentations says: A cron expression is a string consisting of six or seven subexpressions (fields).1

despite this I tried knocking off the 7th element(year) since it's not in any of the examples, and got a different error message:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.config.ScheduledTaskRegistrar#0': Invocation of init method failed; nested exception is java.lang.NumberFormatException: For input string: "0#1"

... does org.springframework.scheduling support a different flavor of cron from everything else? the spring-specific documentation just says 'cron expressions'.

How can I get this cron expression to work as expected in this context? Any help at all would be appreciated.

At the moment my solution would be to simplify this expression to just run every Sunday, and prepend some Java logic to calculate which Sunday of the month it is, and see if that works - but that sort of defeats the purpose of the configuration approach and seems like an antipattern.

解决方案

Spring Scheduled tasks are not in the same format as cron expressions.

They don't follow the same format as UNIX cron expressions.

There are only 6 fields: second, minute, hour, day of month, month, day(s) of week. Asterisk (*) means match any. */X means "every X" (see examples). Numeric days of the week do not work for me. Besides, "MON-FRI" is much easier to read. Here are some example expressions:

"0 0 18 * * MON-FRI" means every weekday at 6:00 PM.

"0 0 */1 * * *" means every hour on the hour.

"0 0 */8 * * *" means every 8 hours on the hour.

"0 0 12 1 * *" means 12:00 PM on the first day of every month.

Here you can find some additional information: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html

Also you may find the spring documentation useful: http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/scheduling.html

这篇关于春天cron和正常cron?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆