【问题标题】:Java task scheduler run daily from start to end dateJava 任务调度程序每天从开始到结束日期运行
【发布时间】:2015-08-14 11:48:40
【问题描述】:

我有一个 java 中的 Spring Boot 应用程序,我需要在每天午夜运行一些计划任务,从 6 月 15 日到 8 月 15 日

@Scheduled(cron = "0 0 0 15-30 5 ?") // Midnight every day from 15th June until end of month
public void sendReminderEmailsJune() {
    doStuff();
}

@Scheduled(cron = "0 0 0 * 6 ?") // Every day in July
public void sendReminderEmailsJuly() {
    doStuff();
}

@Scheduled(cron = "0 0 0 1-15 7 ?") // The first day in August to 15th August
public void sendRemindersEmailsAugust() {
    doStuff();
}

有没有更好的方法来做到这一点,所以我不需要 3 个单独的 @Scheduled 函数?

【问题讨论】:

    标签: spring-boot scheduled-tasks


    【解决方案1】:

    您可以简单地重复这些注释,如果您在 春季 4 / JDK 8

    @Scheduled(cron = "0 0 12 * * ?")   
    @Scheduled(cron = "0 0 18 * * ?")   
    public void sendReminderEmails() {...}
    

    否则,JDK 6+

    @Schedules({ 
        @Scheduled(cron = "0 0 12 * * ?"),  
        @Scheduled(cron = "0 0 18 * * ?")}) 
    public void sendReminderEmails() {...}  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多