0
0
SpringbootHow-ToBeginner · 4 min read

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 fixedDelay but 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 fixedDelay without enabling scheduling via @EnableScheduling will not run the scheduled tasks.
  • Confusing fixedDelay with fixedRate: fixedDelay waits after the task finishes, fixedRate runs 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:

PropertyDescriptionTypeExample
fixedDelayDelay in milliseconds after task completion before next runlong@Scheduled(fixedDelay = 5000)
fixedDelayStringSame as fixedDelay but as String for configString@Scheduled(fixedDelayString = "5000")
@EnableSchedulingEnables Spring's scheduled task processingannotation@EnableScheduling on config class
Difference from fixedRatefixedDelay waits after task ends; fixedRate runs at fixed intervalsconceptfixedDelay = 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.