quartz动态job工具类 serviceh注入问题
package com.heyi.yanglao.common.job.util; import cn.hutool.core.date.DateUtil; import lombok.extern.slf4j.Slf4j; import org.quartz.*; import java.util.Date; /** * 定时任务工具类 * * @author jiang */ @Slf4j public class ScheduleUtils { private final static String JOB_NAME = "TASK_"; /** * 获取触发器key */ public static TriggerKey getTriggerKey(String jobId) { return TriggerKey.triggerKey(JOB_NAME + "TRIGGER_KEY" + jobId); } /** * 获取jobKey */ public static JobKey getJobKey(String jobId) { return JobKey.jobKey(JOB_NAME + "JOB_KEY" + jobId); } /** * 获取表达式触发器 */ public static CronTrigger getCronTrigger(Scheduler scheduler, String jobId) { try { return (CronTrigger) scheduler.getTrigger(getTriggerKey(jobId)); } catch (SchedulerException e) { e.printStackTrace(); log.info("获取表达式触发器失败"); log.info("productId:" + jobId); return null; } } /** * 创建定时任务 * * @param scheduler * @param activeId 活动id * @param status 1进行中2未开始3已结束 * @param cron */ public static void createScheduleJob(Scheduler scheduler, String activeId, String status, String cron) { try { log.info("创建定时任务"); log.info("activeId:" + activeId); log.info("status:" + status); //构建job信息 JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(getJobKey(activeId)).build(); //表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron).withMisfireHandlingInstructionDoNothing(); //按新的cronExpression表达式构建一个新的trigger CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(activeId)).withSchedule(scheduleBuilder).build(); //放入参数,运行时的方法可以获取 jobDetail.getJobDataMap().put("activeId", activeId); jobDetail.getJobDataMap().put("status", status); //执行定时器 scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) { e.printStackTrace(); log.info("创建定时任务失败"); log.info("activeId:" + activeId); } } /** * 更新定时任务 * * @param scheduler * @param activeId 活动id * @param status 1进行中2未开始3已结束 * @param cron */ public static void updateScheduleJob(Scheduler scheduler, String activeId, String status, String cron) { try { log.info("更新定时任务"); log.info("activeId:" + activeId); log.info("status:" + status); TriggerKey triggerKey = getTriggerKey(activeId); //表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron).withMisfireHandlingInstructionDoNothing(); CronTrigger trigger = getCronTrigger(scheduler, activeId); //按新的cronExpression表达式重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); //参数 trigger.getJobDataMap().put("activeId", activeId); trigger.getJobDataMap().put("status", status); scheduler.rescheduleJob(triggerKey, trigger); log.info("更新定时任务开始," + DateUtil.formatDate(new Date())); } catch (SchedulerException e) { e.printStackTrace(); log.info("更新定时任务失败"); log.info("activeId:" + activeId); } } /** * 删除定时任务 * * @param scheduler * @param activeId */ public static void deleteScheduleJob(Scheduler scheduler, String activeId) { try { log.info("删除定时任务"); log.info("activeId:" + activeId); scheduler.deleteJob(getJobKey(activeId)); } catch (SchedulerException e) { e.printStackTrace(); log.info("删除定时任务失败"); log.info("activeId:" + activeId); } } }
解决service注入问题
package com.heyi.yanglao.common.job.config; import org.quartz.spi.TriggerFiredBundle; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.scheduling.quartz.AdaptableJobFactory; import org.springframework.stereotype.Component; /** * 定义自己的job工厂 * * @author: jiang * @Date: 2019/01/25 17:03 * @Desc */ @Component public class MyJobFactory extends AdaptableJobFactory { @Autowired private AutowireCapableBeanFactory capableBeanFactory; @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { // 调用父类的方法 Object jobInstance = super.createJobInstance(bundle); // 进行注入 capableBeanFactory.autowireBean(jobInstance); return jobInstance; } }
quartz基本配置:
@Bean public SchedulerFactoryBean schedulerFactoryBean() { SchedulerFactoryBean factory = new SchedulerFactoryBean(); Properties prop = new Properties(); // prop.put("org.quartz.scheduler.instanceName", "ProductSearchScheduler"); // prop.put("org.quartz.scheduler.instanceId", "AUTO"); //线程池配置 prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool"); prop.put("org.quartz.threadPool.threadCount", "30"); prop.put("org.quartz.threadPool.threadPriority", "5"); //JobStore配置 // prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX"); //集群配置 // prop.put("org.quartz.jobStore.isClustered", "true"); // prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000"); // prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1"); // // prop.put("org.quartz.jobStore.misfireThreshold", "12000"); // prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_"); // prop.put("org.quartz.jobStore.selectWithLockSQL", "SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME = ?"); //PostgreSQL数据库,需要打开此注释 // prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.PostgreSQLDelegate"); factory.setQuartzProperties(prop); factory.setSchedulerName("ActiveScheduler"); //延时启动 // factory.setStartupDelay(30); // factory.setApplicationContextSchedulerContextKey("applicationContextKey"); //可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 // factory.setOverwriteExistingJobs(true); //设置自动启动,默认为true factory.setAutoStartup(true); //service注入问题 factory.setJobFactory(myJobFactory); return factory; }