Sahil Verma
Sahil Verma

Reputation: 141

Spring scheduler: start at 12:25PM and run every 15 minutes after that

I have 2 server, and on both scheduler runs: 1) Main server 2) Disaster recovery server

I want Fist scheduler to run at 12:15 and after 12:15 it should run every 15 minutes. and second scheduler to start running at 12:25 and after that every 15 minutes it should run.

so both will not collide with each other.

Upvotes: 2

Views: 3810

Answers (1)

diginoise
diginoise

Reputation: 7620

Unfortunately you cannot encode start at 12:25pm and then every 15 minutes using @Scheduled(initialDelay = X, fixedDelay = Y, fixedRate = 15 * 60 * 1000) nor using CRON expressions.

Fortunately you just need to encode every 15 minutes starting at 25 (or 15) minutes past the hour, every hour, every day, every year

Using an online cron expression generator we have (please note discussion around 5, 6 or 7 fields cron expressions below):

@Scheduled(cron="0 25/15 * ? * *") for every 15 min starting at 25 min past the hour and @Scheduled(cron="0 15/15 * ? * *") for every 15 min starting at 15 min past the hour.

Please don't forget @EnableScheduling annotation on your config.

*cron expressions discussion*

Please note that cron expressions can have 5 (no seconds) as per crontab which Spring says it supports, however it fails with 5, 6 (seconds, and days of the month) which is the only option supported as far as my crude tests went, or 7 fields(seconds, days of month and days of week). Spring has rejected originally pasted 7 fields expression; I have tested with 6 and it worked correctly (but days and years were stars, i.e. ALL

Upvotes: 3

Related Questions