Skip to content

Latest commit

 

History

History
168 lines (121 loc) · 7.38 KB

File metadata and controls

168 lines (121 loc) · 7.38 KB

Spring Scheduling Tasks

Coverage Mutation SpotBugs

Part of the java-spring-tutorials collection 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.

Requirements

  • Java 17 or later
  • Spring Boot 4.0.0 (via spring-boot-starter-parent)
  • Maven (or the included Maven wrapper mvnw)

Quick start (Maven)

From the project root:

./mvnw spring-boot:run

You 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.

Example console output

Screenshot 2025-12-10 at 2 14 13 PM

Project structure

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 index

Source files

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

Test files

File Description
SchedulingTasksApplicationTests.java Verifies the Spring context loads
ScheduledTasksTest.java Verifies the scheduler runs using Awaitility

Documentation

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

Key concepts

@EnableScheduling

Added to the main application class to enable Spring scheduling:

@SpringBootApplication
@EnableScheduling
public class SchedulingTasksApplication { ... }

@Scheduled(fixedRate = 5000)

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));
}

Scheduling options

  • 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.

Running

See docs/setup/run-instructions.md for more detail.

Maven wrapper:

./mvnw spring-boot:run

Build and run JAR:

./mvnw clean package
java -jar target/scheduling-tasks-0.0.1-SNAPSHOT.jar

Testing

Run tests from the project root:

./mvnw test

The 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 @MockitoSpyBean instead of @SpyBean because 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 @SpyBean you might see in Spring Boot 3.x examples and guides.

Test output

Example ./mvnw test run with all tests passing:

Screenshot 2025-12-10 at 3 04 09 PM