美文网首页
SpringBoot定时任务学习

SpringBoot定时任务学习

作者: 微雨花间_b690 | 来源:发表于2018-10-07 14:23 被阅读0次

日常开发中需要制定一些定时任务,在使用spring boot就很好解决这个问题,现在pom文件里面配置好依赖项

1.pom包配置

org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-devtoolstrue

初始化之后,我们在spring boot的入口类Application.java中,允许支持schedule

@SpringBootApplication@EnableSchedulingpublicclassApplication{publicstaticvoidmain(String[] args){        SpringApplication.run(Application.class, args);    }}

定时任务1

@ComponentpublicclassSchedulerTask{privateintcount=0;@Scheduled(cron="*/6 * * * * ?")privatevoidprocess(){        System.out.println("this is scheduler task runing  "+(count++));    } }

定时任务2

@ComponentpublicclassScheduler2Task{privatestaticfinalSimpleDateFormat dateFormat =newSimpleDateFormat("HH:mm:ss");@Scheduled(fixedRate =6000)publicvoidreportCurrentTime(){        System.out.println("现在时间:"+ dateFormat.format(newDate()));    } }

结果如下:

this is scheduler task runing  0

现在时间:09:44:17

this is scheduler task runing  1

现在时间:09:44:23

this is scheduler task runing  2

现在时间:09:44:29

this is scheduler task runing  3

现在时间:09:44:35

参数说明:

@Scheduled 参数可以接受两种定时的设置,一种是我们常用的cron=”*/6 * * * * ?”,一种是 fixedRate = 6000,两种都表示每隔六秒打印一下内容。

fixedRate 说明

@Scheduled(fixedRate = 6000) :上一次开始执行时间点之后6秒再执行;

@Scheduled(fixedDelay = 6000) :上一次执行完毕时间点之后6秒再执行;

@Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次.

而还有一类定时任务,比如是每天的3点15分执行,那么我们就需要用另外一种方式:cron表达式

cron一共有7位,但是最后一位是年,可以留空,所以我们可以写6位:

* 第一位,表示秒,取值0-59

* 第二位,表示分,取值0-59

* 第三位,表示小时,取值0-23

* 第四位,日期天/日,取值1-31

* 第五位,日期月份,取值1-12

* 第六位,星期,取值1-7,星期一,星期二...,注:不是第1周,第二周的意思

          另外:1表示星期天,2表示星期一。

* 第7为,年份,可以留空,取值1970-2099

cron中,还有一些特殊的符号,含义如下:

(*)星号:可以理解为每的意思,每秒,每分,每天,每月,每年...

(?)问号:问号只能出现在日期和星期这两个位置,表示这个位置的值不确定,每天3点执行,所以第六位星期的位置,我们是不需要关注的,就是不确定的值。同时:日期和星期是两个相互排斥的元素,通过问号来表明不指定值。比如,1月10日,比如是星期1,如果在星期的位置是另指定星期二,就前后冲突矛盾了。

(-)减号:表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12

(,)逗号:表达一个列表值,如在星期字段中使用“1,2,4”,则表示星期一,星期二,星期四

(/)斜杠:如:x/y,x是开始值,y是步长,比如在第一位

(秒)0/15就是,从0秒开始,每15秒,最后就是0,15,30,45,60

另:*/y,等同于0/y

来看几个例子

0/6 * *  * * ?    每隔6s执行

0 0 3 * * ?    每天3点执行

0 5 3 * * ?    每天3点5分执行

0 5 3 ? * *    每天3点5分执行,与上面作用相同

0 5/10 3 * * ?  每天3点的 5分,15分,25分,35分,45分,55分这几个时间点执行

0 10 3 ? * 1    每周星期天,3点10分 执行,注:1表示星期天   

0 10 3 ? * 1#3  每个月的第三个星期,星期天 执行,#号只能出现在星期的位置

相关文章

网友评论

      本文标题:SpringBoot定时任务学习

      本文链接:https://www.haomeiwen.com/subject/dzslaftx.html