This guide walks you through the steps for scheduling tasks with Spring.
You will build an application that prints out the current time every five seconds by using
Spring’s @Scheduled
annotation.
For all Spring applications, you should start with the Spring Initializr. The Initializr offers a fast way to pull in all the dependencies you need for an application and does a lot of the setup for you. This example needs no Spring dependencies. Spring Boot itself provides all the Spring packages that you need for this guide. The following image shows the Initializr set up for this sample project:
Note
|
The preceding image shows the Initializr with Maven chosen as the build tool. You
can also use Gradle. It also shows values of com.example and scheduling-tasks as the
Group and Artifact, respectively. You will use those values throughout the rest of this
sample.
|
The following listing shows the pom.xml
file that is created when you choose Maven:
link:initial/pom.xml[role=include]
The following listing shows the build.gradle
file that is created when you choose Gradle:
link:initial/build.gradle[role=include]
The tests in
complete/src/test/java/com/example/schedulingtasks/ScheduledTasksTest.java
require the awaitility
library.
Note
|
Later versions of the awaitility library do not work for this test, so you have to
specify version 3.1.2.
|
To add the awaitility
library to Maven, add the following dependency:
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
The following listing shows the finished pom.xml
file:
link:complete/pom.xml[role=include]
To add the awaitility
library to Gradle, add the following dependency:
testImplementation 'org.awaitility:awaitility:3.1.2'
The following listing shows the finished build.gradle
file:
link:complete/build.gradle[role=include]
Now that you have set up your project, you can create a scheduled task. The following
listing (from src/main/java/com/example/schedulingtasks/ScheduledTasks.java
) shows how
to do so:
link:complete/src/main/java/com/example/schedulingtasks/ScheduledTasks.java[role=include]
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,
such as 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.
|
Although scheduled tasks can be embedded in web applications and WAR files, the simpler
approach (shown in in the next listing) creates a standalone application. To do so,
package everything in a single, executable JAR file, driven by a good old Java main()
method. The following listing (from
src/main/java/com/example/schedulingtasks/SchedulingTasksApplication.java
) shows the
application class:
link:complete/src/main/java/com/example/schedulingtasks/SchedulingTasksApplication.java[role=include]
The
@EnableScheduling
annotation 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 five seconds. The following listing shows typical output:
...
2019-10-02 12:07:35.659 INFO 28617 --- [ scheduling-1] c.e.schedulingtasks.ScheduledTasks : The time is now 12:07:35
2019-10-02 12:07:40.659 INFO 28617 --- [ scheduling-1] c.e.schedulingtasks.ScheduledTasks : The time is now 12:07:40
2019-10-02 12:07:45.659 INFO 28617 --- [ scheduling-1] c.e.schedulingtasks.ScheduledTasks : The time is now 12:07:45
2019-10-02 12:07:50.657 INFO 28617 --- [ scheduling-1] c.e.schedulingtasks.ScheduledTasks : The time is now 12:07:50
...
Congratulations! You created an application with a scheduled task. Also, this technique works in any type of application.
The following guides may also be helpful: