How to Schedule Tasks in Spring Boot: Simple Guide
In Spring Boot, you schedule tasks by enabling scheduling with
@EnableScheduling and annotating methods with @Scheduled to define when they run. You can specify fixed delays, fixed rates, or cron expressions inside the @Scheduled annotation to control task timing.Syntax
To schedule a task in Spring Boot, first add @EnableScheduling to your configuration class to activate scheduling support. Then, create a method annotated with @Scheduled where you define the timing using one of these options:
fixedRate: runs the task at a fixed interval measured from the start of the last execution.fixedDelay: runs the task at a fixed interval measured from the end of the last execution.cron: uses a cron expression to specify complex schedules.
java
@Configuration @EnableScheduling public class SchedulerConfig { } @Component public class ScheduledTasks { @Scheduled(fixedRate = 5000) public void fixedRateTask() { System.out.println("Runs every 5 seconds"); } @Scheduled(fixedDelay = 7000) public void fixedDelayTask() { System.out.println("Runs 7 seconds after last completion"); } @Scheduled(cron = "0 0/1 * * * ?") public void cronTask() { System.out.println("Runs every minute"); } }
Example
This example shows a Spring Boot application that prints messages at different intervals using @Scheduled. It demonstrates fixed rate, fixed delay, and cron scheduling.
java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @SpringBootApplication public class SchedulingExampleApplication { public static void main(String[] args) { SpringApplication.run(SchedulingExampleApplication.class, args); } } @Configuration @EnableScheduling class SchedulerConfig { } @Component class ScheduledTasks { @Scheduled(fixedRate = 3000) public void fixedRateTask() { System.out.println("Fixed rate task - runs every 3 seconds"); } @Scheduled(fixedDelay = 5000) public void fixedDelayTask() { System.out.println("Fixed delay task - runs 5 seconds after last completion"); } @Scheduled(cron = "0/10 * * * * ?") public void cronTask() { System.out.println("Cron task - runs every 10 seconds"); } }
Output
Fixed rate task - runs every 3 seconds
Fixed delay task - runs 5 seconds after last completion
Cron task - runs every 10 seconds
... (repeats as scheduled)
Common Pitfalls
- Forgetting to add
@EnableSchedulingwill prevent scheduled methods from running. - Using
fixedRatewhen the task takes longer than the interval can cause overlapping executions. - Incorrect cron expressions can cause tasks to never run or run at unexpected times.
- Scheduled methods must have
voidreturn type and no parameters.
java
/* Wrong: Missing @EnableScheduling - tasks won't run */ @Component class WrongTasks { @Scheduled(fixedRate = 1000) public void task() { System.out.println("This won't run without @EnableScheduling"); } } /* Right: Add @EnableScheduling to config */ @Configuration @EnableScheduling class CorrectConfig { } /* Wrong: Scheduled method with parameters */ @Component class WrongParamTask { @Scheduled(fixedRate = 1000) public void taskWithParam(String param) { // Not allowed } } /* Right: No parameters */ @Component class CorrectTask { @Scheduled(fixedRate = 1000) public void correctTask() { // Allowed } }
Quick Reference
| Annotation | Description | Example |
|---|---|---|
| @EnableScheduling | Enables scheduling support in Spring Boot | @Configuration @EnableScheduling class Config {} |
| @Scheduled(fixedRate = ms) | Runs method repeatedly at fixed intervals from start time | @Scheduled(fixedRate = 5000) |
| @Scheduled(fixedDelay = ms) | Runs method repeatedly with delay after last completion | @Scheduled(fixedDelay = 7000) |
| @Scheduled(cron = "expression") | Runs method based on cron expression schedule | @Scheduled(cron = "0 0/1 * * * ?") |
Key Takeaways
Add @EnableScheduling to your configuration to activate scheduling.
Use @Scheduled on methods with fixedRate, fixedDelay, or cron to define task timing.
Scheduled methods must be void and have no parameters.
Test cron expressions carefully to avoid unexpected schedules.
Avoid long-running tasks with fixedRate to prevent overlaps.