86 lines
3.9 KiB
Plaintext
86 lines
3.9 KiB
Plaintext
:toc:
|
|
:spring_version: current
|
|
:icons: font
|
|
:source-highlighter: prettify
|
|
:project_id: gs-scheduling-tasks
|
|
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.
|
|
|
|
== What you'll need
|
|
|
|
:java_version: 1.8
|
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
|
|
|
|
|
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]
|
|
|
|
|
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-gradle.adoc[]
|
|
|
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-maven.adoc[]
|
|
|
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-sts.adoc[]
|
|
|
|
|
|
|
|
[[initial]]
|
|
== Create a scheduled task
|
|
Now that you've set up your project, you can create a scheduled task.
|
|
|
|
`src/main/java/hello/ScheduledTasks.java`
|
|
[source,java]
|
|
----
|
|
include::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 https://docs.spring.io/spring/docs/{spring_version}/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-scheduled[other options], like `fixedDelay`, which specifies the interval between invocations measured from the completion of the task. You can also https://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html[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`
|
|
[source,java]
|
|
----
|
|
include::complete/src/main/java/hello/Application.java[]
|
|
----
|
|
|
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/spring-boot-application.adoc[]
|
|
|
|
https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#scheduling-enable-annotation-support[`@EnableScheduling`] ensures that a background task executor is created. Without it, nothing gets scheduled.
|
|
|
|
|
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_subhead.adoc[]
|
|
|
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_with_both.adoc[]
|
|
|
|
|
|
|
|
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.
|
|
|
|
== See Also
|
|
|
|
The following guides may also be helpful:
|
|
|
|
* https://spring.io/guides/gs/spring-boot/[Building an Application with Spring Boot]
|
|
* https://spring.io/guides/gs/batch-processing/[Creating a Batch Service]
|
|
|
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/footer.adoc[]
|
|
|