标签:
public class QuartzManager { public static StdSchedulerFactory factory = new StdSchedulerFactory(); public static String MODELNAME = "analyzerModel"; public static Scheduler scheduler = null; public static Scheduler getScheduler(){ try { scheduler = factory.getScheduler(); } catch (SchedulerException e) { e.printStackTrace(); } return scheduler; } public static String getJobName(String id) { return "job" + id; } public static String getTriggerName(String id) { return "trigger" + id; } /** * 启动所有定时任务 */ public static void startJobs() { try { Scheduler sched = factory.getScheduler(); sched.start(); } catch (Exception e) { throw new RuntimeException(e); } } /** * 停用所有定时任务 */ public static void shutdownJobs() { try { Scheduler sched = factory.getScheduler(); if (!sched.isShutdown()) { sched.shutdown(); } } catch (Exception e) { throw new RuntimeException(e); } } public static void addJob(TaskModel model) throws SchedulerException{ String exp = model.getExpress(); for (AnalyzerRelation analyzerModel : model.getaLists()) { addJob(analyzerModel,exp,model.getName()); } } public static void addJob(AnalyzerRelation analyzerModel,String exp,String name) throws SchedulerException{ System.out.println("【"+name+"】执行计划开始启动("+exp+")................."); String jobName = getJobName(analyzerModel.getId()); String triggerName = getTriggerName(analyzerModel.getId()); System.out.println("【"+jobName+"】job名称.............................."); System.out.println("【"+triggerName+"】trigger名称......................"); JobDetail job = (JobDetail) JobBuilder.newJob(CustomJob.class) .withIdentity(jobName, Scheduler.DEFAULT_GROUP).build(); job.getJobDataMap().put(QuartzManager.MODELNAME, analyzerModel); Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, Scheduler.DEFAULT_GROUP) .withSchedule(CronScheduleBuilder.cronSchedule(exp)).build(); scheduler.scheduleJob(job, trigger); } public static void removeJob(String id,String name) throws Exception{ System.out.println("【"+name+"】移除定时开始................................"); String jobName = getJobName(id); JobKey jobKey = JobKey.jobKey(jobName); String triggerName = QuartzManager.getTriggerName(id); TriggerKey triggerKey = TriggerKey.triggerKey(triggerName); scheduler.unscheduleJob(triggerKey); scheduler.pauseTrigger(triggerKey); scheduler.deleteJob(jobKey); System.out.println("【"+name+"】移除定时成功................................."); } }
package com.diqm.quality.plugin.job; import java.util.List; import org.quartz.CronTrigger; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.TriggerKey; import com.diqm.quality.service.model.AnalyzerRelation; /** * 启动定时任务 * * @author THINK * */ public class JobExecute { /** * 启动多个定时任务 * @param lists */ public void start(List<TaskModel> lists) { Scheduler scheduler = null; try { scheduler = QuartzManager.getScheduler(); for (TaskModel model : lists) { QuartzManager.addJob(model); } scheduler.start(); } catch (SchedulerException e) { e.printStackTrace(); } } /** * 启动定时任务 * @param lists */ public void start(TaskModel model) { Scheduler scheduler = null; try { scheduler = QuartzManager.getScheduler(); QuartzManager.addJob(model); scheduler.start(); } catch (SchedulerException e) { e.printStackTrace(); } } /** * 停止定时任务 * @param lists */ public void stop(List<AnalyzerRelation> lists) { Scheduler scheduler = null; try { scheduler = QuartzManager.getScheduler(); for (AnalyzerRelation analyzerModel : lists) { String triggerName = QuartzManager.getTriggerName(analyzerModel.getId()); TriggerKey triggerKey = TriggerKey.triggerKey(triggerName); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); if (trigger == null) { continue; }else{ QuartzManager.removeJob(analyzerModel.getId(),analyzerModel.getName()); } } scheduler.start(); } catch (Exception e) { e.printStackTrace(); } } public void modifyStart(TaskModel model){ Scheduler scheduler = null; try { scheduler = QuartzManager.getScheduler(); String exp = model.getExpress(); for (AnalyzerRelation analyzerModel : model.getaLists()) { String triggerName = QuartzManager.getTriggerName(analyzerModel.getId()); TriggerKey triggerKey = TriggerKey.triggerKey(triggerName); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); if (trigger == null) { QuartzManager.addJob(analyzerModel, exp,model.getName()); }else{ String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(exp)) { System.out.println("【修改时间】开始("+exp+")..."); QuartzManager.removeJob(analyzerModel.getId(),analyzerModel.getName()); QuartzManager.addJob(analyzerModel,exp,model.getName()); } } } scheduler.start(); } catch (Exception e) { e.printStackTrace(); } } /** * 停用所有定时任务 */ public static void shutdownJobs() { QuartzManager.shutdownJobs(); } }
标签:
原文地址:http://www.cnblogs.com/zt528/p/5413853.html