我们应用@Scheduled来创建准时义务 这个注解用来标注一个准时义务办法。经由过程看@Scheduled源码可以看出它支撑多种参数:
(1)cron:cron表达式,指定义务在特准时光履行;
(2)fixedDelay:表示上一次义务履行完成后多久再次履行,参数类型为long,单位ms;
(4)fixedRate:表示按必定的频率履行义务,参数类型为long,单位ms;
(5)fixedRateString: 与fixedRate的含义一样,只是将参数类型变为String;
(6)initialDelay:表示延迟多久再第一次履行义务,参数类型为long,单位ms;
(7)initialDelayString:与initialDelay的含义一样,只是将参数类型变为String;
? : 只能用在每月第几天和礼拜两个域。表示不指定值,当2个子表达式个中之一被指定了值今后,为了避免冲突,须要将另一个子表达式的值设为“?”;
2.开启准时义务
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.scheduling.annotation.EnableScheduling;
- /**
- * Description: 启动类
- * Designer: jack
- * Date: 2017/8/10
- * Version: 1.0.0
- */
- @SpringBootApplication
- @EnableScheduling
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
留意这里的 @EnableScheduling 注解,它的感化是发明注解@Scheduled的义务并由后台履行。没有它的话将无法履行准时义务。
引用官方文档袈洵文:
@EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.
3. 履行结不雅 (单线程)
到此我们就完成了一个简单的准时义务模型,下面履行springBoot不雅察履行结不雅。
- 2017-08-11 12:06:19.738 INFO 52252 --- [pool-1-thread-1] com.test.ScheduledTasks : ===initialDelay: 第1次履行办法
- 2017-08-11 12:06:23.739 INFO 52252 --- [pool-1-thread-1] com.test.ScheduledTasks : ===fixedRate: 第2次履行办法
- 2017-08-11 12:06:23.739 INFO 52252 --- [pool-1-thread-1] com.test.ScheduledTasks : ===fixedDelay: 第2次履行办法
- 2017-08-11 12:06:24.738 INFO 52252 --- [pool-1-thread-1] com.test.ScheduledTasks : ===initialDelay: 第2次履行办法
- 2017-08-11 12:06:28.739 INFO 52252 --- [pool-1-thread-1] com.test.ScheduledTasks : ===fixedRate: 第3次履行办法
- 2017-08-11 12:06:28.740 INFO 52252 --- [pool-1-thread-1] com.test.ScheduledTasks : ===fixedDelay: 第3次履行办法
推荐阅读
【51CTO晃荡】8.26 带你深度懂得清华大年夜学、搜狗基于算法的IT运维实践与摸索 本文起首介绍非抢占式内核(Non-Preemptive Kernel)和可抢占式内核(Preemptive Kernel)的差别。接着分析L>>>详细阅读
地址:http://www.17bianji.com/lsqh/36757.html
1/2 1