The behaviour of fixedRate
and cron
is different.
Overlapping jobs are queued for fixedRate
(as per the above answer from @Igor).
Overlapping jobs are skipped for cron
.
Sample Java code to demonstrate the difference:
int i = 0;
@Scheduled(fixedRate = 5000)
public void test() throws InterruptedException {
Date start = new Date();
if (i < 3) Thread.sleep(10000);
i++;
System.out.printf("start %TT, finish %TT, i = %s%n", start, new Date(), i);
}
And the output:
start 13:25:30, finish 13:25:40, i = 1
start 13:25:40, finish 13:25:50, i = 2
start 13:25:50, finish 13:26:00, i = 3
start 13:26:00, finish 13:26:00, i = 4
start 13:26:00, finish 13:26:00, i = 5
start 13:26:00, finish 13:26:00, i = 6
start 13:26:00, finish 13:26:00, i = 7
start 13:26:05, finish 13:26:05, i = 8
start 13:26:10, finish 13:26:10, i = 9
start 13:26:15, finish 13:26:15, i = 10
As can be seen, the overlapping jobs are queued and start as soon as the previous one completes, with no 5 second gap.
However, if we use @Scheduled(cron = "*/5 * * ? * *")
instead, the output becomes:
start 13:22:10, finish 13:22:20, i = 1
start 13:22:25, finish 13:22:35, i = 2
start 13:22:40, finish 13:22:50, i = 3
start 13:22:55, finish 13:22:55, i = 4
start 13:23:00, finish 13:23:00, i = 5
start 13:23:05, finish 13:23:05, i = 6
start 13:23:10, finish 13:23:10, i = 7
start 13:23:15, finish 13:23:15, i = 8
start 13:23:20, finish 13:23:20, i = 9
start 13:23:25, finish 13:23:25, i = 10
There is always a 5 second gap between the jobs. The overlapping jobs are NOT queued and are skipped.