0
0
SpringbootHow-ToBeginner · 3 min read

How to Use fixedRate in @Scheduled in Spring Boot

In Spring Boot, use @Scheduled(fixedRate = milliseconds) to run a method repeatedly at a fixed interval measured from the start of the last invocation. This ensures the method runs every specified milliseconds regardless of how long the previous execution took.
📐

Syntax

The @Scheduled annotation schedules a method to run periodically. The fixedRate attribute defines the interval in milliseconds between the start of each method execution.

  • fixedRate: Time in milliseconds between method start times.
  • fixedDelay: (Not shown here) Time between method end and next start.
  • initialDelay: (Optional) Delay before first execution.
java
@Scheduled(fixedRate = 5000)
public void task() {
    // task code here
}
💻

Example

This example shows a Spring Boot component with a method that prints the current time every 3 seconds using fixedRate. The method runs repeatedly at fixed intervals regardless of how long the method takes to execute.

java
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalTime;

@Component
public class FixedRateExample {

    @Scheduled(fixedRate = 3000)
    public void printTime() {
        System.out.println("Current time: " + LocalTime.now());
    }
}
Output
Current time: 10:00:00.123 Current time: 10:00:03.123 Current time: 10:00:06.123 ...
⚠️

Common Pitfalls

Common mistakes when using fixedRate include:

  • Not enabling scheduling with @EnableScheduling in a configuration class.
  • Confusing fixedRate with fixedDelay. fixedRate counts from method start, fixedDelay counts from method end.
  • Using very short intervals without considering method execution time, which can cause overlapping executions.
java
/* Wrong: Missing @EnableScheduling annotation */
@Component
public class Task {
    @Scheduled(fixedRate = 1000)
    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

  • @Scheduled(fixedRate = ms): Runs method every ms milliseconds from start time.
  • @Scheduled(fixedDelay = ms): Runs method ms milliseconds after last execution finishes.
  • @EnableScheduling: Must be added to enable scheduling.
  • Use initialDelay to delay first run.

Key Takeaways

Use @Scheduled(fixedRate = milliseconds) to run tasks at fixed intervals from method start.
Always enable scheduling with @EnableScheduling in a configuration class.
fixedRate triggers tasks regardless of previous execution duration, possibly overlapping if tasks run long.
Use initialDelay to postpone the first execution if needed.
Understand difference between fixedRate (start-based) and fixedDelay (end-based) scheduling.