Search code examples
javaspring-bootcronannotationsquartz-scheduler

Spring-Boot @Scheduled Cron expressions to make slight delay between two tasks?


I am using Spring-Boot @Scheduled Cron for caching data retrieved from persistent storage.

I have two different tasks,

  • Set result in cache
  • Clear cache

Task1 will run for every 15 minutes. I have set cron like

@Scheduled(cron = "0 0/15 * * * *")

so the frequency would be

12:00:00
12:15:00
12:30:00

Now I want to run Task2 10 seconds before Task1

ie

11:59:50
12:14:50
12:29:50

I am trying this expression

@Scheduled(cron = "50 0/14 * * * *")

But it fires for every 14 minutes interval.

Can anyone please suggest me a solution to fix it?


Solution

  • Try with:

    @Scheduled(cron = "50 59/15 * * * *")
    

    Explanation:

    50 59/15 * * * * = at 50 seconds, every hour at 59 minute, every 15 minutes
    50 59/15 * * * * = starting at 11:59:50 for every 15 minutes.
    

    Your issue is that */X means "every X". So 0/14 means every 14 minutes.

    More you can read in the docs: