/** * @Description: * * @Title: QuartzManager.java * @Package com.joyce.quartz * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:15:52 * @version V2.0 */ package com.viewhigh.tasktimer.autoCache; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import org.apache.log4j.Logger; import org.quartz216.CronTrigger; import org.quartz216.JobDetail; import org.quartz216.JobKey; import org.quartz216.Scheduler; import org.quartz216.SchedulerFactory; import org.quartz216.TriggerKey; import org.quartz216.impl.JobDetailImpl; import org.quartz216.impl.StdSchedulerFactory; import org.quartz216.impl.triggers.CronTriggerImpl; /** * @Description: 定时任务管理类 * * @ClassName: QuartzManager * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:15:52 * @version V2.0 */ public class QuartzManager { private static Logger log = Logger.getLogger(QuartzManager.class); private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); private static String JOB_GROUP_NAME = "AUTO_TASK_JOB_GROUP"; private static String TRIGGER_GROUP_NAME = "AUTO_TASK_TRIGGER_GROUP"; /** * @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 * * @param jobName * 任务名 * @param cls * 任务 * @param time * 时间设置,参考quartz说明文档 * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:47:44 * @version V2.0 */ @SuppressWarnings({ "unchecked", "deprecation" }) public static void addJob(String jobName, Class cls, String time,Map<String,Object> attr) { try { Scheduler sched = gSchedulerFactory.getScheduler(); JobDetail jobDetail = new JobDetailImpl(jobName, JOB_GROUP_NAME, cls);// 任务名,任务组,任务执行类 // 触发器 CronTriggerImpl trigger = new CronTriggerImpl(jobName, TRIGGER_GROUP_NAME);// 触发器名,触发器组 if(attr != null){ for (Entry<String, Object> entry : attr.entrySet()) { trigger.getJobDataMap().put(entry.getKey(), entry.getValue()); } } // time = "0 0/"+time+" * * * ?"; // time = "0/0 * * * * ?"; trigger.setCronExpression(time);// 触发器时间设定 sched.scheduleJob(jobDetail, trigger); // 启动 if (!sched.isShutdown()) { log.debug("启动定时任务:"+jobName+",time:"+time); sched.start(); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * @Description: 添加一个定时任务 * * @param jobName * 任务名 * @param jobGroupName * 任务组名 * @param triggerName * 触发器名 * @param triggerGroupName * 触发器组名 * @param jobClass * 任务 * @param time * 时间设置,参考quartz说明文档 * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:48:15 * @version V2.0 */ @SuppressWarnings({ "unchecked", "deprecation" }) public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); JobDetail jobDetail = new JobDetailImpl(jobName, jobGroupName, jobClass);// 任务名,任务组,任务执行类 // 触发器 CronTriggerImpl trigger = new CronTriggerImpl(triggerName, triggerGroupName);// 触发器名,触发器组 trigger.setCronExpression(time);// 触发器时间设定 sched.scheduleJob(jobDetail, trigger); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名) * * @param jobName * @param time * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:49:21 * @version V2.0 */ @SuppressWarnings("unchecked") public static void modifyJobTime(String jobName, String time,Map<String,Object> attr) { try { Scheduler sched = gSchedulerFactory.getScheduler(); CronTrigger trigger = (CronTrigger) sched.getTrigger(new TriggerKey(jobName,TRIGGER_GROUP_NAME)); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { JobDetail jobDetail = sched.getJobDetail(new JobKey(jobName,JOB_GROUP_NAME)); Class objJobClass = jobDetail.getJobClass(); removeJob(jobName); addJob(jobName, objJobClass, time,attr); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * @Description: 修改一个任务的触发时间 * * @param triggerName * @param triggerGroupName * @param time * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:49:37 * @version V2.0 */ /* public static void modifyJobTime(String triggerName, String triggerGroupName, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName,triggerGroupName); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { CronTrigger ct = (CronTrigger) trigger; // 修改时间 ct.setCronExpression(time); // 重启触发器 sched.resumeTrigger(triggerName, triggerGroupName); } } catch (Exception e) { throw new RuntimeException(e); } } */ /** * @Description: 移除一个任务(使用默认的任务组名,触发器名,触发器组名) * * @param jobName * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:49:51 * @version V2.0 */ public static void removeJob(String jobName) { try { Scheduler sched = gSchedulerFactory.getScheduler(); sched.pauseTrigger(new TriggerKey(jobName, TRIGGER_GROUP_NAME));// 停止触发器 log.info("停止触发器,jobName:"+jobName); sched.unscheduleJob(new TriggerKey(jobName, TRIGGER_GROUP_NAME));// 移除触发器 log.info("移除触发器,jobName:"+jobName); sched.deleteJob(new JobKey(jobName, JOB_GROUP_NAME));// 删除任务 log.info("删除Job,jobName:"+jobName); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * @Description: 移除一个任务 * * @param jobName * @param jobGroupName * @param triggerName * @param triggerGroupName * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:50:01 * @version V2.0 */ public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) { try { Scheduler sched = gSchedulerFactory.getScheduler(); sched.pauseTrigger(new TriggerKey( triggerName, triggerGroupName));// 停止触发器 sched.unscheduleJob(new TriggerKey( triggerName, triggerGroupName));// 移除触发器 sched.deleteJob(new JobKey(jobName, jobGroupName));// 删除任务 } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * @Description:启动所有定时任务 * * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:50:18 * @version V2.0 */ public static void startJobs() { try { Scheduler sched = gSchedulerFactory.getScheduler(); sched.start(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * @Description:关闭所有定时任务 * * * @Title: QuartzManager.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:50:26 * @version V2.0 */ public static void shutdownJobs() { try { Scheduler sched = gSchedulerFactory.getScheduler(); if (!sched.isShutdown()) { sched.shutdown(); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void main(String[] args) { try { Map<String,Object> attr = new HashMap<String, Object>(); attr.put("aaa", "传递的参数aaa"); String job_name = "动态任务调度"; System.out.println("【系统启动】开始(每1秒输出一次)..."); QuartzManager.addJob(job_name, QuartzJob.class, "0/1 * * * * ?",attr); Thread.sleep(5000); System.out.println("【修改时间】开始(每2秒输出一次)..."); QuartzManager.modifyJobTime(job_name, "10/2 * * * * ?",attr); Thread.sleep(6000); System.out.println("【移除定时】开始..."); QuartzManager.removeJob(job_name); System.out.println("【移除定时】成功"); System.out.println("【再次添加定时任务】开始(每10秒输出一次)..."); QuartzManager.addJob(job_name, QuartzJob.class, "*/10 * * * * ?",attr); Thread.sleep(60000); System.out.println("【移除定时】开始..."); QuartzManager.removeJob(job_name); System.out.println("【移除定时】成功"); } catch (Exception e) { e.printStackTrace(); } } public static boolean isExitsJob(String jobName){ Scheduler sched; try { sched = gSchedulerFactory.getScheduler(); CronTrigger trigger = (CronTrigger) sched.getTrigger(new TriggerKey(jobName,TRIGGER_GROUP_NAME)); return trigger != null; } catch (Exception e) { e.printStackTrace(); } return false; } }