What if your app could remember to do important jobs on time, all by itself?
Why Scheduled tasks with @Scheduled in Spring Boot? - Purpose & Use Cases
Imagine you need to send reminder emails every day at 9 AM, but you have to remember to run the code yourself or set up complicated external tools.
Manually running tasks or relying on external schedulers is error-prone, easy to forget, and hard to maintain. It can cause missed tasks or duplicated work.
The @Scheduled annotation lets your Spring Boot app run tasks automatically on a set schedule, without extra setup or manual intervention.
new Timer().schedule(new TimerTask() { public void run() { sendEmail(); } }, delay, period);@Scheduled(cron = "0 0 9 * * *")
public void sendEmail() { ... }You can automate repetitive tasks inside your app reliably and clearly, freeing you from manual triggers or external schedulers.
A Spring Boot app that cleans up old files every night at midnight without anyone needing to start it manually.
Manual task scheduling is unreliable and hard to maintain.
@Scheduled automates running code on a fixed schedule inside Spring Boot.
This makes your app smarter and reduces human error.
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
