I have following job in spring boot application:
@Scheduled(cron = "0/5 * * * * *")
fun processDocumentsBatch() {
log.info("job has started")
Thread.sleep(7000)
log.info("job has finished")
}
I see logs like this:
2025-05-28T13:26:10.010+03:00 INFO 23424 --- [ scheduling-1] Service : job has started
2025-05-28T13:26:17.035+03:00 INFO 23424 --- [ scheduling-1] Service : job has finished
2025-05-28T13:26:20.005+03:00 INFO 23424 --- [ scheduling-1] Service : job has started
2025-05-28T13:26:27.041+03:00 INFO 23424 --- [ scheduling-1] Service : job has finished
2025-05-28T13:26:30.012+03:00 INFO 23424 --- [ scheduling-1] Service : job has started
2025-05-28T13:26:37.036+03:00 INFO 23424 --- [ scheduling-1] Service : job has finished
2025-05-28T13:26:40.003+03:00 INFO 23424 --- [ scheduling-1] Service : job has started
2025-05-28T13:26:47.031+03:00 INFO 23424 --- [ scheduling-1] Service : job has finished
I expected that in case of overlap the task will be put in a queue and it wil be taken for execution as soon as previous one is finished but looks like it is just skipped.
Based on this spring uses single thread executor with queue for scheduled tasks.
In Jdk I see this one and it has a queue.
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new AutoShutdownDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory));
}
So could you please clarify why job is cancelled ?
Is there way to change this behaviour to pospoone the task instead of cancelling ? Is there any configuration key for that purpose ?
@Scheduled(fixedRate = 5000)?