問題:服務宕機重啓後quartz沒有執行之前設置的定時任務。
原因:沒有配置jobStore,設置的定時不會寫入到qrtz開頭的表中。重啓服務quartz無法自動拾取之前設置的定時任務。
有jobStore的配置:
# quartz: # #相關屬性配置 # properties: # org: # quartz: # scheduler: # #quartzScheduler # instanceName: quartzScheduler # instanceId: AUTO # jobStore: # class: org.quartz.impl.jdbcjobstore.JobStoreTX # driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate # tablePrefix: QRTZ_ # #isClustered: false # isClustered: true # clusterCheckinInterval: 10000 # #useProperties: false # useProperties: true # dataSource: quartzDs # threadPool: # class: org.quartz.simpl.SimpleThreadPool # threadCount: 10 # threadPriority: 5 # threadsInheritContextClassLoaderOfInitializingThread: true # #數據庫方式 # job-store-type: JDBC # #初始化表結構 # jdbc: # initialize-schema: NEVER
沒有jobStore的配置:
quartz: #相關屬性配置 properties: org: quartz: scheduler: instanceName: quartzScheduler instanceId: AUTO threadPool: class: org.quartz.simpl.SimpleThreadPool threadCount: 10 threadPriority: 5 threadsInheritContextClassLoaderOfInitializingThread: true
沒有配置jobStore的配置的,需要在服務重啓中加載cron列表並放入定時器scheduler中。而配置jobStore的quartz會自動加載qrtz中之前設置的定時任務。
package com.lee.quartz.init; import com.lee.quartz.entity.QuartzJob; import com.lee.quartz.enums.JobStatus; import com.lee.quartz.mapper.JobMapper; import com.lee.quartz.service.IJobService; import org.quartz.JobKey; import org.quartz.Scheduler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import java.util.List; @Component public class ApplicationInit implements CommandLineRunner { private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationInit.class); @Autowired private JobMapper jobMapper; @Autowired private IJobService jobService; @Autowired private Scheduler scheduler; @Override public void run(String... args) throws Exception { loadJobToQuartz(); } private void loadJobToQuartz() throws Exception { LOGGER.info("quartz job load..."); // List<QuartzJob> jobs = jobMapper.listJob(""); // for(QuartzJob job : jobs) { // jobService.schedulerJob(job); // if (JobStatus.PAUSED.getStatus().equals(job.getTriggerState())) { // scheduler.pauseJob(new JobKey(job.getJobName(), job.getJobGroup())); // } // } } }