SpringBoot 1.3.0 Scheduling: Setup, Cron Expressions, fixedRate, fixedDelay, and Asynchronous Execution
This guide explains how to enable SpringBoot scheduling, write cron expressions, configure fixedRate and fixedDelay intervals, and run scheduled tasks asynchronously, providing step‑by‑step instructions and a detailed table of cron field meanings for backend developers.
Original link: https://www.cnblogs.com/fanwenkeer/p/18194168
There is a daily requirement to extract and aggregate data, such as calculating the average processing time of each department's sales review process.
Preparation
1. Add @EnableScheduling on the main application class to enable scheduling.
2. Annotate the scheduled method with @Scheduled and mark the class with @Component .
3. Write a cron expression or configure fixedRate / fixedDelay attributes.
Cron Expressions
SpringBoot supports 6‑field cron expressions and does not support the 7‑field (Year) format.
Only the special symbol ? is supported in SpringBoot cron expressions; other symbols are rarely needed.
Seconds
Minutes
Hours
DayofMonth
Month
DayofWeek
Year
Field
Seconds
Minutes
Hours
DayofMonth
Month
DayofWeek
Year
Meaning
Second
Minute
Hour
Day
Month
Weekday
Year
Symbol
, - * /
, - * /
, - * /
, - * / ?
, - * /
, - * /
, - * / ?
Symbol explanations:
, : enumeration (e.g., 1,5 in Seconds means trigger at the 1st and 5th second).
- : range (e.g., 1-5 in Seconds means trigger every second from 1 to 5).
* : wildcard, meaning every unit (e.g., every second).
/ : interval (e.g., 1/5 in Seconds means trigger at second 1, then every 5 seconds).
? : no specific value, can only be used in DayofMonth and DayofWeek ; the two fields are mutually exclusive and one must be set.
fixedRate
The fixedRate attribute in @Scheduled specifies the execution interval in milliseconds, meaning the method runs at a constant frequency.
Example: setting fixedRate = 1000 makes the task run every 1 second, waiting 1 second after the start of the previous execution before the next run.
If the previous execution takes longer than the interval, the next execution starts immediately after the previous one finishes.
fixedDelay
The fixedDelay attribute in @Scheduled specifies the delay between the end of one execution and the start of the next, also in milliseconds.
Example: setting fixedDelay = 1000 makes the task run every 1 second, waiting 1 second after the previous execution completes before starting again.
If the previous execution blocks, the scheduler waits for it to finish and then respects the configured delay before the next run.
Enable Asynchronous Execution of Scheduled Tasks
1. Add @EnableAsync on the main application class.
2. Annotate the scheduled method with @Async .
Enabling async causes each scheduled task to be executed in a separate thread from a thread pool, preventing long‑running tasks from blocking other scheduled jobs.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.