Jordan
Jordan

Reputation: 1965

Java task scheduler run daily from start to end date

I've got a spring boot application in java with some scheduled tasks I need to run at midnight every day, from 15th June to 15th August

@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();
}

Is there a better way to do this so I don't need 3 separate @Scheduled functions?

Upvotes: 1

Views: 1495

Answers (1)

reto
reto

Reputation: 10453

You could simply repeat these annotations, if you are on Spring 4 / JDK 8

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

else, JDK 6+

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

Upvotes: 4

Related Questions