Skip to content
Scheduling Tasks :: Learn how to schedule tasks with Spring.
Branch: master
Clone or download
spring-operator and gregturn URL Cleanup (#17)
This commit updates URLs to prefer the https protocol. Redirects are not followed to avoid accidentally expanding intentionally shortened URLs (i.e. if using a URL shortener).

# Fixed URLs

## Fixed Success
These URLs were switched to an https URL with a 2xx status. While the status was successful, your review is still recommended.

* [ ] http://www.apache.org/licenses/LICENSE-2.0 with 4 occurrences migrated to:
  https://www.apache.org/licenses/LICENSE-2.0 ([https](https://www.apache.org/licenses/LICENSE-2.0) result 200).
Latest commit 7f5a0c5 Mar 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
complete URL Cleanup (#17) Mar 21, 2019
initial Upgrade to Spring Boot 2.1.3.RELEASE Mar 20, 2019
test Convert to mvn compile for initial codeset Feb 19, 2014
.gitignore
.travis.yml
CONTRIBUTING.adoc
LICENSE.code.txt
LICENSE.writing.txt URL Cleanup (#16) Mar 21, 2019
README.adoc

README.adoc

This guide walks you through the steps for scheduling tasks with Spring.

What you’ll build

You’ll build an application that prints out the current time every five seconds using Spring’s @Scheduled annotation.

Create a scheduled task

Now that you’ve set up your project, you can create a scheduled task.

src/main/java/hello/ScheduledTasks.java

link:complete/src/main/java/hello/ScheduledTasks.java[]

The Scheduled annotation defines when a particular method runs. NOTE: This example uses fixedRate, which specifies the interval between method invocations measured from the start time of each invocation. There are other options, like fixedDelay, which specifies the interval between invocations measured from the completion of the task. You can also use @Scheduled(cron=". . .") expressions for more sophisticated task scheduling.

Enable Scheduling

Although scheduled tasks can be embedded in web apps and WAR files, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java main() method.

src/main/java/hello/Application.java

link:complete/src/main/java/hello/Application.java[]

@EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.

Logging output is displayed and you can see from the logs that it is on a background thread. You should see your scheduled task fire every 5 seconds:

[...]
2016-08-25 13:10:00.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:00
2016-08-25 13:10:05.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:05
2016-08-25 13:10:10.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:10
2016-08-25 13:10:15.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:15

Summary

Congratulations! You created an application with a scheduled task. Heck, the actual code was shorter than the build file! This technique works in any type of application.

You can’t perform that action at this time.