Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

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:

  1. Enable scheduling in configuration class.
  2. 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.