How to Use fixedDelay in @Scheduled in Spring Boot
In Spring Boot, use
@Scheduled(fixedDelay = milliseconds) to run a method repeatedly with a fixed delay after the previous execution finishes. The fixedDelay value sets the wait time in milliseconds between the end of one execution and the start of the next.Syntax
The @Scheduled annotation supports fixedDelay to schedule tasks with a fixed delay between executions.
- fixedDelay: Time in milliseconds to wait after the last execution finishes before starting the next.
- fixedDelayString: Same as
fixedDelaybut accepts a String value, useful for externalizing configuration.
java
@Scheduled(fixedDelay = 5000) public void task() { // task logic here }
Example
This example shows a Spring Boot component that prints the current time every 5 seconds, waiting 5 seconds after the previous run finishes before starting again.
java
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalTime; @Component public class FixedDelayExample { @Scheduled(fixedDelay = 5000) public void printTime() { System.out.println("Current time: " + LocalTime.now()); try { Thread.sleep(2000); // simulate task taking 2 seconds } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }
Output
Current time: 10:00:00.123
Current time: 10:00:07.125
Current time: 10:00:14.130
...
Common Pitfalls
- Using
fixedDelaywithout enabling scheduling via@EnableSchedulingwill not run the scheduled tasks. - Confusing
fixedDelaywithfixedRate:fixedDelaywaits after the task finishes,fixedRateruns at fixed intervals regardless of task duration. - Long-running tasks can delay subsequent executions when using
fixedDelay.
java
/* Wrong: Missing @EnableScheduling annotation */ @Component public class Task { @Scheduled(fixedDelay = 3000) public void run() { System.out.println("Running task"); } } /* Right: Add @EnableScheduling in a configuration class */ import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; @Configuration @EnableScheduling public class SchedulerConfig { }
Quick Reference
Summary of key points for using fixedDelay in Spring Boot scheduling:
| Property | Description | Type | Example |
|---|---|---|---|
| fixedDelay | Delay in milliseconds after task completion before next run | long | @Scheduled(fixedDelay = 5000) |
| fixedDelayString | Same as fixedDelay but as String for config | String | @Scheduled(fixedDelayString = "5000") |
| @EnableScheduling | Enables Spring's scheduled task processing | annotation | @EnableScheduling on config class |
| Difference from fixedRate | fixedDelay waits after task ends; fixedRate runs at fixed intervals | concept | fixedDelay = 5000 vs fixedRate = 5000 |
Key Takeaways
Use @Scheduled(fixedDelay = milliseconds) to run tasks with a delay after each execution finishes.
Always enable scheduling with @EnableScheduling in a configuration class.
fixedDelay waits after the task completes before starting again, unlike fixedRate.
Long-running tasks affect the interval between executions when using fixedDelay.
Use fixedDelayString to externalize delay values as strings for easier configuration.