Multiple @Scheduled annotations at one method
Asked Answered
R

1

8

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() {
//...
}
Rabbinism answered 5/5, 2021 at 9:32 Comment(0)
D
12

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() {
//...
}
Diazomethane answered 5/5, 2021 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.