任务调度框架quartz

核心概念

  1. Scheduler: 运行容器

  2. Job: 任务

  3. Trigger: 触发器

  4. JobDetail: 任务实例

  5. ThreadPool: Scheduler进行任务调度的运行资源

依赖

附1: SpringBoot基础调度控制器

  1. 启动添加注解: @EnableScheduling

  2. 在需要调度的方法前加注解: @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线程池

  1. 编写配置类(如下)

  2. 在需要异步调用的方法前加注解@Async

  1. @Configuration
  2. @EnableAsync
  3. public class ThreadPoolConfig {
  4. @Bean
  5. public Executor commonExecutor() {
  6. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  7. // 核心线程数
  8. executor.setCorePoolSize(5);
  9. // 最大线程数
  10. executor.setMaxPoolSize(10);
  11. // 队列容量
  12. executor.setQueueCapacity(20);
  13. // 线程活跃时间(秒)
  14. executor.setKeepAliveSeconds(60);
  15. // 默认线程名称
  16. executor.setThreadNamePrefix("common-");
  17. // 设置拒绝策略: 直接拒绝
  18. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
  19. return executor;
  20. }
  21. }