Part of the
java-spring-tutorialscollection of Spring.io guide implementations.
A Spring Boot application that demonstrates scheduled task execution using the @Scheduled annotation. The app prints the current time to the console every 5 seconds.
Based on the official Spring Scheduling Tasks Guide.
- Java 17 or later
- Spring Boot 4.0.0 (via
spring-boot-starter-parent) - Maven (or the included Maven wrapper
mvnw)
From the project root:
./mvnw spring-boot:runYou should see log output like:
INFO ... The time is now 12:34:56
INFO ... The time is now 12:35:01
INFO ... The time is now 12:35:06
For more run options, see Running.
02-spring-scheduling-tasks/
├── pom.xml # Maven build configuration
├── mvnw, mvnw.cmd # Maven wrapper scripts
├── src/
│ ├── main/
│ │ ├── java/com/example/schedulingtasks/
│ │ │ ├── SchedulingTasksApplication.java # Main app with @EnableScheduling
│ │ │ └── ScheduledTasks.java # Scheduled task component
│ │ └── resources/
│ │ └── application.properties # App configuration
│ └── test/
│ └── java/com/example/schedulingtasks/
│ ├── SchedulingTasksApplicationTests.java
│ └── ScheduledTasksTest.java
└── docs/ # Documentation
├── images/ # Screenshots
├── setup/
│ ├── spring-initializr.md # Spring Initializr setup notes
│ └── run-instructions.md # How to run the app
├── concepts/
│ ├── scheduled-tasks.md # ScheduledTasks class breakdown
│ └── testing.md # Testing with Awaitility
└── reference/
└── guide.md # Spring guide reference
| File | Description |
|---|---|
SchedulingTasksApplication.java |
Main class with @SpringBootApplication and @EnableScheduling |
ScheduledTasks.java |
Component with @Scheduled method that logs the time |
application.properties |
Spring Boot configuration |
pom.xml |
Maven dependencies and build config |
| File | Description |
|---|---|
SchedulingTasksApplicationTests.java |
Verifies the Spring context loads |
ScheduledTasksTest.java |
Verifies the scheduler runs using Awaitility |
| File | Description |
|---|---|
docs/setup/spring-initializr.md |
How to set up the project with Spring Initializr |
docs/setup/run-instructions.md |
Detailed instructions for running the application |
docs/concepts/scheduled-tasks.md |
Breakdown of ScheduledTasks and SLF4J logging |
docs/concepts/testing.md |
How to test scheduled tasks with Awaitility |
docs/reference/guide.md |
Spring scheduling guide reference |
Added to the main application class to enable Spring scheduling:
@SpringBootApplication
@EnableScheduling
public class SchedulingTasksApplication { ... }Runs the annotated method every 5000 milliseconds (5 seconds):
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
log.info("The time is now {}", LocalTime.now().format(TIME_FORMATTER));
}fixedRate- Run at fixed intervals from the start of each invocation.fixedDelay- Run at fixed intervals from the end of each invocation.cron- Use cron expressions for complex schedules.initialDelay- Delay before the first execution.
See docs/setup/run-instructions.md for more detail.
Maven wrapper:
./mvnw spring-boot:runBuild and run JAR:
./mvnw clean package
java -jar target/scheduling-tasks-0.0.1-SNAPSHOT.jarRun tests from the project root:
./mvnw testThe project includes two tests:
SchedulingTasksApplicationTests- verifies the Spring context loads.ScheduledTasksTest- verifies the scheduler runs using Awaitility.
See docs/concepts/testing.md for a detailed breakdown of how the tests work.
Note: This project uses
@MockitoSpyBeaninstead of@SpyBeanbecause Spring Boot 4.0.0 and the newer Spring test support use the new Mockito-based override annotations (such as@MockitoSpyBean) instead of the older@SpyBeanyou might see in Spring Boot 3.x examples and guides.
Example ./mvnw test run with all tests passing:

