Spring Boot Scheduler Example

Scheduling the tasks, which are to be executed at fixed time intervals is a common requirement in application development. A practical example is generating a daily user login report for a web application. Also, trigger an email to send the report to the business team at a particular time of the day. We can easily create a scheduler using the spring boot framework.

Spring framework provides a task scheduling feature, which we can use to run a task at regular intervals. Spring supports scheduling tasks by providing the @Scheduled annotation.

In this article, we will learn about spring scheduler and how to use it in our spring boot application development.

Table of Contents

Enabling the spring boot scheduler

To enable the scheduler feature in the spring boot application, we have to add the @EnableScheduling annotation in our spring boot application class.

By using this annotation, spring enables the detection of the @Scheduled annotation, which is used to define scheduled tasks.

package com.asb.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SpringBootSchedulerExampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootSchedulerExampleApplication.class, args);
    }
}

Using the @Scheduled annotation

Create a component java class with the name TaskRunner as given below. This class will also have a method(s), annotated with the @Scheduled annotation.

The @Scheduled annotation also accepts different configuration properties like fixedDelayString, cron, etc., which we can use to configure task scheduler configurations.

In the below example, the application executes the run() method every 10 seconds interval.

package com.asb.example;

import java.time.LocalDateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TaskRunner {
    @Scheduled(fixedDelayString = "10000") // Fixed delay of 10 secs
    public void run() {
        System.out.println(LocalDateTime.now() + " : Running Scheduled Task!!");
    }
}

We can also use the cron property to set the task scheduler to run at a particular time of the day as given below. Here, the application executes the scheduler every 10 seconds interval.

@Component
public class TaskRunner {
    @Scheduled(cron="*/10 * * * * *") //Run every 10 secs
    public void run() {
        System.out.println(LocalDateTime.now() + " : Running Scheduled Task!!");
    }
}

We can also set the time zone for the scheduler with the help of the zone configuration property as shown below.

@Scheduled(cron = "0 0 14 * * *" , zone = "GMT+5:00")
public void run() {
        // Scheduler Implementation..
    }

Testing the application

Let’s run our spring boot application to check our scheduler.

Finally, the below image shows that our scheduled task is running every 10 seconds interval.

Spring Boot scheduler example

Conclusion

In conclusion, the Spring framework supports scheduled tasks with the help of the @Scheduled annotation.

We can use this feature while developing spring applications to schedule tasks.

The sample code is available on GitHub.

Spring Boot Scheduler Example
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading