Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple @Scheduled annotations at one method

Is it allowed / working to have multiple @Scheduled annotations at one method?

@Scheduled(cron = "0 5 0 * * *", zone = "Europe/Stockholm")
@Scheduled(fixedRate = 1000 * 60 * 20, initialDelay = 1000 * 60 * 5)
public void setSalariesAsArchived() {
//...
}
like image 565
Artegon Avatar asked Apr 22 '26 06:04

Artegon


1 Answers

Yes, this is perfectly legal as @Scheduled is a @Repeatable annotation like stated in the Javadoc for @Schedules

Container annotation that aggregates several Scheduled annotations. Can be used natively, declaring several nested Scheduled annotations. Can also be used in conjunction with Java 8's support for repeatable annotations, where Scheduled can simply be declared several times on the same method, implicitly generating this container annotation.

So you can either use it as you did, or use @Schedules to wrap it like in the following example

@Schedules({
    @Scheduled(cron = "0 5 0 * * *", zone = "Europe/Stockholm"),
    @Scheduled(fixedRate = 1000 * 60 * 20, initialDelay = 1000 * 60 * 5)
})
public void setSalariesAsArchived() {
//...
}
like image 98
Yassin Hajaj Avatar answered Apr 24 '26 20:04

Yassin Hajaj