任务调度框架quartz
核心概念
Scheduler: 运行容器
Job: 任务
Trigger: 触发器
JobDetail: 任务实例
ThreadPool: Scheduler进行任务调度的运行资源
依赖
附1: SpringBoot基础调度控制器
启动添加注解: @EnableScheduling
在需要调度的方法前加注解: @Scheduled(cron = “30 ?”)
附2: CRON表达式
corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份
每日0点:0 0 0 ?
每周一0点:0 0 0 ? MON 或者 0 0 0 ? 2 (注:1=SUN,2=MON,3=TUE,4=WED,5=THU,6=FRI,7=SAT)
每月1日0点: 0 0 0 1 * ?
每年1月1日0点:0 0 0 1 1 ?或者 0 0 0 1 JAN ?(注:1-12月依次是JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC)
2046年8月1日0点:0 0 0 1 8 ? 2046
附3: SpringBoot线程池
编写配置类(如下)
在需要异步调用的方法前加注解@Async
@Configuration
@EnableAsync
public class ThreadPoolConfig {
@Bean
public Executor commonExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数
executor.setCorePoolSize(5);
// 最大线程数
executor.setMaxPoolSize(10);
// 队列容量
executor.setQueueCapacity(20);
// 线程活跃时间(秒)
executor.setKeepAliveSeconds(60);
// 默认线程名称
executor.setThreadNamePrefix("common-");
// 设置拒绝策略: 直接拒绝
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
return executor;
}
}