SoFunction
Updated on 2025-03-06

SpringBoot integrates timing tasks

1. Scheduled principle

The Executor architecture in JUC package brings the separation of thread creation and execution. ExecutorService, the successor of Executor, has two important implementation classes derived below, they are
1. ThreadPoolExecutor thread pool
2. ScheduledThreadPoolExecutor supports thread pools for periodic tasks
ThreadPoolExecutor can implement various custom thread pools, while the ScheduledThreadPoolExecutor class adds the function of periodic execution of tasks on the basis of custom thread pools.

2. Import dependencies

After springboot3, we have a timer tasker, so you only need to refer to the springboot launcher.

<parent>
    <groupId></groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.5</version>
</parent>

3. Write scheduled tasks

The class where the timing task is located needs to be a component of spring, so the @Component annotation needs to be added to the timing task class, and the @Scheduled annotation needs to be added to the timing task method, declaring that this is a timing task. (It should be noted here that the cron expression in the @Scheduled annotation will be introduced in detail later)

@Component
public class MySchduled {
    private int count = 0;
    @Scheduled(cron = "0/3 * * * * ? ")
    public void print(){
        ("hello:" + count++);
    }
}

4. Cron expression

The Cron expression  is a string used to specify the execution time of a timed task. It consists of six or seven fields, representing seconds, minute, time, date, month, week, and year (optional). Each field can be a specific value, a range, an incremental step, or a special character is used. 12

  • Asterisk (*): When used in a field, it means that the field can take all possible values ​​of the field. For example, use * in the Minutes field to represent every minute.
  • Question mark (?): Used only in the Date and Week fields, usually as meaningless values, equivalent to placeholders.
  • Minus sign (-): represents a range. For example, in the hour field, use "10-12" to represent from 10 to 12 o'clock, that is, 10, 11, and 12 o'clock.
  • Comma (,): represents a list value, for example, in the week field, use "MON,WED,FRI" to represent Monday, Wednesday and Friday.
  • Slash (/): represents an equal-step sequence. For example, in the minute field, 0/15 means starting from 0 seconds and triggering every 15 seconds.
  • L: Used only in the Date and Week fields, representing "Last", in the Date field, it represents the last day of the month, and in the Week field, it represents Saturday.
  • W: It can only appear in the date field, which is a modification of the leading date, indicating the working day closest to the date.

For example, the (0/3 * * * * ?) we used above means that it is executed every 3 seconds

There are many online generators of cron expressions on the Internet, which can be generated online and are more convenient to use.

V. Start

Starting a timed task scanning is a very important step. If the timed task is not started, the timed task will not be executed. Starting a timed task requires adding the @EnableScheduling annotation to the startup class or configuration class, indicating that the timed task scanning is started.

@SpringBootApplication
@MapperScan("")
@EnableScheduling
@EnableAspectJAutoProxy
public class AuthApplication {
    public static void main(String[] args) {
        (,args);
    }
}

This is the end of this article about SpringBoot integrated timing tasks. For more related SpringBoot timing tasks, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!