/gs-scheduling-tasks

Scheduling Tasks :: Learn how to schedule tasks with Spring.

Primary LanguageJavaApache License 2.0Apache-2.0

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

What You Will Build

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

What You Need

Starting with Spring Initializr

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 listing shows the pom.xml file that is created when you choose Maven:

link:initial/pom.xml[]

The following listing shows the build.gradle file that is created when you choose Gradle:

link:initial/build.gradle[]

Adding the awaitility Dependency

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[]

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[]

Create a Scheduled Task

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[]

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.

Enable Scheduling

Although scheduled tasks can be embedded in web applications and WAR files, the simpler approach (shown 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[]

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

Summary

Congratulations! You created an application with a scheduled task. Also, this technique works in any type of application.

See Also

The following guides may also be helpful: