Spring Boot FAQ: Top Questions
40. How do you schedule tasks in Spring Boot?
Task scheduling in Spring Boot is achieved using @Scheduled
annotation and enabling scheduling through @EnableScheduling
.
πΊοΈ Steps:
- Enable scheduling in configuration class.
- Annotate methods with scheduling intervals.
π₯ Example:
@Scheduled(cron = "0 0 * * * ?")
public void runEveryHour() {
System.out.println("Task executed hourly");
}
π Expected Output:
Method runs every hour based on cron expression.
π οΈ Use Cases:
- Daily reports, data clean-up, or sync jobs.
- Scheduled emails or notifications.