一、利弊
针对001 中设置,不方便程序中动态添加任务,只能使用配置进行配置任务,
适用于已知固定时刻需要执行的任务。
针对002中设置,不方便结合调用spring注入的实体
使用于程序内部新增添的任务
二、结合spring以及动态添加任务
1.编写ApplicationContext对象注入类
public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext context) throws BeansException { SpringContextUtil.applicationContext = context; } public static Object getBean(String name){ return applicationContext.getBean(name); } }
2.spring,xml配置
<bean id="spring" class="com.jd.ofc.trace.common.quartz.SpringContextUtil" />
3.编写Job接口工厂
public class JobFactory extends AdaptableJobFactory { @Autowired private AutowireCapableBeanFactory capableBeanFactory; @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { //调用父类的方法 Object jobInstance = super.createJobInstance(bundle); //进行注入 capableBeanFactory.autowireBean(jobInstance); return jobInstance; } }
4.注入Job工厂,以及quartz调度工厂
<bean id="jobFactory" class="com.jd.ofc.trace.common.quartz.JobFactory"></bean> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobFactory" ref="jobFactory"></property> </bean>
5.java帮助类
public class TaskJobUtil { private static final Logger logger = LoggerFactory.getLogger(TaskJobUtil.class); private TaskJobUtil() { } /** * 添加任务 * * @param job 要调用的job类 * @param jobName job名称 * @param cronExpression 任务触发时间表达式 * @param param 需要用到的参数,可在job类中的context获得 */ public static void addJob(Job job, String jobName, String cronExpression, Map param) { Scheduler scheduler = null; try { removeJob(jobName, job.getClass().getCanonicalName() + "Group");//删除原任务 scheduler= (StdScheduler)SpringContextUtil.getBean("schedulerFactoryBean"); JobDetail jobDetail = getJobDetail(job, jobName, param); String jobGroup = jobDetail.getGroup(); CronTrigger cronTrigger = getCronTrigger(job, jobName, jobGroup, cronExpression); scheduler.scheduleJob(jobDetail, cronTrigger); logger.info("添加任务:" + jobName); startScheduler(); } catch (SchedulerException e) { logger.error("", e); } } /** * 对外停止任务调度器方法 */ public static void stopJob() { shutdownScheduler(); } /** * 启动调度器 */ protected static void startScheduler() { Scheduler scheduler = null; try { scheduler= (StdScheduler)SpringContextUtil.getBean("schedulerFactoryBean"); //如果调度器未启动,启动调度器 if (scheduler != null && !scheduler.isStarted()) { scheduler.start(); } } catch (SchedulerException e) { logger.error("", e); } } /** * 关闭调度器,关闭后任务也都清除 */ protected static void shutdownScheduler() { Scheduler scheduler = null; try { scheduler= (StdScheduler)SpringContextUtil.getBean("schedulerFactoryBean"); //如果调度器未关闭,关闭调度器 if (scheduler != null && !scheduler.isShutdown()) { scheduler.shutdown(); } } catch (SchedulerException e) { logger.error("", e); } } /** * 获取JobDetail * * @param job * @param param * @return */ protected static JobDetail getJobDetail(Job job, String jobName, Map param) { Class jobClazz = job.getClass(); if (jobName == null || "".equals(jobName)) { jobName = jobClazz.getCanonicalName(); } String jobGroup = jobClazz.getCanonicalName() + "Group"; JobDetail jobDetail = new JobDetail(); jobDetail.setDurability(true); jobDetail.setRequestsRecovery(true); jobDetail.setName(jobName); jobDetail.setGroup(jobGroup); jobDetail.setJobClass(jobClazz); if (param != null && param.size() > 0) { JobDataMap jobDataMap = new JobDataMap(param);//存放参数 jobDetail.setJobDataMap(jobDataMap); logger.info("任务 " + jobName + " jobDetail存放的参数:" + param); } return jobDetail; } /** * 获取触发器 * * @param job * @param jobName * @param cronExpression * @return */ protected static CronTrigger getCronTrigger(Job job, String jobName, String jobGroup, String cronExpression) { Class jobClazz = job.getClass(); String triggerName = jobName+jobClazz.getCanonicalName() + "Trigger"; String triggerGroup = jobClazz.getCanonicalName() + "TriggerGroup"; CronTrigger cronTrigger = new CronTrigger(); cronTrigger.setName(triggerName); cronTrigger.setGroup(triggerGroup); cronTrigger.setJobName(jobName); cronTrigger.setJobGroup(jobGroup); try { cronTrigger.setCronExpression(cronExpression);//触发任务的时间表达式 } catch (Exception e) { logger.error("",e); } logger.info("任务 " + jobName + " 触发时间:" + cronExpression); return cronTrigger; } /** * 删除job * * @param jobName * @param jobGroup */ public static void removeJob(String jobName, String jobGroup) { logger.info("删除任务:jobName:" + jobName + " jobGroup:" + jobGroup); Scheduler scheduler = null; String[] jobNames = null; try { scheduler= (StdScheduler)SpringContextUtil.getBean("schedulerFactoryBean"); jobNames = scheduler.getJobNames(jobGroup); if (jobNames == null || jobNames.length == 0) { return; } for (String name : jobNames) { if (name.equals(jobName)) { scheduler.pauseTrigger(jobName, jobGroup);//停止触发器 scheduler.pauseJob(jobName, jobGroup);//暂停job scheduler.unscheduleJob(jobName, jobGroup);//取消预订的job scheduler.deleteJob(jobName, jobGroup);//删除job } } } catch (SchedulerException e) { logger.error("", e); } } /** * 获取任务列表 * * @return */ public static Map<String, List<String>> getJobList() { Scheduler scheduler = null; Map<String, List<String>> map = null; try { scheduler= (StdScheduler)SpringContextUtil.getBean("schedulerFactoryBean"); String[] jobGroupNames = scheduler.getJobGroupNames(); if (jobGroupNames == null || jobGroupNames.length == 0) { return null; } map = new HashMap<String, List<String>>(); for (String jobGroup : jobGroupNames) { String[] jobNames = scheduler.getJobNames(jobGroup); if (jobNames == null || jobNames.length == 0) { continue; } List<String> jobNameList = new ArrayList<String>(); for (String jobName : jobNames) { jobNameList.add(jobName); } map.put(jobGroup, jobNameList); } } catch (SchedulerException e) { logger.error("", e); } logger.info("获取job列表:" + map); return map; } }
6.程序中使用
直接抵用
addJob等即可