这几天做了一个谷歌验证码的小项目,由于验证码是每30秒刷新的。一开始采用的是前端发送刷新请求,获取时间戳后倒计时,但是发现会出现bug,由于谷歌那边是每0s和每30s准时刷新,但是我的是根据用户访问网页时候才发送刷新请求,就会导致会有时间差。于是又改成用@Schedule来定时刷新,前端只负责获取到验证码和时间戳,不再对系统进行操纵。
下面来说说如何使用@Schedule
了解一个新玩意最好的方法是官方文档!
https://spring.io/guides/gs/scheduling-tasks/
1.@EnableScheduling
该@EnableScheduling注释确保后台任务执行被创建。没有它,什么都无法安排。
package com.example.schedulingtasks;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SchedulingTasksApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulingTasksApplication.class);
}
}
2.在方法上添加@Schedule 当然了不要忘记把类注入到Sring中。
@Component
public class ScheduledTasks {
@Scheduled(cron = "0,30 * * * * ?")
public void refreshAll() {
LocalDateTime localDateTime = LocalDateTime.now().plusSeconds(EXPIRE_TIME);
System.out.println(localDateTime);
}
}
3.点进@Schedule标签中。
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
String CRON_DISABLED = "-";
String cron() default "";
String zone() default "";
long fixedDelay() default -1L;
String fixedDelayString() default "";
long fixedRate() default -1L;
String fixedRateString() default "";
long initialDelay() default -1L;
String initialDelayString() default "";
}
1.Cron
cron是表达式,可以丰富的指定确切的时间段执行代码。
1. 结构
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:
1.Seconds Minutes Hours DayofMonth Month DayofWeek Year
2.Seconds Minutes Hours DayofMonth Month DayofWeek
2. 各字段的含义
字段 | 允许值 | 允许的特殊字符 |
秒(Seconds) | 0~59的整数 | , - * / 四个字符 |
分(Minutes) | 0~59的整数 | , - * / 四个字符 |
小时(Hours) | 0~23的整数 | , - * / 四个字符 |
日期(DayofMonth) | 1~31的整数(但是你需要考虑你月的天数) | ,- * ? / L W C 八个字符 |
月份(Month) | 1~12的整数或者 JAN-DEC | , - * / 四个字符 |
星期(DayofWeek) | 1~7的整数或者 SUN-SAT (1=SUN) | , - * ? / L C # 八个字符 |
年(可选,留空)(Year) | 1970~2099 | , - * / 四个字符 |
*
(1)* :表示匹配该域的任意值。假如在Minutes域使用*, 即表示每分钟都会触发事件。
(2)? :只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。
(3)- :表示范围。例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次
(4)/ :表示起始时间开始触发,然后每隔固定时间触发一次。例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次.
(5), :表示列出枚举值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。
(6)L :表示最后,只能出现在DayofWeek和DayofMonth域。如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。
(7)W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份 。
(8)LW :这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
(9)# :用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。
// 在任意年任意月任意日任意点任意分的0秒和30秒执行一次
@Scheduled(cron = "0,30 * * * * ? ")
public void scheduleTest1(){
System.out.println("cron表达式 =>"+LocalTime.now());
}
2. zone
zone指定时区,默认使用服务器localzone。
3. fixedRate
fixedRate指定多久执行一次,单位:毫秒。
// 启动后每10秒执行一次
@Scheduled(fixedRate = 10000)
public void scheduleTest2(){
System.out.println(LocalDateTime.now());
}
4. fixedDelay
fixedDelay指定在指定的时间执行一次,单位:毫秒
//在创建后延迟5秒后执行一次,然后每个五秒执行一次
@Scheduled(fixedDelay = 5000)
public void scheduleTest3(){
System.out.println(LocalDateTime.now());
}
5. initialDelay
initialDelay指定延迟容器启动后执行,单位:毫秒
// initialDelay 延迟容器启动后执行,单位:毫秒
@Scheduled(fixedRate = 10000,initialDelay = 2000)
public void scheduleTest4(){
System.out.println("initialDelay =>"+ LocalTime.now());
}
有什么问题可以评论或者私信我,每日在线解(LIAO)疑(SAO)。
这里是大誌的博客,一位初生的卑微码农,觉得好用记得点赞!!!