项目中需要用到定时任务,用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
完成!