SteelReed

Search

Search posts, questions…

Spring BootQuestion 23 / 26

Junior

How do you schedule jobs with @Scheduled in Spring? How do cron, fixedRate and fixedDelay differ?

Short answer

Turn on @EnableScheduling and put @Scheduled on a method. fixedRate counts from when the previous run started, fixedDelay from when it finished, cron follows the calendar.

fixedRate = 5sfixedDelay = 5scron 0 0 2 * * *Run 1 starts at 0stakes 3sRun 2 at 5sRun 2 at 8sExactly 2 a.m.

3 points to say:

  1. fixedRate: from the start; fixedDelay: from the end, so runs never overlap; cron: real calendar time.
  2. Spring cron has 6 fields, with seconds first, versus 5 fields in Unix cron.
  3. Always set zone, and use day names (MON-FRI) rather than numbers.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.