一 任务调度基本介绍 任务调度器就是按照规定的计划完成任务;比如windows,linux的自带的任务调度系统功能;平常开发中也就是按照规定的时间点轮询执行计划任务(比如每周三的凌晨
一 任务调度基本介绍
任务调度器就是按照规定的计划完成任务;比如windows,linux的自带的任务调度系统功能;平常开发中也就是按照规定的时间点轮询执行计划任务(比如每周三的凌晨进行数据备份),或者按时间隔触发一次任务调度(比如每3小时执行一次定时抓拍);
二 corn表达式介绍
2.1 位数介绍
如果有用过quartz的读者肯定了解cron时钟周期计划;下面是cron对应位数的说明,其中第七位年份通常忽略,第四位跟第六位同时表达会有歧义,通常只表达具体的一位,另一位使用?表示解决冲突;
位数
说明
2.2 占位符说明
占位符
说明
2.3 常用cron举例
corn
说明
三使用cron进行任务调度
3.1 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.2 具体corn任务调度计划
在@Scheduled注解中自定义cron调度计划;将注解用在需要进行调度的方法上
/**
* @Author lsc
* @Description <p> </p>
* @Date 2019/11/11 22:23
*/
@Service
public class PlainService {
@Scheduled(cron = "30 * * * * ?")
public void cronScheduled(){
System.out.println("关注作者博客和公众号,不定期分享原创文章");
}
}
3.3 启动类
启动类需要加上 @EnableScheduling 表示开启任务调度;
/**
* @Author lsc
* @Description <p> 任务调度启动类 </p>
* @Date 2019/11/11 22:20
*/
@SpringBootApplication
// 开启任务调度
@EnableScheduling
public class ScheduledApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduledApplication.class,args);
}
}
四 Scheduled 中其它方式进行任务调度
4.1 fixedDelay
每隔3000毫秒执行一次,必须是上次调度成功后3000毫秒;
@Scheduled(fixedDelay = 3000)
public void fixedDelayScheduled(){
System.out.println("the day nice");
}
4.2fixedRate
每个3000毫秒执行一次,无论上次是否会执行成功,下次都会执行;
@Scheduled(fixedRate = 3000)
public void fixedRateScheduled(){
System.out.println("the night nice");
}
4.3 initialDelay
initialDelay 表示初始化延迟1000毫秒后,执行具体的任务调度,之后按照fixedRate进行任务调度;
@Scheduled(initialDelay = 1000,fixedRate = 3000)
public void initialDelayStringScheduled(){
System.out.println("the night nice");
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
