Spring Boot provides a lightweight mechanism for scheduling tasks using the @Scheduled annotation, eliminating the need for external libraries like Quartz in many use cases. This built-in feature allows developers to configure recurring jobs directly through method-level annotations.
Understanding the @Scheduled Annotation
The @Scheduled annotation, part of org.springframework.scheduling.annotation, enables periodic execution of annotated methods. Only one of the following scheduling strategies must be specified per annotation:
cron: Uses a cron-like expression (including seconds)fixedDelay: Waits a fixed time after method completion before next executionfixedRate: Starts next execution at fixed intervals from the previous start time
Additional attributes include initialDelay (delay before first execution) and zone (timezone for cron expressions). All timing values can also be provided as strings via *String variants (e.g., fixedDelayString), supporting property placeholders and java.time.Duration formats.
Cron Expression Syntax
Cron expressions consist of 6-7 fields (seconds to year):
Seconds Minutes Hours DayOfMonth Month DayOfWeek [Year]
Common examples:
"0 0 12 * * ?": Daily at noon"0 15 10 ? * MON-FRI": Weekdays at 10:15 AM"0 0/5 14,18 * * ?": Every 5 minutes between 2-2:55 PM and 6-6:55 PM
Scheduling Strategies Compared
| Strategy | Behavior | Use Case |
|---|---|---|
| fixedRate | Next execution starts at fixed intervals from previous start time | Regular polling when task duration is predictable |
| fixedDelay | Next execution starts after fixed delay from previous completion time | Resource-intensive tasks requiring completion gaps |
| cron | Calendar-based scheduling with complex time patterns | Time-specific executions (e.g., daily reports) |
Implementation Example
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class TaskScheduler {
private final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
// Executes every 10 seconds from start time
@Scheduled(fixedRate = 10_000)
public void reportCurrentTime() {
System.out.println("Fixed rate: " + timeFormat.format(new Date()));
}
// First execution after 1s delay, then 2s after each completion
@Scheduled(initialDelay = 1000, fixedDelay = 2000)
public void delayedExecution() {
System.out.println("Fixed delay: " + timeFormat.format(new Date()));
}
// Executes daily at 21:41:50
@Scheduled(cron = "50 41 21 * * ?")
public void nightlyJob() {
System.out.println("Cron job: " + timeFormat.format(new Date()));
}
}
Enabling Scheduling
To activate scheduled tasks, add @EnableScheduling to your main application class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}