项目ITP(七) javaWeb 整合 Quartz 实现动态调度 并且 持久化
原创地址:http://www.cnblogs.com/Alandre/(泥沙砖瓦浆木匠),需要转载的,保留下!
弟子规 圣人训 首孝弟 次谨信 泛爱众 而亲仁 有余力 则学文
Written In The Font
需要:WEB-INF/lib/quartz-2.2.1.jar
基本步骤:
- web.xml注册监听器ScheduleStartListener
- 监听器类sedion.jeffli.wmuitp.listener.ScheduleStartListener实现
- 测试案例第一步: Job 接口实现类JobTest
- 测试案例第二步:QuartzTest
web.xml注册监听器ScheduleStartListener
注册quartz监听器,监听项目是否启动或者重启.保证项目启动或重启时,所有任务会被重新安排到任务调度中.
web.xml添加一个Listener:
<!-- quartz监听器 --> <listener> <listener-class>sedion.jeffli.wmuitp.listener.ScheduleStartListener</listener-class> </listener>
监听器类sedion.jeffli.wmuitp.listener.ScheduleStartListener实现
监听器类主要是实现recovery各个任务,重新恢复在triggerGroups组中所有的触发器,按新的trigger重新设置job执行.顺便说下,这个异常自定义(不需要删除即可): sedion.jeffli.wmuitp.exception.QuartzException;
package sedion.jeffli.wmuitp.listener; import java.util.List; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.Trigger; import org.quartz.TriggerKey; import org.quartz.impl.StdSchedulerFactory; import sedion.jeffli.wmuitp.exception.QuartzException; public class ScheduleStartListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent sce) { } public void contextInitialized(ServletContextEvent sce) { try { recovery(); } catch (Exception e) { throw new QuartzException(" ScheduleStartListener contextInitialized ERROR!!",e); } } public void recovery() { Scheduler scheduler = null; try { SchedulerFactory schedulerFactory = new StdSchedulerFactory(); scheduler = schedulerFactory.getScheduler();//可以通过SchedulerFactory创建一个Scheduler实例 List<String> triggerGroups = scheduler.getTriggerGroupNames();//获取调度器中所有的触发器组 System.out.println("调度器中所有的触发器组 size():"+triggerGroups.size()); if(triggerGroups != null && triggerGroups.size() != 0)//重新恢复在triggerGroups组中所有的触发器 { for (int i = 0; i < triggerGroups.size(); i++) { TriggerKey triggerKey = TriggerKey.triggerKey(triggerGroups.get(i), triggerGroups.get(i)); System.out.println("triggerKey:"+triggerKey); Trigger tg = scheduler.getTrigger(triggerKey);//获取trigger System.out.println(triggerKey+" -> 执行时间 :"+tg.getNextFireTime()); scheduler.rescheduleJob(triggerKey, tg);//按新的trigger重新设置job执行 } } scheduler.start(); } catch (Exception e) { throw new QuartzException("ScheduleStartListener recovery() Error!", e); } } }
测试案例第一步: Job 接口实现类JobTest
顾名思义,用于自定义任务,方法的实现.你可以在其中写入任意你想要在那个点上干的事情(操作数据库,前台显示等).在下面那处地方写入你想要写的:System.out.println("添入需要加入任务的具体操作"); .顺便说下,这个异常自定义(不需要删除即可):
package test.quartz; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.TriggerKey; import org.quartz.impl.StdSchedulerFactory; import sedion.jeffli.wmuitp.exception.QuartzException; public class JobTest implements Job{ public JobTest() {} public void execute(JobExecutionContext context)throws JobExecutionException { JobDataMap data = context.getJobDetail().getJobDataMap(); System.out.println("data.testId : "+data.getInt("testId")); //不需要可删除 try { System.out.println("添入需要加入任务的具体操作"); } catch (Exception e) { throw new QuartzException("JobTest execute() ERROR !!", e); } } public static void removeJob(JobKey jobKey, TriggerKey tiKey)throws SchedulerException { SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); sched.pauseTrigger(tiKey); //停止触发器 sched.unscheduleJob(tiKey); //移除触发器 sched.deleteJob(jobKey); //删除任务 } }
测试案例第二步:QuartzTest
顾名思义,用于实现,检验.可以通过SchedulerFactory创建一个Scheduler实例,把触发器在集群节点实例命名的组只是为了区分(伐木)从什么地方定问调度重新执行此作业,如果它是正在进行时调度下去.
package test.quartz; import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; import java.util.Date; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.StdSchedulerFactory; public class QuartzTest { public void run(String date, int id)throws Exception { SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler();//可以通过SchedulerFactory创建一个Scheduler实例 //设置工作详情 JobDetail job = newJob(JobTest.class) .withIdentity("job_"+id, "test"+id) // (String name, String group)把触发器在集群节点实例命名的组只是为了区分(伐木)从什么地方定问调度重新执行此作业,如果它是正在进行时调度下去... .requestRecovery() .build(); job.getJobDataMap().put("testId", id); //设置存储参数(不需要可删除) Date startDate = FormatDate.stringToDateAll(date);//Date转String //设置触发器 SimpleTrigger trigger = (SimpleTrigger) newTrigger() .withIdentity("overdue"+id, "overdue"+id)//withIdentity("trigger", "group") .startAt(startDate) .build(); scheduler.scheduleJob(job, trigger); scheduler.start(); System.out.println("------- Start Scheduler ----------------"); } public static void main(String[] args) throws Exception { QuartzTest quartzOverdue = new QuartzTest(); quartzOverdue.run("2014-07-02 00:30:00",666);//666,随便的吉祥数字 } }
然后我们运行:
public static void main(String[] args) throws Exception { QuartzTest quartzOverdue = new QuartzTest(); quartzOverdue.run("2014-07-02 00:30:00",666);//666,随便的吉祥数字 }
看控制台:
先输出
------- Start Scheduler ----------------
然后时间到了
添入需要加入任务的具体操作