How to Use @Scheduled Annotation in Spring for Task Scheduling
Use the
@Scheduled annotation on a method to run it automatically at fixed intervals or cron expressions. Enable scheduling by adding @EnableScheduling to a configuration class. The method annotated with @Scheduled should have no parameters and return void.Syntax
The @Scheduled annotation can be used with different parameters to control when the method runs:
fixedRate: Runs the method at a fixed interval in milliseconds.fixedDelay: Runs the method with a fixed delay after the previous execution finishes.cron: Runs the method based on a cron expression for flexible scheduling.
The method must be void and take no arguments.
java
@Scheduled(fixedRate = 5000) public void task() { // code to run every 5 seconds } @Scheduled(fixedDelay = 10000) public void taskWithDelay() { // code runs 10 seconds after last execution finishes } @Scheduled(cron = "0 0/1 * * * ?") public void cronTask() { // code runs every minute }
Example
This example shows a Spring Boot application that prints a message every 3 seconds using @Scheduled(fixedRate = 3000). Scheduling is enabled with @EnableScheduling.
java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @SpringBootApplication @EnableScheduling public class ScheduledExampleApplication { public static void main(String[] args) { SpringApplication.run(ScheduledExampleApplication.class, args); } } @Component class ScheduledTasks { @Scheduled(fixedRate = 3000) public void printMessage() { System.out.println("Task executed at " + java.time.LocalTime.now()); } }
Output
Task executed at 10:00:00.123
Task executed at 10:00:03.124
Task executed at 10:00:06.125
... (every 3 seconds)
Common Pitfalls
- Forgetting to add
@EnableSchedulingin a configuration class disables all scheduled tasks. - The scheduled method must be public, return
void, and have no parameters. - Using
fixedRateruns tasks at a fixed interval regardless of task duration, which can cause overlap if the task takes longer than the interval. - Using
fixedDelaywaits for the task to finish before starting the delay timer. - Incorrect cron expressions can cause tasks not to run; always validate cron syntax.
java
@Component class WrongScheduled { // Wrong: method has parameters @Scheduled(fixedRate = 1000) public void taskWithParam(String param) { System.out.println(param); } // Correct: @Scheduled(fixedRate = 1000) public void correctTask() { System.out.println("Runs every second"); } }
Quick Reference
Use this quick guide to remember key points about @Scheduled:
| Parameter | Description | Example |
|---|---|---|
| fixedRate | Runs method every fixed milliseconds regardless of duration | @Scheduled(fixedRate = 5000) |
| fixedDelay | Runs method after fixed delay from last completion | @Scheduled(fixedDelay = 5000) |
| cron | Runs method based on cron expression | @Scheduled(cron = "0 0/5 * * * ?") |
| @EnableScheduling | Enable scheduling support in Spring | Add on a @Configuration or @SpringBootApplication class |
Key Takeaways
Add @EnableScheduling to enable scheduled tasks in Spring.
Annotate a void, no-argument method with @Scheduled to run it automatically.
Use fixedRate for regular intervals and fixedDelay to wait after task completion.
Validate cron expressions carefully to avoid scheduling errors.
Scheduled methods must be public and have no parameters.