一、试验代码
1 //@Scheduled(fixedRate = 5000) 2 //@Scheduled(fixedDelay = 5000) 3 @Scheduled(cron ="*/5 * * * * *") 4 publicvoid reportCurrentTime()throwsInterruptedException{ 5 log.info("action bgn {}----", dateFormat.format(newDate())); 6 Thread.sleep(1500); 7 log.info("action end {}", dateFormat.format(newDate())); 8 }
二、cron参数的配置
该参数非常灵活,这个参数的大致理解如下:
he pattern is a list of six single space-separated fields: representing second, minute, hour, day, month, weekday. Month and weekday names can be given as the first three letters of the English names.
Example patterns:
"0 0 * * * *" = the top of every hour of every day.
"*/10 * * * * *" = every ten seconds.
"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
"0 * 6,19 * * *" = 6:00 AM and 7:00 PM every day.
"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.
"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
"0 0 0 25 12 ?" = every Christmas Day at midnight 有了这种配置之后,我就在想,我们以前做自动备份、自动导出的配置时,为什么要自己写这种逻辑,然后又要自己去定义配置格式呢,这是不是就叫做没文化真可怕
三、三种参数的试验数据如下。
fixedDelay=5000
2017-01-18 13:37:55.011 : action bgn 13:37:55----
2017-01-18 13:37:56.512 : action end 13:37:56
2017-01-18 13:38:01.513 : action bgn 13:38:01----
2017-01-18 13:38:03.013 : action end 13:38:03
2017-01-18 13:38:08.015 : action bgn 13:38:08----
2017-01-18 13:38:09.515 : action end 13:38:09
2017-01-18 13:38:14.517 : action bgn 13:38:14----
2017-01-18 13:38:16.018 : action end 13:38:16
fixedRate=5000
2017-01-18 13:39:23.643 : action bgn 13:39:23----
2017-01-18 13:39:25.144 : action end 13:39:25
2017-01-18 13:39:28.644 : action bgn 13:39:28----
2017-01-18 13:39:30.145 : action end 13:39:30
2017-01-18 13:39:33.643 : action bgn 13:39:33----
2017-01-18 13:39:35.144 : action end 13:39:35
2017-01-18 13:39:38.643 : action bgn 13:39:38----
2017-01-18 13:39:40.143 : action end 13:39:40
2017-01-18 13:39:43.643 : action bgn 13:39:43----
cron = "*/5 * * * * *"
2017-01-18 13:43:25.002 : action bgn 13:43:25----
2017-01-18 13:43:26.510 : action end 13:43:26
2017-01-18 13:43:30.001 : action bgn 13:43:30----
2017-01-18 13:43:31.502 : action end 13:43:31
2017-01-18 13:43:35.001 : action bgn 13:43:35----
2017-01-18 13:43:36.502 : action end 13:43:36
2017-01-18 13:43:40.001 : action bgn 13:43:40----
2017-01-18 13:43:41.502 : action end 13:43:41
2017-01-18 13:43:45.001 : action bgn 13:43:45----
2017-01-18 13:43:46.502 : action end 13:43:46