Quartz由Java編寫的功能豐富的開源作業調度框架,可以集成到幾乎任何Java應用程式中,並且能夠創建多個作業調度; ...
目錄
標簽:Quartz.Job.Scheduler;
一、簡介
Quartz由Java編寫的功能豐富的開源作業調度框架,可以集成到幾乎任何Java應用程式中,並且能夠創建多個作業調度;
在實際的業務中,有很多場景依賴定時任務,比如常見的:訂單超時處理,數據報表統計分析,會員等周期性管理,業務識別和預警通知等;
二、工程搭建
1、工程結構
2、依賴管理
在starter-quartz
組件中,實際依賴的是quartz
組件2.3.2
版本,使用Quartz框架時,需要自定義任務和執行邏輯,以更加靈活的方式管理業務調度;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>${spring-boot.version}</version>
</dependency>
3、資料庫
Quartz框架使用的表結構在如圖的路徑下,本文選擇MySQL資料庫存儲,除此之外自定義兩張表:quartz_job
任務表和quartz_log
任務執行日誌表;
4、配置文件
在配置文件中使用Druid組件連接boot-quartz
資料庫,對於Quartz框架,主要配置資料庫存儲,調度器的基礎信息,以及執行任務的線程池;
spring:
# 定時器配置
quartz:
# 使用資料庫存儲
job-store-type: jdbc
# 初始化完成後自動啟動調度程式
autoStartup: true
properties:
org:
quartz:
# 調度器配置
scheduler:
instanceName: bootQuartzScheduler
instanceId: AUTO
# 存儲配置
jobStore:
class: org.springframework.scheduling.quartz.LocalDataSourceJobStore
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
tablePrefix: qrtz_
isClustered: true
misfireThreshold: 12000
clusterCheckinInterval: 15000
useProperties: false
# 線程池配置
threadPool:
threadNamePrefix: Boot_Job_Pool
threadPriority: 5
threadCount: 10
class: org.quartz.simpl.SimpleThreadPool
三、Quartz用法
對於任務管理的相關Web介面,採用Swagger文檔組件,介面和實體類添加註解後,訪問IP:Port/swagger-ui/index.html
地址即可;
1、初始化載入
在服務啟動時執行init
初始化方法,查詢quartz_job
表中運行和暫停狀態的任務,判斷觸發器是否存在,如果不存在則創建,如果存在則更新;
@Service
public class QuartzJobService {
@Resource
private QuartzJobMapper quartzJobMapper ;
@Resource
private QuartzManage quartzManage;
@PostConstruct
public void init () {
LambdaQueryWrapper<QuartzJob> queryWrapper = new LambdaQueryWrapper<>() ;
queryWrapper.in(QuartzJob::getState,JobState.JOB_RUN.getStatus(),JobState.JOB_STOP.getStatus());
List<QuartzJob> jobList = quartzJobMapper.selectList(queryWrapper);
jobList.forEach(quartzJob -> {
CronTrigger cronTrigger = quartzManage.getCronTrigger(quartzJob.getId()) ;
if (Objects.isNull(cronTrigger)){
quartzManage.createJob(quartzJob);
} else {
quartzManage.updateJob(quartzJob);
}
});
}
}
2、新增任務
在創建任務時,需要定義JobKey
和TriggerKey
的構建規則,Key需要具備唯一性,通常使用任務表的主鍵ID,任務一般是基於Cron表達式被調度執行的;
@Component
public class QuartzManage {
@Resource
private Scheduler scheduler ;
public void createJob (QuartzJob quartzJob){
try {
// 構建任務
JobDetail jobDetail = JobBuilder.newJob(QuartzRecord.class).withIdentity(getJobKey(quartzJob.getId())).build() ;
// 構建Cron調度器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder
.cronSchedule(quartzJob.getCronExpres())
.withMisfireHandlingInstructionDoNothing() ;
// 任務觸發器
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(getTriggerKey(quartzJob.getId()))
.withSchedule(scheduleBuilder).build() ;
jobDetail.getJobDataMap().put(QuartzJob.JOB_PARAM_KEY,quartzJob);
scheduler.scheduleJob(jobDetail,trigger) ;
// 狀態校驗
checkStop(quartzJob) ;
} catch (SchedulerException e){
throw new RuntimeException("createJob Fail",e) ;
}
}
}
3、更新任務
先通過任務ID查詢TriggerKey
,對於更新來說,最常見的就是Cron表達式即調度規則的更新,或者任務的執行參數更新;
@Component
public class QuartzManage {
@Resource
private Scheduler scheduler ;
public void updateJob(QuartzJob quartzJob) {
try {
// 查詢觸發器Key
TriggerKey triggerKey = getTriggerKey(quartzJob.getId());
// 構建Cron調度器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder
.cronSchedule(quartzJob.getCronExpres())
.withMisfireHandlingInstructionDoNothing();
// 任務觸發器
CronTrigger trigger = getCronTrigger(quartzJob.getId())
.getTriggerBuilder().withIdentity(triggerKey)
.withSchedule(scheduleBuilder).build();
trigger.getJobDataMap().put(QuartzJob.JOB_PARAM_KEY, quartzJob);
scheduler.rescheduleJob(triggerKey, trigger);
// 狀態校驗
checkStop(quartzJob) ;
} catch (SchedulerException e) {
throw new RuntimeException("updateJob Fail",e) ;
}
}
}
4、暫停任務
先通過任務ID查詢JobKey
,判斷任務是非運行狀態,則停止任務;
@Component
public class QuartzManage {
@Resource
private Scheduler scheduler ;
public void checkStop (QuartzJob quartzJob){
try {
if(quartzJob.getState() != JobState.JOB_RUN.getStatus()){
this.scheduler.pauseJob(getJobKey(quartzJob.getId()));
}
} catch (SchedulerException e){
throw new RuntimeException("pauseJob Fail",e) ;
}
}
}
5、恢復任務
先通過任務ID查詢JobKey
,恢復任務正常執行;
@Component
public class QuartzManage {
@Resource
private Scheduler scheduler ;
public void resumeJob (Integer jobId){
try {
this.scheduler.resumeJob(getJobKey(jobId));
} catch (SchedulerException e){
throw new RuntimeException("resumeJob Fail",e) ;
}
}
}
6、執行一次
傳入任務主體,再通過任務ID查詢JobKey
,然後立即執行一次任務;
@Component
public class QuartzManage {
@Resource
private Scheduler scheduler ;
public void run (QuartzJob quartzJob){
try {
JobDataMap dataMap = new JobDataMap() ;
dataMap.put(QuartzJob.JOB_PARAM_KEY,quartzJob);
this.scheduler.triggerJob(getJobKey(quartzJob.getId()),dataMap);
} catch (SchedulerException e){
throw new RuntimeException("run Fail",e) ;
}
}
}
7、刪除任務
先通過任務ID查詢JobKey
,徹底刪除任務;
@Component
public class QuartzManage {
@Resource
private Scheduler scheduler ;
public void deleteJob (Integer jobId){
try {
scheduler.deleteJob(getJobKey(jobId));
} catch (SchedulerException e){
throw new RuntimeException("deleteJob Fail",e) ;
}
}
}
8、任務執行
Quartz被集成在Spring框架之後,任務類自然會以Bean對象的方式被管理,在任務創建時,設置要執行的作業類QuartzRecord
,該類繼承QuartzJobBean
抽象類,通過重寫executeInternal
方法,來管理任務實際執行的邏輯;
public class QuartzRecord extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) {
QuartzJob quartzJob = (QuartzJob)context.getMergedJobDataMap().get(QuartzJob.JOB_PARAM_KEY) ;
QuartzLogService quartzLogService = (QuartzLogService)SpringContextUtil.getBean("quartzLogService") ;
// 定時器日誌記錄
QuartzLog quartzLog = new QuartzLog () ;
quartzLog.setJobId(quartzJob.getId());
quartzLog.setBeanName(quartzJob.getBeanName());
quartzLog.setParams(quartzJob.getParams());
quartzLog.setCreateTime(new Date());
long beginTime = System.currentTimeMillis() ;
try {
// 載入並執行
Object target = SpringContextUtil.getBean(quartzJob.getBeanName());
Method method = target.getClass().getDeclaredMethod("run", String.class);
method.invoke(target, quartzJob.getParams());
long executeTime = System.currentTimeMillis() - beginTime;
quartzLog.setTimes((int)executeTime);
quartzLog.setState(LogState.LOG_SUS.getStatus());
} catch (Exception e){
// 異常信息
long executeTime = System.currentTimeMillis() - beginTime;
quartzLog.setTimes((int)executeTime);
quartzLog.setState(LogState.LOG_FAIL.getStatus());
quartzLog.setError(e.getMessage());
} finally {
// 保存執行日誌
quartzLogService.insert(quartzLog) ;
}
}
}
四、參考源碼
文檔倉庫:
https://gitee.com/cicadasmile/butte-java-note
源碼倉庫:
https://gitee.com/cicadasmile/butte-spring-parent
Gitee主頁: https://gitee.com/cicadasmile/butte-java-note