[英]Run a job with (Spring @Scheduled) on specific days
I have a customer check job.我有一个客户检查工作。 I could not find how to automatically set the job time to run on 10am only on Monday, Friday and Saturday.
我找不到如何自动将工作时间设置为仅在周一、周五和周六上午 10 点运行。 Is there a possible way to set it using Spring
@Scheduled
?有没有可能使用 Spring
@Scheduled
设置它的方法?
I found the solution like this:我找到了这样的解决方案:
@Scheduled(cron = "0 0 10 * * MON,FRI,SAT")
public void dailyScheduleJob() {
/*
your code here
*/
}
Additionally, if the requested days are sequential such as Monday to Friday(a job running only on weekdays), this expression is shorter:此外,如果请求的日期是连续的,例如星期一到星期五(作业仅在工作日运行),则此表达式更短:
@Scheduled(cron = "0 0 10 * * MON-FRI")
public void dailyScheduleJob() {
/*
your code here
*/
}
It is also possible to represent days with numbers from 1-7 .也可以用1-7 的数字表示天数。 In this case 1 will be SUN, and 7 will be SAT and the same cron job above can be written like this:
在这种情况下,1 将是 SUN,而 7 将是 SAT,上面相同的 cron 作业可以这样写:
@Scheduled(cron = "0 0 10 * * 2-6")
public void dailyScheduleJob() {
/*
your code here
*/
}
To run a job every Monday, Friday and Saturday at 10:00 AM: 每周一,周五和周六上午10:00开始工作:
@Scheduled(cron = "0 10 * * 1,5,6")
public void scheduleTaskUsingCronExpression() {
// ...
}
For more information, see Cron cheatsheet , or this answer . 有关更多信息,请参阅Cron cheatsheet或此答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.