java Quartz 定時任務管理類
package org.fh.util; import java.util.Map; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.Job; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.TriggerBuilder; import org.quartz.TriggerKey; import org.quartz.impl.StdSchedulerFactory; /** * 說明:定時任務管理類 * 作者:FH Admin * from:fhadmin.cn */ public class QuartzManager { private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); //建立一個SchedulerFactory工廠例項 private static String JOB_GROUP_NAME = "FH_JOBGROUP_NAME"; //任務組 private static String TRIGGER_GROUP_NAME = "FH_TRIGGERGROUP_NAME"; //觸發器組 /**新增一個定時任務,使用預設的任務組名,觸發器名,觸發器組名 * @param jobName 任務名 * @param cls 任務 * @param time 時間設定,參考quartz說明文件 */ public static void addJob(String jobName, Class<? extends Job> cls, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); //通過SchedulerFactory構建Scheduler物件 JobDetail jobDetail= JobBuilder.newJob(cls).withIdentity(jobName,JOB_GROUP_NAME).build(); //用於描敘Job實現類及其他的一些靜態資訊,構建一個作業例項 CronTrigger trigger = (CronTrigger) TriggerBuilder .newTrigger() //建立一個新的TriggerBuilder來規範一個觸發器 .withIdentity(jobName, TRIGGER_GROUP_NAME) //給觸發器起一個名字和組名 .withSchedule(CronScheduleBuilder.cronSchedule(time)) .build(); sched.scheduleJob(jobDetail, trigger); if (!sched.isShutdown()) { sched.start(); // 啟動 } } catch (Exception e) { throw new RuntimeException(e); } } /**新增一個定時任務,使用預設的任務組名,觸發器名,觸發器組名 (帶引數) * @param jobName 任務名 * @param cls 任務 * @param time 時間設定,參考quartz說明文件 */ public static void addJob(String jobName, Class<? extends Job> cls, String time, Map<String,Object> parameter) { try { Scheduler sched = gSchedulerFactory.getScheduler(); //通過SchedulerFactory構建Scheduler物件 JobDetail jobDetail= JobBuilder.newJob(cls).withIdentity(jobName,JOB_GROUP_NAME).build(); //用於描敘Job實現類及其他的一些靜態資訊,構建一個作業例項 jobDetail.getJobDataMap().put("parameterList", parameter); //傳引數 CronTrigger trigger = (CronTrigger) TriggerBuilder .newTrigger() //建立一個新的TriggerBuilder來規範一個觸發器 .withIdentity(jobName, TRIGGER_GROUP_NAME) //給觸發器起一個名字和組名 .withSchedule(CronScheduleBuilder.cronSchedule(time)) .build(); sched.scheduleJob(jobDetail, trigger); if (!sched.isShutdown()) { sched.start(); // 啟動 } } catch (Exception e) { throw new RuntimeException(e); } } /**新增一個定時任務 * @param jobName 任務名 * @param jobGroupName 任務組名 * @param triggerName 觸發器名 * @param triggerGroupName 觸發器組名 * @param jobClass 任務 * @param time 時間設定,參考quartz說明文件 */ public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class<? extends Job> jobClass, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); JobDetail jobDetail= JobBuilder.newJob(jobClass).withIdentity(jobName,jobGroupName).build();// 任務名,任務組,任務執行類 CronTrigger trigger = (CronTrigger) TriggerBuilder // 觸發器 .newTrigger() .withIdentity(triggerName, triggerGroupName) .withSchedule(CronScheduleBuilder.cronSchedule(time)) .build(); sched.scheduleJob(jobDetail, trigger); if (!sched.isShutdown()) { sched.start(); // 啟動 } } catch (Exception e) { throw new RuntimeException(e); } } /**新增一個定時任務 (帶引數) * @param jobName 任務名 * @param jobGroupName 任務組名 * @param triggerName 觸發器名 * @param triggerGroupName 觸發器組名 * @param jobClass 任務 * @param time 時間設定,參考quartz說明文件 */ public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class<? extends Job> jobClass, String time, Map<String,Object> parameter) { try { Scheduler sched = gSchedulerFactory.getScheduler(); JobDetail jobDetail= JobBuilder.newJob(jobClass).withIdentity(jobName,jobGroupName).build();// 任務名,任務組,任務執行類 jobDetail.getJobDataMap().put("parameterList", parameter); //傳引數 CronTrigger trigger = (CronTrigger) TriggerBuilder // 觸發器 .newTrigger() .withIdentity(triggerName, triggerGroupName) .withSchedule(CronScheduleBuilder.cronSchedule(time)) .build(); sched.scheduleJob(jobDetail, trigger); if (!sched.isShutdown()) { sched.start(); // 啟動 } } catch (Exception e) { throw new RuntimeException(e); } } /** 修改一個任務的觸發時間(使用預設的任務組名,觸發器名,觸發器組名) * @param jobName 任務名 * @param time 新的時間設定 */ public static void modifyJobTime(String jobName, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); //通過SchedulerFactory構建Scheduler物件 TriggerKey triggerKey = TriggerKey.triggerKey(jobName,TRIGGER_GROUP_NAME); //通過觸發器名和組名獲取TriggerKey CronTrigger trigger = (CronTrigger)sched.getTrigger(triggerKey); //通過TriggerKey獲取CronTrigger if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME); //通過任務名和組名獲取JobKey JobDetail jobDetail = sched.getJobDetail(jobKey); Class<? extends Job> objJobClass = jobDetail.getJobClass(); removeJob(jobName); addJob(jobName, objJobClass, time); } } catch (Exception e) { throw new RuntimeException(e); } } /**修改一個任務的觸發時間 * @param triggerName 任務名稱 * @param triggerGroupName 傳過來的任務名稱 * @param time 更新後的時間規則 */ public static void modifyJobTime(String triggerName, String triggerGroupName, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); //通過SchedulerFactory構建Scheduler物件 TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroupName); //通過觸發器名和組名獲取TriggerKey CronTrigger trigger = (CronTrigger)sched.getTrigger(triggerKey); //通過TriggerKey獲取CronTrigger if (trigger == null) return; CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(trigger.getCronExpression()); String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { trigger = (CronTrigger)trigger.getTriggerBuilder() //重新構建trigger .withIdentity(triggerKey) .withSchedule(scheduleBuilder) .withSchedule(CronScheduleBuilder.cronSchedule(time)) .build(); sched.rescheduleJob(triggerKey, trigger); //按新的trigger重新設定job執行 } } catch (Exception e) { throw new RuntimeException(e); } } /**移除一個任務(使用預設的任務組名,觸發器名,觸發器組名) * @param jobName 任務名稱 */ public static void removeJob(String jobName) { try { Scheduler sched = gSchedulerFactory.getScheduler(); TriggerKey triggerKey = TriggerKey.triggerKey(jobName,TRIGGER_GROUP_NAME); //通過觸發器名和組名獲取TriggerKey JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME); //通過任務名和組名獲取JobKey sched.pauseTrigger(triggerKey); // 停止觸發器 sched.unscheduleJob(triggerKey);// 移除觸發器 sched.deleteJob(jobKey); // 刪除任務 } catch (Exception e) { throw new RuntimeException(e); } } /**移除一個任務 * @param jobName 任務名 * @param jobGroupName 任務組名 * @param triggerName 觸發器名 * @param triggerGroupName 觸發器組名 */ public static void removeJob(String jobName, String jobGroupName,String triggerName, String triggerGroupName) { try { Scheduler sched = gSchedulerFactory.getScheduler(); TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroupName); //通過觸發器名和組名獲取TriggerKey JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); //通過任務名和組名獲取JobKey sched.pauseTrigger(triggerKey); // 停止觸發器 sched.unscheduleJob(triggerKey);// 移除觸發器 sched.deleteJob(jobKey); // 刪除任務 } catch (Exception e) { throw new RuntimeException(e); } } /** * 啟動所有定時任務 */ public static void startJobs() { try { Scheduler sched = gSchedulerFactory.getScheduler(); sched.start(); } catch (Exception e) { throw new RuntimeException(e); } } /** * 關閉所有定時任務 */ public static void shutdownJobs() { try { Scheduler sched = gSchedulerFactory.getScheduler(); if (!sched.isShutdown()) { sched.shutdown(); } } catch (Exception e) { throw new RuntimeException(e); } } }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31558068/viewspace-2856565/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Quartz 定時任務管理類quartz
- springboot Quartz 定時任務工具類Spring Bootquartz
- 石英定時任務-quartzquartz
- quartz定時任務時間設定quartz
- Quartz定時任務框架(二) Quartz詳解quartz框架
- 簡單說說Java 定時任務框架---QuartzJava框架quartz
- java 定時任務 quartz 時間表示式Cron總結Javaquartz
- Spring Scheduler定時任務 + QuartzSpringquartz
- Spring Boot 配置 Quartz 定時任務Spring Bootquartz
- Quartz高可用定時任務快速上手quartz
- 普通web整合quartz跑定時任務Webquartz
- Quartz實現動態定時任務quartz
- SpringBoot整合Quartz定時任務Spring Bootquartz
- 淺談分散式定時任務之quartz分散式quartz
- Quartz定時任務框架(一) 入門案例quartz框架
- Spring+quartz 實現定時任務Springquartz
- 定時任務管理
- Quartz - Spring整合Quartz實現叢集的定時任務quartzSpring
- Java 定時任務Java
- spring2.0 quartz 靜態定時任務Springquartz
- quartz學習-quartz編碼方式實現定時任務簡例quartz
- java web定時任務JavaWeb
- Java & Go 定時任務JavaGo
- Grails整合Quartz外掛實現定時任務(Job)AIquartz
- 使用Spring整合Quartz輕鬆完成定時任務Springquartz
- Spring 整合 Quartz 實現動態定時任務Springquartz
- 一看便知spring+quartz定時任務Springquartz
- NET作業排程(定時任務)-Quartz.Netquartz
- 定時任務管理系統
- 初識spring與quartz整合實現定時任務Springquartz
- Java編寫定時任務Java
- java定時任務巢狀Java巢狀
- Spring+quartz實現動態化定時任務 (轉)Springquartz
- springboot整合Quartz實現動態配置定時任務Spring Bootquartz
- Java 中的定時任務(一)Java
- Java定時任務解決方案Java
- Java之定時任務全家桶Java
- Java如何實現定時任務?Java