Spring Boot Integration with Spring Task Scheduler Quick Start Demo

HBLOG
3 min readMay 29, 2024

--

1. Introduction to Spring Task Scheduler

There are two concepts in Spring Scheduler: a task and a framework for running a task (TaskExecutor/TaskScheduler). TaskExecutor, as the name suggests, is an executor of tasks that allows us to execute multiple tasks asynchronously. TaskScheduler is a task scheduler that runs future scheduled tasks. The most commonly used trigger is CronTrigger, which is described in detail in the following sections. Spring has built-in multiple types of TaskExecutor and TaskScheduler, which are convenient for users to choose according to different business scenarios.

Enable scheduled tasks

Spring Boot is used by default without any third-party dependencies spring-context Timed task tools available under modules Spring Task。 We just need to use @EnableScheduling annotation can enable the relevant timed task function. As:

2. Various timer implementations

cron timer

Annotation method

/**
* ┌───────────── second (0-59)
* │ ┌───────────── minute (0 - 59)
* │ │ ┌───────────── hour (0 - 23)
* │ │ │ ┌───────────── day of the month (1 - 31)
* │ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
* │ │ │ │ │ ┌───────────── day of the week (0 - 7)
* │ │ │ │ │ │ (0 or 7 is Sunday, or MON-SUN)
* │ │ │ │ │ │
* * * * * * *
*
*/
@Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
log.info("cron ....");
}

Code method

public void scheduleCronTrigger() {
CronTrigger cronTrigger = new CronTrigger("10 * * * * ?");
taskScheduler.schedule(new RunnableTask("Cron Trigger"), cronTrigger);
}

scheduleWithFixedDelay

annotation implementation

@Scheduled(initialDelay = 5000, fixedRate = 5000)
public void performTask1() {
log.info("Task performed at {}", LocalDateTime.now());
}

Code implementation

public void scheduleWithFixedDelay() {
taskScheduler.scheduleWithFixedDelay(new RunnableTask("Fixed 1 second Delay"), 1000);
}

scheduleAtFixedRate

Annotation method

@Scheduled(fixedRate = 5000)
public void performTask() {
log.info("Task performed at {}", LocalDateTime.now());
}

Code implementation

public void scheduleAtFixedRate() {
taskScheduler.scheduleAtFixedRate(new RunnableTask("Fixed Rate of 2 seconds") , 2000);
}

Configure the timer thread pool

package com.et.scheduler.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@Configuration
public class ThreadPoolTaskSchedulerConfig {
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
ThreadPoolTaskScheduler threadPoolTaskScheduler
= new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(5);
threadPoolTaskScheduler.setThreadNamePrefix(
"ThreadPoolTaskScheduler");
return threadPoolTaskScheduler;
}
}

The above are just some of the key codes, all of which can be found in the repositories below

Code repositories

3. Limitations

Running at the same time

For the same task, if the previous one has not finished running, the next one will not be triggered, which is no problem. But it doesn’t make sense that different tasks can’t be run at the same time. However, it is actually due to the fact that the default number of threads of the scheduler is 1.

Distributed is not supported

The scheduler runs on multiple instances at the same time. In this case, you can choose a distributed scheduled task framework, such as xxl-job, quartz, etc

4. Summary

TaskScheduler is a very useful interface in the Spring framework, which can help us implement scheduled tasks, periodic tasks, and asynchronous tasks. Through the introduction and examples in this article, I believe that readers have a deeper understanding of how to use TaskScheduler. In the actual project, we can select the appropriate TaskScheduler implementation class according to specific needs, and configure and use TaskScheduler through annotations to better manage and execute tasks.

--

--