Ajmal V Aliyar
Ajmal V Aliyar

Reputation: 1669

Run a job with Spring @Scheduled annotation at two different times on different days of the week

I have a job that has to be run

My thinking is - Either

Is this possible?

Upvotes: 1

Views: 2566

Answers (2)

Ajmal V Aliyar
Ajmal V Aliyar

Reputation: 1669

@Schedules annotation can be used to set multiple calendar-based timer expressions.

This annotation will get rid of the redundancy of writing the same method multiple times.

In the following example, the first expression sets a timer to expire on the last day of every month. The second expression sets a timer to expire every Friday at 11:00 PM.

@Schedules ({
    @Schedule(dayOfMonth="Last"),
    @Schedule(dayOfWeek="Fri", hour="23")
})
public void doPeriodicCleanup() { ... }

Upvotes: 4

dxjuv
dxjuv

Reputation: 909

If your method does exactly the same work then write it once and call it twice with two different CRONs.

public void doStuff(){
//do stuff
}

@Scheduled //With 8 AM Weekdays CRON
doStuff();

@Scheduled //With 10 AM Weekends CRON
doStuff();

Upvotes: 2

Related Questions