问题:服务宕机重启后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())); // } // } } }