简体   繁体   English

在特定日期使用 (Spring @Scheduled) 运行作业

[英]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.

相关问题 如何在Java Spring中运行预定作业? - How to run scheduled job in java spring? Spring @Scheduled 作业将每秒运行一次,但在第二个之后的特定毫秒数 - Spring @Scheduled job that will run every second but at a specific number of milliseconds after the second 在@Scheduled 中运行的作业不会调用 spring 数据 jpa 保存 - Job run in @Scheduled does not invoke spring data jpa save Java Spring Scheduled作业不起作用 - Java Spring Scheduled job not working 如何仅在特定年份运行春季预定工作? - How to run spring scheduled jobs only in specific year? 如何在 1 个给定的 UAT 服务器中以计划模式运行 spring 批处理作业,但在所有 3 个给定的 UAT 服务器中运行手动作业? - How to run spring batch job in scheduled mode in 1 given UAT server, but run manual job in all 3 given UAT servers? 是否可以将带有Spring @Scheduled批注的作业安排为每小时运行一次,但每次随机运行一次? - Is it possible to schedule a job with Spring @Scheduled annotation to run every hour but each time at random hour? 计划任务在给定时间每3天运行一次 - Scheduled task to run every 3 days at given time 如何在Spring中运行计划任务? - How to run scheduled tasks in Spring? Spring预定作业中的Hibernate Lazy init异常 - Hibernate Lazy init exception in spring scheduled job
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM