Search code examples
springspring-bootcronscheduled-tasksspring-annotations

Multiple Scheduled in the same method


I'm working with @Scheduled annotations. I need to run the method in different moments, exactly each 2 and 5 seconds.

How can I do it?

In this moment my code is the next:

@Scheduled(cron = "${cron.startdate}")
public void check() {
    LOGGER.info("1 - Check DB");

}

and the application.yml:

cron:
    startdate: 0/2 * * * * *

My configuration will be executed each 2 seconds, but I need it to be executed each 5 seconds too.

The output should be:

[11:20:00] | 1 - Check DB
[11:20:02] | 1 - Check DB
[11:20:04] | 1 - Check DB
[11:20:05] | 1 - Check DB
[11:20:06] | 1 - Check DB
[11:20:08] | 1 - Check DB
[11:20:10] | 1 - Check DB
[11:20:12] | 1 - Check DB
.
.
.

Thanks guys.


Solution

  • I think @Scheduled(cron = "0/2,0/5 * * * * *") should work.