Spring boot starts a simple scheduled task

Spring boot comes with its own scheduled task Schedule. It can meet the requirements of simple scheduled tasks.

1 Start the timer through the entry that comes with Spring boot

This way of starting the timer is also the most commonly used way.

Add an annotation to the startup class: @EnableScheduling  ,

@EnableScheduling
@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

2 Configure on the method of timed tasks

Annotate the method  schedule  : @Scheduled

Add cron expressions to annotations.

@Component
public class EventFactorySchedule {

    //每分钟的第10s启动
    @Scheduled(cron = "10 0/1 * * * ?")
    public void schedule() {
    }
    

Above, complete   the timing task configuration of the spring boot project.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324910604&siteId=291194637