Spring cron vs 普通 cron? [英] Spring cron vs normal cron?

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

问题描述

我正在尝试在遗留 Java/Spring/Hibernate 项目中获得一份 cron 工作,因此我决定使用 spring 调度程序.

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

我希望 myTask.doStuff 在每个月的第一个星期日的 12:00 运行.

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

在我的 application-context.xml 中,我将任务调度程序配置为:

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"/>

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

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

myTask 是一个 bean,它有一个名为 doStuff 的方法,在从单元测试运行时可以完美地工作.

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

当我构建和部署时,我从 spring 收到一个启动时异常:

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)

鉴于我第一次使用 cron 表达式,我的第一个假设是我做错了什么,但我使用 cronmaker 仔细检查 它给了我同样的结果.

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.

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

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

尽管如此,我还是尝试取消第 7 个元素(年份),因为它不在任何示例中,并得到了不同的错误消息:

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"

... org.springframework.scheduling 是否支持与其他一切不同的 cron 风格?spring 特定文档 只是说cron 表达式".

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

我怎样才能让这个 cron 表达式在这种情况下按预期工作?任何帮助都将不胜感激.

目前我的解决方案是将这个表达式简化为只在每个星期日运行,并预先添加一些 Java 逻辑来计算它是当月的哪个星期日,然后看看这是否有效——但这有点违背了配置方法,似乎是一种反模式.

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 任务不同于与 cron 表达式的格式相同.

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

它们不遵循与 UNIX cron 表达式相同的格式.

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

只有 6 个字段:

  • 第二,
  • 分钟,
  • 小时,
  • 月份中的第几天,
  • 月,
  • 星期几.

星号 (*) 表示匹配任何.*/X 表示每个 X";(参见示例).

Asterisk (*) means match any. */X means "every X" (see examples).

一周中的数字天数对我不起作用.此外,MON-FRI"更容易阅读.以下是一些示例表达式:

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.

您还可以找到 spring有用的文档.

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

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