How to use Schedule

1.1 Start classes with annotations

Add an annotation @enablesCheduling to the startup class

@SpringBootApplication
@EnableScheduling
public class Application {
  public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code

1.2 Add an annotation @scheduled to the method

Add an annotation @scheduled to the methods that need to implement Scheduled tasks.

@Component
public class MySchedule {
  
  @Scheduled(cron = "0/10 * * * * *")
  public void test(a) throws InterruptedException {
    log.info(Thread.currentThread().getName()+"---test");
    TimeUnit.SECONDS.sleep(12); }}Copy the code

Three kinds of task schedulers

Schedule has three scheduling modes: fixedRate, fixedDelay, and Cron expression.

2.1 fixedRate

Fixed frequency task.

This attribute indicates the delay of invoking again after the last invocation (without waiting for the completion of the last invocation), which may cause the problem of repeated execution of tasks. Therefore, it is not recommended to use this attribute, but it can be used if the amount of data is not large and can be executed within the configured interval.

Note: When the method execution time exceeds the task scheduling frequency, the scheduler will execute the next task as soon as the current method execution completes.

@Scheduled(fixedRate = 1000 * 10)
public void work(a) {
  // do your work here
  
}
Copy the code

The unit of fixedRate is millisecond. In the preceding example, fixedRate = 1000 x 10 indicates that the fixedRate is executed every 10 seconds.

Set the start time of the first execution to 0, the second execution to 10 seconds, and the third execution to 20 seconds…… And so on.

However, if the task is not completed within 10 seconds, for example, the first task takes 15 seconds to complete, the second execution will take 15 seconds and will be executed immediately.

2.2 fixedDelay

Fixed interval tasks.

The next task execution time is calculated from the end time of the last task execution of the method. Start executing tasks periodically with this rule.

@Scheduled(fixedDelay = 1000 * 10)
public void work(a) {
  // do your work here
  
}
Copy the code

If the preceding example is executed every 10 seconds, set the start time of the first task to 0. If the execution time of the next task is 5 seconds, the start time of the next task is 5 + 10 = 15 seconds. The next task starts at 15 + 5 + 10 = 30 seconds, and so on.

2.3 Cron expression

The Cron expression consists of six or seven space-separated time fields, as shown below:

* The first digit is second, 0-59 * the second digit is minute, 0-59 * the third digit is hour, 0-23 * the fourth digit, date, 1-31 * the fifth digit, month, 1-12 * the sixth digit, day of the week, 1-7 * the seventh digit, year, can be left blank. The value ranges from 1970 to 2099 (*). The asterisk (*) indicates "every". The question mark: Can only appear in the date and the week, indicating that the value of this position is uncertain (-) to express a range, such as in the hour field 10-12, from 10 to 12 (,) comma, to express a list value, such as in the week field 1,2,4, For example, x/y is the start value and y is the step size. If 0/15 is used in the first (second), the value starts from 0 seconds and every 15 seconds. 0 5 3 * * every day at 3:00? 0, 5, 3 every day at 3:05? * * Execute 0 5/10 3 * * every day at 3:05? Every day at 3:05, 15, 25, 35, 45, 55 you do 0, 10, 3? * 1 Run on Sunday at 3:10 every week. Note: 1 indicates Sunday 0 10 3? * 1#3 is executed on the third Sunday of each month. # can only appear in the position of the weekNote: the number in the sixth place (day of the week) may not be expressed correctly and can be abbreviated as: SunCopy the code

Note that when method execution time exceeds the task scheduling frequency, the scheduler executes the next cycle.

The following example executes every 10 seconds, but the task execution time is 12 seconds.

If the start time of the first task is 0, the first task should be completed in 12 seconds. The second task should be executed at the 10th second. However, since the first task is not finished at the 10th second, the node in the 10th second is skipped and the second task is executed at the 20th second.

@Scheduled(cron = "0/10 * * * * *")
public void test(a) throws InterruptedException {
  log.info(Thread.currentThread().getName()+"---test");
  TimeUnit.SECONDS.sleep(12);
}
Copy the code

Configure the TaskScheduler thread pool

Spring creates a single thread pool by default, and if there are multiple scheduled tasks to execute in the system, the task scheduler will have time drifts and the task execution time will be uncertain.

So let’s customize a TaskScheduler thread pool.

@Configuration
public class InitBeanConfig {

  /** * Configure the Schedule thread pool. */
  @Bean
  public TaskScheduler taskScheduler(a) {
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(10);
    taskScheduler.setThreadNamePrefix("springboot-task");
    returntaskScheduler; }}Copy the code

When multiple tasks need to be executed at the same time, up to 10 threads can be opened to handle them.

Reference article:

Segmentfault.com/a/119000001…

www.cnblogs.com/skychenjiaj…


The above is all content of SpringBoot integration Schedule. We will continue to update it if we encounter related problems in the future development. Welcome to pay attention.