spring boot quartz 实现定时任务
wc3 教程:https://www.w3cschool.cn/quartz_doc/quartz_doc-2put2clm.html
官网: http://www.quartz-scheduler.org/
一、Quartz API
- Scheduler 与调度程序交互的主要api
- Job 调度执行器的任务组件需要实现的接口
- JobDetail 用于定义作业的实例
- Trigger 触发器,定义指向给定作业的计划的组件
- JobBuilder 用于定义、构建JobDetail 实例,用于定义作业的实例
- TriggerBuilder 用于定义/构建触发器实例
Job 和 Trigger 的 key 必须唯一,即一个 Job 或 Trigger 的 key 由名称 name 和 分组 group 组成
二、定义 Job
可以使用 JobDataMap 传递一些数据
@Slf4j public class HelloWorldJob implements Job { String name; int age; @Override public void execute(JobExecutionContext context) throws JobExecutionException { log.info("Hello,World!"); log.info(name); log.info(age+""); } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } }
设置JobDetail 与 Trigger
@Configuration @ConditionalOnProperty(value = "quartz.enable",havingValue = "true") public class QuartzConfiguration { @Bean public JobDetail helloWorldJobDetail(){ JobDetail jobDetail = JobBuilder.newJob(HelloWorldJob.class) .withIdentity("myJob1","myJobGroup1") //JobDataMap可以给任务execute传递参数 .usingJobData("name","张三") .usingJobData("age",15) .storeDurably() .build(); return jobDetail; } @Bean public Trigger helloWorldTrigger(){ Trigger trigger = TriggerBuilder.newTrigger() .forJob(helloWorldJobDetail()) .withIdentity("mytrigger1","myJobGroup1") .usingJobData("job_trigger_param","job_trigger_param1") .startNow() //.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()) .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .build(); return trigger; } }
三、Trigger
Trigger 是 quartz 的触发器,会通知 Scheduler 何时执行Job
- SimpleTrigger
- CronTrigger 功能强大,是基于日历的作业调度
在线生成定时表达式:https://cron.qqe2.com/
四、实战
项目需要统计一些数据,每天凌晨定时统计,使用quartz
首先yml 配置如下
spring: quartz: properties: org: quartz: scheduler: instanceName: niu_quartz instanceId: AUTO jobStore: class: org.quartz.impl.jdbcjobstore.JobStoreTX driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate tablePrefix: QRTZ_ isClustered: true clusterCheckinInterval: 10000 useProperties: false threadPool: class: org.quartz.simpl.SimpleThreadPool threadCount: 10 threadPriority: 5 threadsInheritContextClassLoaderOfInitializingThread: true job-store-type: jdbc jdbc: initialize-schema: always quartz: enable: true
定义需要定时处理的逻辑,(QuartzServer 是自己定义的业务逻辑实现)
@Slf4j @Component @AllArgsConstructor public class AccountSubscribeJob implements Job { private final QuartzServer quartzServer; @Override public void execute(JobExecutionContext context) throws JobExecutionException { log.info("**********开始统计账户订阅信息**********"); //1.统计账户订阅人数 List<QuartzAccountVO> accountVOList = quartzServer.getAccountSubscribe(); quartzServer.insertAccountBatch(accountVOList); log.info("**********统计账户订阅信息结束**********"); } }
最后配置Quartz 核心的配置文件
@Configuration @ConditionalOnProperty(value = "quartz.enable",havingValue = "true") public class QuartzConfiguration { @Bean public JobDetail accountSubscribeDetail(){ JobDetail accountSubscribeJobDetail = JobBuilder.newJob(AccountSubscribeJob.class) .withIdentity("accountSubscribe","accountSubscribeGroup") .storeDurably() .build(); return accountSubscribeJobDetail; } @Bean public Trigger accountSubscribeTrigger(){ Trigger accountSubscribeTrigger = TriggerBuilder.newTrigger() .forJob(accountSubscribeDetail()) .withIdentity("accountSubscribeTrigger","accountSubscribeTriggerGroup") .startNow() .withSchedule(CronScheduleBuilder.cronSchedule("0 0 23 1/1 * ?")) .build(); return accountSubscribeTrigger; }
}