Spring Boot 2 - Scheduling Tasks

This post walks you through the steps for scheduling tasks with Spring Boot 2. Spring boot provides a @Scheduled annotation to schedule tasks. The @Scheduled annotation is added to a method along with some information about when to execute it, and Spring Boot takes care of the rest.
If you have been already working on Spring or Spring Boot Application and you have a requirement to schedule a task based on some interval, then these below two quick steps will help to set up.
First, we will enable scheduling simply by adding the @EnableScheduling annotation to the main application class or one of the Configuration classes.
Scheduling a task with Spring Boot is as simple as annotating a method with @Scheduled annotation, and providing few parameters that will be used to decide the time at which the task will run.

Let's develop a complete example to demonstrates how to schedule tasks using Spring Boot.

What we’ll build

We’ll build an application that prints out the current time every five seconds using Spring’s @Scheduled annotation. We will also look into useful attributes of @Scheduled annotation.

Tools and Technologies Used

  • Spring Boot - 2.0.5.RELEASE
  • JDK - 1.8 or later
  • Spring Framework - 5.0.9 RELEASE
  • Maven - 3.2+
  • IDE - Eclipse or Spring Tool Suite (STS)

Create Spring boot Project

There are many ways to create a Spring Boot application. The simplest way is to use Spring Initializr at http://start.spring.io/, which is an online Spring Boot application generator.
Look at the above diagram, we have specified the following details:
  • Generate: Maven Project
  • Java Version: 1.8 (Default)
  • Spring Boot:2.0.4
  • Group: net.javaguides.springboot2
  • Artifact: springboot2-schedule-tasks
  • Name: springboot2-schedule-tasks
  • Description: Schedule tasks using Spring Boot
  • Package Name : net.javaguides.springboot2
  • Packaging: jar (This is the default value)
  • Dependencies: Web
Once, all the details are entered, click on Generate Project button will generate a spring boot project and downloads it. Next, Unzip the downloaded zip file and import it into your favorite IDE.

The pom.xml File

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.guides.springboot2</groupId>
    <artifactId>springboot2-schedule-tasks</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot2-schedule-tasks</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
       </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
The Spring Boot Maven plugin provides many convenient features:
  • It collects all the jars on the classpath and builds a single, runnable "├╝ber-jar", which makes it more convenient to execute and transport your service.
  • It searches for the public static void main() method to flag as a runnable class.
  • It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.

Create a scheduled task

Now that we’ve set up our simple spring boot project, we can create a scheduled task. In this example, reportCurrentTime() method is invoked every five seconds (measured between the successive start times of each invocation):
package net.guides.springboot2.springboot2scheduletasks;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        LOGGER.info("Fixed Rate Task :: Execution Time - {}", dateFormat.format(LocalDateTime.now()));
    }
}
The @Scheduled annotation has the following useful attributes:

fixedDelay

Execute the annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next.
@Scheduled(fixedDelay=5000)
public void doSomething() {
    // something that should execute periodically
}

fixedDelayString

Execute the annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next.
@Scheduled(fixedDelayString = "${fixed.delay}")
public void doSomething() {
 // do something
}

initialDelay

For fixed-delay and fixed-rate tasks, we can specify an initial delay by indicating the number of milliseconds to wait before the first execution of the method, as the following fixedRate example shows:
@Scheduled(initialDelay=1000, fixedRate=5000)
public void doSomething() {
    // something that should execute periodically
}

cron

If simple periodic scheduling is not expressive enough, we can provide a cron expression. For example, the following executes only on weekdays:
@Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
    // something that should execute on weekdays only
}

Enable Scheduling

We can enable scheduling simply by adding the @EnableScheduling annotation to the main application class or one of the Configuration classes.
Open SpringbootScheduleTasksApplication.java and add @EnableScheduling annotation like so -
package net.guides.springboot2.springboot2scheduletasks;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SpringbootScheduleTasksApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootScheduleTasksApplication.class, args);
    }
}
@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 tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
  • Normally we would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
  • @ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.
The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.
@EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.

Running Application

Two ways we can start the standalone Spring boot application.
  • We are using maven so just run the application using ./mvnw spring-boot:run. Or you can build the JAR file with ./mvnw clean package. Then you can run the JAR file:
java -jar target/springboot2-schedule-tasks.jar
  • From your IDE, run the SpringbootScheduleTasksApplication.main() method as a standalone Java class.

Output

The following diagram shows, tasks are executed every five seconds:
Learn Spring Boot on Spring Boot 2 Tutorial

Comments