Spring Boot allows developers to easily schedule and execute background tasks using the @Scheduled annotation. This is useful for running periodic jobs, automation tasks, and batch processing without manual intervention.
In this section, we clarify Why Use @Scheduled in Spring Boot? and summarize the key points you will apply in implementation.
Ensure you have the following:
To enable scheduling, you need to include spring-boot-starter-web in your pom.xml or build.gradle file.
Maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Gradle:
implementation 'org.springframework.boot:spring-boot-starter-web'
Why
spring-boot-starter-web? Spring Boot applications using@Scheduledneed a running Spring Context. Includingspring-boot-starter-webensures that the application lifecycle is properly managed.
To enable scheduling, annotate your main application class with @EnableScheduling.
Define a scheduled task using @Scheduled.
Run the Spring Boot application:
./mvnw spring-boot:run
Or using Gradle:
gradle bootRun
The scheduled task will execute every 5 seconds and print a timestamp.
In this section, we clarify Testing the Scheduled Task and summarize the key points you will apply in implementation.
Compare your console logs with this output to quickly confirm the behavior is correct.
Scheduled task executed at: 12:00:01 Scheduled task executed at: 12:00:06 Scheduled task executed at: 12:00:11
You now have a practical Spring Boot Scheduled Tasks implementation with a clear, production-friendly Spring Boot structure. As a next step, adapt configuration and tests to your own domain, then validate behavior under realistic traffic and failure scenarios.
package com.example.scheduled; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class ScheduledApplication { public static void main(String[] args) { SpringApplication.run(ScheduledApplication.class, args); } }
package com.example.scheduled import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication import org.springframework.scheduling.annotation.EnableScheduling @SpringBootApplication @EnableScheduling class ScheduledApplication fun main(args: Array<String>) { runApplication<ScheduledApplication>(*args) }
package com.example.scheduled; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.time.LocalTime; @Service public class ScheduledTask { @Scheduled(fixedRate = 5000) public void runTask() { System.out.println("Scheduled task executed at: " + LocalTime.now()); } }
package com.example.scheduled import org.springframework.scheduling.annotation.Scheduled import org.springframework.stereotype.Service import java.time.LocalTime @Service class ScheduledTask { @Scheduled(fixedRate = 5000) fun runTask() { println("Scheduled task executed at: ${LocalTime.now()}") } }