Scheduling the tasks, which are to be executed on fixed time interval might be a requirement which may comes up during any application development.

A practical example is generating a daily user login report for a web application and triggering an email to send it to business team on a particular time of the day.

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

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

Enabling the spring scheduler

To enable scheduler feature in spring boot application, we have to add @EnableScheduling annotation in our spring boot application class. By using this annotation, spring enables detection of @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 @Scheduled annotation

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

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

Here, in below example, run() method is called on 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 cron property to set the task scheduler to run on a particular time of the day as given below. Here, scheduler is executed every on 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 time zone for the scheduler with the help of zone configuration property as shown below.

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

It’s show time!! ๐Ÿ™‚ ๐Ÿ™‚ Let’s run our spring boot application to check our scheduler. ๐Ÿ™‚

Output

Below image shows that our scheduled task is running in every 10 seconds interval.

Spring Boot scheduler example

Conclusion

Spring framework supports scheduled tasks with the help of @Scheduled annotation. We can use this feature while developing spring applications to schedule tasks.

Sample code is available on GitHub. Happy coding.. ๐Ÿ™‚ ๐Ÿ™‚

You may also interested in