标签:测试 factory int ble str 介绍 class pre imp
转载请标明出处:
http://blog.csdn.net/forezp/article/details/71023783
本文出自方志朋的博客
这篇文章将介绍怎么通过spring去做调度任务。
创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务。
@SpringBootApplication
@EnableScheduling
public class SpringbootSchedulingTasksApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSchedulingTasksApplication.class, args);
}
}
创建一个定时任务,每过5s在控制台打印当前时间。
@Component
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
}
通过在方法上加@Scheduled注解,表明该方法是一个调度任务。
启动springboot工程,控制台没过5s就打印出了当前的时间。
2017-04-29 17:39:37.672 INFO 677 — [pool-1-thread-1] com.forezp.task.ScheduledTasks : The time is now 17:39:37
2017-04-29 17:39:42.671 INFO 677 — [pool-1-thread-1] com.forezp.task.ScheduledTasks : The time is now 17:39:42
2017-04-29 17:39:47.672 INFO 677 — [pool-1-thread-1] com.forezp.task.ScheduledTasks : The time is now 17:39:47
2017-04-29 17:39:52.675 INFO 677 — [pool-1-thread-1] com.forezp.task.ScheduledTasks : The time is now 17:39:52
在springboot创建定时任务比较简单,只需2步:
https://spring.io/guides/gs/scheduling-tasks/
https://github.com/forezp/SpringBootLearning
SpringBoot非官方教程 | 第十八篇: 定时任务(Scheduling Tasks)
标签:测试 factory int ble str 介绍 class pre imp
原文地址:http://blog.csdn.net/forezp/article/details/71023783