Understanding Spring's @Scheduled Annotation and Its Task Scheduling Mechanism
This article explains how Spring's @Scheduled annotation works, covering configuration options like cron, fixedDelay and fixedRate, the internal processing by ScheduledAnnotationBeanPostProcessor, method reflection, task registration with ScheduledTaskRegistrar, and the underlying execution using ThreadPoolTaskScheduler and ScheduledThreadPoolExecutor.
The @Scheduled annotation in Spring allows methods to be executed periodically using three configuration styles: cron expressions, fixedDelay (based on the previous execution's completion time), and fixedRate (based on the previous execution's start time).
Processing of @Scheduled is handled by org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor , which scans beans for @Scheduled‑annotated methods, creates runnable tasks, and registers them with org.springframework.scheduling.config.ScheduledTaskRegistrar .
Reflection is used to collect all annotated methods:
Map<Method, Set<Scheduled>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
(MethodIntrospector.MetadataLookup<Set<Scheduled>>) method -> {
Set<Scheduled> scheduledAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations(
method, Scheduled.class, Schedules.class);
return (!scheduledAnnotations.isEmpty() ? scheduledAnnotations : null);
});Each method is wrapped into a org.springframework.scheduling.support.ScheduledMethodRunnable and handed to the registrar:
// Non‑empty set of methods
annotatedMethods.forEach((method, scheduledAnnotations) ->
scheduledAnnotations.forEach(scheduled -> processScheduled(scheduled, method, bean)));The registrar delegates execution to a TaskScheduler , typically the ThreadPoolTaskScheduler implementation. All scheduling ultimately relies on java.util.concurrent.ScheduledThreadPoolExecutor .
FixedDelay tasks are executed via ScheduledExecutorService#scheduleWithFixedDelay , while FixedRate tasks use ThreadPoolTaskScheduler#scheduleAtFixedRate . Cron tasks are managed by CronTrigger , which calculates the next execution time with CronTrigger#nextExecutionTime and reschedules the job.
During each execution the framework records the scheduling context (scheduled time, actual execution time, completion time) to compute the next trigger, updating the TriggerContext accordingly.
In summary, @Scheduled supports cron, fixedDelay, and fixedRate configurations; the annotation is processed by a BeanPostProcessor that reflects annotated methods, creates runnables, registers them with a task registrar, and finally executes them via a thread‑pool‑based scheduler backed by the JDK's ScheduledThreadPoolExecutor.
Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.