/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

To initialize the project:

  1. Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you.

  2. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.

  3. Click Generate.

  4. Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.

Note
If your IDE has the Spring Initializr integration, you can complete this process from your IDE.
Note
You can also fork the project from Github and open it in your IDE or other editor.

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

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

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

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: