spring boot启动简单的定时任务

spring boot 自带有定时任务 Schedule。可以满足简单的定时任务要求。

1 通过Spring boot 自带的入口开开启定时器

这种开启定时器的方式,也是最常用的方式。

在启动类上添加注解:@EnableScheduling ,

@EnableScheduling
@SpringBootApplication
public class App {

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

2 在定时任务的方法上进行配置

在方法 schedule 上加上注解:@Scheduled

注解中加入 cron 表达式。

@Component
public class EventFactorySchedule {

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

以上,完成 spring boot 项目的定时任务配置。

猜你喜欢

转载自my.oschina.net/pding/blog/1801822