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 Framework’s @Scheduled
annotation.
The source code for this guide can be found in this repository. If you would like a local copy of the code you can download it there.
Although scheduled tasks can be embedded in web applications, the simpler approach (shown in this guide) creates a standalone application. To do so, package everything in a single, executable JAR file, driven by a Java main() method. The following snippet (from src/main/java/com/example/schedulingtasks/SchedulingTasksApplication.java
) shows the application class:
@SpringBootApplication
@EnableScheduling
public class SchedulingTasksApplication {
Spring Initializr adds the @SpringBootApplication
annotation to our main class. @SpringBootApplication
is a convenience annotation that adds all of the following:
-
@Configuration
: Tags the class as a source of bean definitions for the application context. -
@EnableAutoConfiguration
: Spring Boot attempts to automatically configure your Spring application based on the dependencies that you have added. -
@ComponentScan
: Tells Spring to look for other components, configurations, and services. If specific packages are not defined, recursive scanning begins with the package of the class that declares the annotation.
Additionally, add the @EnableScheduling
annotation. This annotation enables Spring’s scheduled task execution capability.
Create a new class src/main/java/com/example/schedulingtasks/ScheduledTasks.java
called:
@Component
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
}
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. Other options are cron() and fixedDelay() . For periodic tasks, exactly one of these three options must be specified, and optionally, initialDelay() . For a one-time task, it is sufficient to just specify an initialDelay()
|
You should now be able to run the application by executing the main method in SchedulingTasksApplication
. You can run the program from your IDE, or by executing the following Gradle command in the project root directory:
./gradlew bootRun
Doing so starts the application, and the method annotated with @Scheduled runs. You should see log messages similar to:
20yy-mm-ddT07:23:01.665-04:00 INFO 19633 --- [ scheduling-1] c.e.schedulingtasks.ScheduledTasks : The time is now 07:23:01 20yy-mm-ddT07:23:06.663-04:00 INFO 19633 --- [ scheduling-1] c.e.schedulingtasks.ScheduledTasks : The time is now 07:23:06 20yy-mm-ddT07:23:11.663-04:00 INFO 19633 --- [ scheduling-1] c.e.schedulingtasks.ScheduledTasks : The time is now 07:23:11
Note
|
This example uses fixedRate() scheduling, so the application runs indefinitely until you interrupt it manually.
|
To properly test your application, you can use the awaitility
library. Since Spring Boot 3.2, this is a dependency that Boot manages. You can create a new test or view the existing test at src/test/java/com/example/schedulingtasks/ScheduledTasksTest.java
:
@SpringBootTest
public class ScheduledTasksTest {
@SpyBean
ScheduledTasks tasks;
@Test
public void reportCurrentTime() {
await().atMost(Durations.TEN_SECONDS).untilAsserted(() -> {
verify(tasks, atLeast(2)).reportCurrentTime();
});
}
}
This test automatically runs when you run the ./gradlew clean build
task.
In this section, we will describe four different ways to run this guide:
Regardless of how you choose to execute the application, the output should be the same.
To run the application, you can package the application as an executable jar.
The command ./gradlew clean build
will compile the application to an executable jar.
You can then run the jar with the command java -jar build/libs/gs-scheduling-tasks-0.0.1-SNAPSHOT.jar
Alternatively, if you have a Docker environment available, you could create a Docker image directly from your Maven or Gradle plugin, using buildpacks.
With Cloud Native Buildpacks, you can create Docker compatible images that you can run anywhere.
Spring Boot includes buildpack support directly for both Maven and Gradle.
This means you can type a single command and quickly get a sensible image into a locally running Docker daemon.
To create a Docker image using Cloud Native Buildpacks, run the command ./gradlew bootBuildImage
.
With a Docker environment enabled, you can execute the application with the command docker run docker.io/library/gs-scheduling-tasks:0.0.1-SNAPSHOT
Spring Boot also supports compilation to a native image, provided you have a GraalVM distribution on your machine.
To create a native image with Gradle using Native Build Tools, first make sure that your Gradle build contains a plugins
block that includes org.graalvm.buildtools.native
.
plugins { id 'org.graalvm.buildtools.native' version '0.9.28' ...
You can run the command ./gradlew nativeCompile
to generate a native image. When the build completes, you will be able to run the code with a near instantaneous start up time by executing the command build/native/nativeCompile/gs-scheduling-tasks
.
You can also create a Native Image using Buildpacks. You can generate a native image by running the command ./gradlew bootBuildImage
. Once the build completes, you can start your application with the command docker run docker.io/library/gs-scheduling-tasks:0.0.1-SNAPSHOT
The following guides may also be helpful:
Want to write a new guide or contribute to an existing one? Check out our contribution guidelines.
Important
|
All guides are released with an ASLv2 license for the code, and an Attribution, NoDerivatives creative commons license for the writing. |