Create Spring Boot scheduled task method based on annotations

There are three ways to perform scheduled tasks in spring boot. The first is the simplest one: based on annotations (@Scheduled) ; the second: based on interfaces (SchedulingConfigurer) ; the third: based on annotations to set multiple Thread scheduled tasks .

Annotation-based approach


First, open the idea and create a springboot project. There is no need to introduce any jars. Springboot comes with its own timing.

Then, mark the startup class with the @EnableScheduling annotation to indicate that there is a scheduled task in this class. Add the @Scheduled() annotation on the scheduled execution method.

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling//第1步:开启定时任务
public class ScheduleDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ScheduleDemoApplication.class, args);
    }
    @Scheduled(fixedRate = 1000*5)//第2步:设置需要定时执行的任务,每隔5秒执行一次
    public void doSomething() {
        System.out.println("hello spring 定时器");
    }
}

 Click Start and you will see the console output "hello" every 6 seconds.

Of course, scheduled tasks can also be placed in other classes. For example, create a class Test.

package com.example.test;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
/**
 * @Description
 * @ClassName Task1
 * @Author User
 * @date 2020.06.07 12:24
 */
@Component
public class Test {
 
    @Scheduled()
    public void sayWord(cron ="*/6 * * * * ?") {
        System.out.println("开启了定时任务");
    }
}

@Scheduled has three methods besides cron: fixedRate, fixedDelay, initialDelay

cron: expression can be customized to execute tasks, but the execution method is similar to fixedDelay, and it will also be calculated from the end time of the last method.

*Suggestion: It is more convenient to directly click on the online Cron expression generator to generate parameters*

fixedRate: The interval time between control method executions is calculated from the time when the last method is executed. If the last method execution is blocked, then the next time will be executed until the last execution is completed and after a given time interval.

fixedRate: is executed at a certain rate, which is calculated from the time when the last method was executed. If the last method was blocked, it will not be executed next time, but the cumulative number of times it should be executed during the blocking period, when When it is no longer blocked, execute all of these at once, and then continue execution at a fixed rate.

initialDelay: initialDelay = 10000 means that after the container is started, the timer will be executed again after a delay of 10 seconds.

Guess you like

Origin blog.csdn.net/lanlan112233/article/details/129424575