package com.sihuatech.project.task.manager;import java.text.parseexception;import org.quartz.crontrigger;import org.quartz.job;import org.quartz.jobdetail;import org.quartz.scheduler;import org.quartz.schedulerexception;import org.quartz.schedulerfactory;import org.quartz.trigger;import org.quartz.impl.stdschedulerfactory;/** * @title:quartz管理类 * * @description: * * @copyright: * @author lei 2008-10-8 14:19:01 * @version 1.00.000 * */public class quartzmanager { private static schedulerfactory sf = new stdschedulerfactory(); private static string job_group_name = "group1"; private static string trigger_group_name = "trigger1"; private static quartzmanager quartzmanager; public static quartzmanager getinstance() { if (quartzmanager == null) { quartzmanager = new quartzmanager(); } return quartzmanager; } private quartzmanager() { } /** * 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 * * @param jobname * 任务名 * @param job * 任务 * @param time * 时间设置,参考quartz说明文档 * @throws schedulerexception * @throws parseexception */ public static void addjob(string jobname, job job, string time) throws schedulerexception, parseexception { scheduler sched = sf.getscheduler(); jobdetail jobdetail = new jobdetail(jobname, job_group_name, job.getclass());// 任务名,任务组,任务执行类 // 触发器 crontrigger trigger = new crontrigger(jobname, trigger_group_name);// 触发器名,触发器组 trigger.setcronexpression(time);// 触发器时间设定 sched.schedulejob(jobdetail, trigger); // 启动 if (!sched.isshutdown()) sched.start(); } /** * 添加一个定时任务 * * @param jobname * 任务名 * @param jobgroupname * 任务组名 * @param triggername * 触发器名 * @param triggergroupname * 触发器组名 * @param job * 任务 * @param time * 时间设置,参考quartz说明文档 * @throws schedulerexception * @throws parseexception */ public static void addjob(string jobname, string jobgroupname, string triggername, string triggergroupname, job job, string time) throws schedulerexception, parseexception { scheduler sched = sf.getscheduler(); jobdetail jobdetail = new jobdetail(jobname, jobgroupname, job.getclass());// 任务名,任务组,任务执行类 // 触发器 crontrigger trigger = new crontrigger(triggername, triggergroupname);// 触发器名,触发器组 trigger.setcronexpression(time);// 触发器时间设定 sched.schedulejob(jobdetail, trigger); if (!sched.isshutdown()) sched.start(); } /** * 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名) * * @param jobname * @param time * @throws schedulerexception * @throws parseexception */ public static void modifyjobtime(string jobname, string time) throws schedulerexception, parseexception { scheduler sched = sf.getscheduler(); trigger trigger = sched.gettrigger(jobname, trigger_group_name); if (trigger != null) { crontrigger ct = (crontrigger) trigger; ct.setcronexpression(time); sched.resumetrigger(jobname, trigger_group_name); } } /** * 修改一个任务的触发时间 * * @param triggername * @param triggergroupname * @param time * @throws schedulerexception * @throws parseexception */ public static void modifyjobtime(string triggername, string triggergroupname, string time) throws schedulerexception, parseexception { scheduler sched = sf.getscheduler(); trigger trigger = sched.gettrigger(triggername, triggergroupname); if (trigger != null) { crontrigger ct = (crontrigger) trigger; // 修改时间 ct.setcronexpression(time); // 重启触发器 sched.resumetrigger(triggername, triggergroupname); } } /** * 移除一个任务(使用默认的任务组名,触发器名,触发器组名) * * @param jobname * @throws schedulerexception */ public static void removejob(string jobname) throws schedulerexception { scheduler sched = sf.getscheduler(); sched.pausetrigger(jobname, trigger_group_name);// 停止触发器 sched.unschedulejob(jobname, trigger_group_name);// 移除触发器 sched.deletejob(jobname, job_group_name);// 删除任务 } /** * 移除一个任务 * * @param jobname * @param jobgroupname * @param triggername * @param triggergroupname * @throws schedulerexception */ public static void removejob(string jobname, string jobgroupname, string triggername, string triggergroupname) throws schedulerexception { scheduler sched = sf.getscheduler(); sched.pausetrigger(triggername, triggergroupname);// 停止触发器 sched.unschedulejob(triggername, triggergroupname);// 移除触发器 sched.deletejob(jobname, jobgroupname);// 删除任务 }}