Scheduled tasks let your app do things automatically at set times. This helps with regular jobs like cleaning up data or sending reminders without you doing it manually.
Scheduled tasks with @Scheduled in Spring Boot
@Scheduled(cron = "cron_expression")
public void methodName() {
// task code here
}The @Scheduled annotation goes above a method to run it on a schedule.
You can use cron, fixedRate, or fixedDelay to set timing.
@Scheduled(fixedRate = 5000) public void runEvery5Seconds() { System.out.println("Runs every 5 seconds"); }
@Scheduled(fixedDelay = 10000) public void run10SecondsAfterLastFinish() { System.out.println("Runs 10 seconds after last finish"); }
@Scheduled(cron = "0 0 9 * * ?") public void runAt9AMEveryDay() { System.out.println("Runs every day at 9 AM"); }
This Spring Boot app prints a message every 3 seconds automatically. The @EnableScheduling annotation turns on scheduling support.
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; @SpringBootApplication @EnableScheduling public class ScheduledTaskApp { public static void main(String[] args) { SpringApplication.run(ScheduledTaskApp.class, args); } @Scheduled(fixedRate = 3000) public void printMessage() { System.out.println("Hello! This runs every 3 seconds."); } }
Remember to add @EnableScheduling on a configuration or main class to activate scheduling.
Cron expressions are powerful but can be tricky; use online tools to build them.
Scheduled methods should not take parameters and should return void.
@Scheduled runs methods automatically on a set schedule.
You can schedule by fixed rate, fixed delay, or cron expressions.
Enable scheduling with @EnableScheduling in your Spring Boot app.
Practice
@Scheduled annotation in Spring Boot?Solution
Step 1: Understand the role of @Scheduled
The@Scheduledannotation is used to mark methods that should run automatically on a schedule without manual calls.Step 2: Compare with other options
Options A, C, and D relate to other Spring features like REST, dependency injection, and database setup, not scheduling.Final Answer:
To run a method automatically at fixed intervals or times -> Option AQuick Check:
@Scheduled = automatic method run [OK]
- Confusing @Scheduled with REST or dependency injection annotations
- Thinking @Scheduled configures databases
- Assuming @Scheduled requires manual method calls
@Scheduled?Solution
Step 1: Understand fixedRate vs fixedDelay
fixedRateruns the method every specified milliseconds regardless of method duration.fixedDelaywaits after method finishes before next run.Step 2: Check the options
@Scheduled(fixedRate = 5000) usesfixedRate = 5000which means run every 5 seconds. @Scheduled(fixedDelay = 5000) uses fixedDelay which waits after method ends. @Scheduled(rate = 5) uses invalid attributerate. @Scheduled(cron = "0 */5 * * * *") uses a cron expression which runs every 5 minutes.Final Answer:
@Scheduled(fixedRate = 5000) -> Option DQuick Check:
fixedRate = 5000 means every 5 seconds [OK]
- Using 'rate' instead of 'fixedRate'
- Confusing fixedDelay with fixedRate
- Incorrect cron syntax for simple intervals
@Scheduled(fixedDelay = 3000)
public void printMessage() {
System.out.println("Hello World");
}Solution
Step 1: Understand fixedDelay behavior
fixedDelay = 3000means the method runs, then waits 3 seconds after it finishes before running again.Step 2: Analyze the print output frequency
Since the method prints "Hello World" each time it runs, it will print every 3 seconds after the previous run completes.Final Answer:
"Hello World" prints every 3 seconds after method finishes -> Option CQuick Check:
fixedDelay = wait after method ends [OK]
- Confusing fixedDelay with fixedRate
- Thinking method runs only once
- Assuming compilation error without reason
@Scheduled(fixedRate = "5000")
public void task() {
System.out.println("Running task");
}Solution
Step 1: Check fixedRate attribute type
ThefixedRateattribute expects a long number (milliseconds), not a string. Using quotes causes a syntax error.Step 2: Verify other options
Method can be void, so no return needed. Missing @EnableScheduling is a configuration issue but not a syntax error here. Method name 'task' is valid.Final Answer:
fixedRate value should be a number, not a string -> Option AQuick Check:
fixedRate expects number, not string [OK]
- Putting quotes around fixedRate or fixedDelay values
- Assuming method must return a value
- Confusing configuration errors with syntax errors
@Scheduled. Which cron expression is correct?Solution
Step 1: Understand cron format in Spring
Spring cron has 6 fields: second, minute, hour, day of month, month, day of week.Step 2: Analyze each option
@Scheduled(cron = "0 30 8 ? * MON"): "0 30 8 ? * MON" means at second 0, minute 30, hour 8, any day of month (?), any month (*), on Monday (MON). This matches 8:30 AM Monday.
@Scheduled(cron = "30 8 * * 1") is missing seconds field and uses 5 fields.
@Scheduled(cron = "0 8 30 * * MON") has hour and minute swapped.
@Scheduled(cron = "0 30 8 * * 1") uses day of week as 1 which is Sunday in some systems, MON is clearer.Final Answer:
@Scheduled(cron = "0 30 8 ? * MON") -> Option BQuick Check:
Spring cron: sec min hour day month weekday [OK]
- Using 5-field cron instead of 6-field in Spring
- Swapping hour and minute positions
- Using numeric day of week without knowing system mapping
Ready to prove it?
Pass the test and this pattern moves unknown
