Lucky-gril
积少成多,每天进步一点点。

项目中需要用到定时任务,用spring boot实,demo如下:

1.启动程序增加@EnableScheduling注解,开启定时任务。

@SpringBootApplication
@EnableScheduling
public class MerchantCenterApplication {

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

2.写测试类

@Component
public class ScheduleTask {

  @Scheduled(cron = "0/5 * * * * ? ")
  public void printSay() {
    System.out.println("This is a say method!"+new Date());
  }
}

 

3.启动程序,控制台输出

This is a say method!Tue Jul 10 14:00:20 CST 2018
This is a say method!Tue Jul 10 14:00:25 CST 2018
This is a say method!Tue Jul 10 14:00:30 CST 2018
This is a say method!Tue Jul 10 14:00:35 CST 2018
This is a say method!Tue Jul 10 14:00:40 CST 2018
This is a say method!Tue Jul 10 14:00:45 CST 2018
This is a say method!Tue Jul 10 14:00:50 CST 2018

完成!

posted on 2018-07-10 14:02  Lucky-gril  阅读(194)  评论(0编辑  收藏  举报