Multiple @Scheduled annotations at one method

Viewed 634

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() {
//...
}
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() {
//...
}
Related