d-m*_*man 176 java cron spring cronexpression spring-scheduled
我试图让我的代码按照固定的时间表执行,基于Spring cron表达式.我希望每天1:01:am执行代码.我尝试了下面的表达式,但这对我来说并没有起作用.这里的语法有什么问题?
@Scheduled(cron = "0 1 1 ? * *")
public void resetCache() {
// ...
}
Run Code Online (Sandbox Code Playgroud)
gip*_*ani 519
试试:
@Scheduled(cron = "0 1 1 * * ?")
Run Code Online (Sandbox Code Playgroud)
您可以在下面找到spring论坛的示例模式:
* "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 8,10 * * *" = 8 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
Run Code Online (Sandbox Code Playgroud)
Cron表达式由六个字段表示:
second, minute, hour, day of month, month, day(s) of week
Run Code Online (Sandbox Code Playgroud)
(*) 意味着匹配任何
*/X 意思是"每X"
?("无特定值") - 当您需要在允许该字符的两个字段之一中指定某些内容时非常有用,而在另一个字段中则不需要.例如,如果我希望我的触发器在该月的某一天(例如,第10天)触发,但我不关心一周中的哪一天,我会在当天放置"10" - 月份字段和"?" 在星期几的字段中.
PS:为了使其工作,请记住在您的应用程序上下文中启用它:https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html#scheduling-注释支持
Bah*_*mir 82
对于我的调度程序,我每天早上6点用它来开火,我的cron符号是:
0 0 6 * * *
Run Code Online (Sandbox Code Playgroud)
如果你想要1:01:am然后将其设置为
0 1 1 * * *
Run Code Online (Sandbox Code Playgroud)
调度程序的完整代码
@Scheduled(cron="0 1 1 * * *")
public void doScheduledWork() {
//complete scheduled work
}
Run Code Online (Sandbox Code Playgroud)
**非常重要
为了确保调度程序的触发时间正确性,您必须设置像这样的区域值(我在伊斯坦布尔):
@Scheduled(cron="0 1 1 * * *", zone="Europe/Istanbul")
public void doScheduledWork() {
//complete scheduled work
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到完整的时区值.
注意:我的Spring框架版本是:4.0.7.RELEASE
Mal*_*ana 12
gipinani的回答中缺少一些东西
@Scheduled(cron = "0 1 1,13 * * ?", zone = "CST")
Run Code Online (Sandbox Code Playgroud)
这将在1.01和13.01执行.当您需要每天多次运行没有模式的作业时,可以使用它.
当您在远程服务器中进行部署时,zone属性非常有用.这是在春季4推出的.
小智 6
我注意到的一件事是:Spring CronTrigger不是cron.您最终可能会在有效的cron表达式中使用7个参数(您可以在cronmaker.com上验证),然后不接受它.大多数情况下,您只需删除最后一个参数,一切正常.
| 归档时间: |
|
| 查看次数: |
295557 次 |
| 最近记录: |