Quartz動態添加,修改,刪除任務(暫停,任務狀態,恢復,最近觸發時間)
本案例採用quartz-2.2.3.jarphp
入門案例,執行比較簡單的任務html
QuartzManager.java (任務的管理)前端
-
import java.text.ParseException;
-
import java.text.SimpleDateFormat;
-
import java.util.ArrayList;
-
import java.util.Date;
-
import java.util.List;
-
-
import org.quartz.CronScheduleBuilder;
-
import org.quartz.CronTrigger;
-
import org.quartz.JobBuilder;
-
import org.quartz.JobDetail;
-
import org.quartz.JobKey;
-
import org.quartz.Scheduler;
-
import org.quartz.SchedulerException;
-
import org.quartz.SchedulerFactory;
-
import org.quartz.Trigger;
-
import org.quartz.Trigger.TriggerState;
-
import org.quartz.TriggerBuilder;
-
import org.quartz.TriggerKey;
-
import org.quartz.TriggerUtils;
-
import org.quartz.impl.StdSchedulerFactory;
-
import org.quartz.impl.triggers.CronTriggerImpl;
-
-
public class QuartzManager {
-
-
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
-
private static String JOB_GROUP_NAME = "MY_JOBGROUP_NAME";
-
private static String TRIGGER_GROUP_NAME = "MY_TRIGGERGROUP_NAME";
-
-
/**
-
* @Description: 添加一個定時任務,使用默認的任務組名,觸發器名,觸發器組名
-
* @param jobName
-
* 任務名
-
* @param cls
-
* 任務
-
* @param time
-
* 時間設置,參考quartz說明文檔
-
*/
-
-
public static void addJob(String jobName, Class cls, String cron) {
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
JobDetail job = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME).build();
-
// 表達式調度構建器
-
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
-
// 按新的cronExpression表達式構建一個新的trigger
-
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, TRIGGER_GROUP_NAME)
-
.withSchedule(scheduleBuilder).build();
-
-
// 交給scheduler去調度
-
sched.scheduleJob(job, trigger);
-
-
// 啓動
-
if (!sched.isShutdown()) {
-
sched.start();
-
System.err.println( "添加任務:"+jobName);
-
}
-
} catch (Exception e) {
-
throw new RuntimeException(e);
-
}
-
}
-
-
/**
-
* @Description: 添加一個定時任務
-
* @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 jobClass, String cron) {
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
JobDetail job = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
-
// 表達式調度構建器
-
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
-
// 按新的cronExpression表達式構建一個新的trigger
-
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName)
-
.withSchedule(scheduleBuilder).build();
-
sched.scheduleJob(job, trigger);
-
// 啓動
-
if (!sched.isShutdown()) {
-
sched.start();
-
}
-
} catch (Exception e) {
-
throw new RuntimeException(e);
-
}
-
}
-
-
/**
-
* @Description: 修改一個任務的觸發時間(使用默認的任務組名,觸發器名,觸發器組名)
-
* @param jobName
-
* @param time
-
*/
-
public static void modifyJobTime(String jobName, String cron) {
-
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME);
-
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
-
if (trigger == null) {
-
return;
-
}
-
String oldTime = trigger.getCronExpression();
-
if (!oldTime.equalsIgnoreCase(cron)) {
-
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
-
// 按新的cronExpression表達式從新構建trigger
-
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
-
// 按新的trigger從新設置job執行
-
sched.rescheduleJob(triggerKey, trigger);
-
}
-
} catch (Exception e) {
-
throw new RuntimeException(e);
-
}
-
}
-
-
-
/**
-
* @Description:修改任務,(能夠修改任務名,任務類,觸發時間)
-
* 原理:移除原來的任務,添加新的任務
-
* @param oldJobName :原任務名
-
* @param jobName
-
* @param jobclass
-
* @param cron
-
* @date 2018年5月23日 上午9:13:10
-
*/
-
-
public static void modifyJob(String oldJobName, String jobName, Class jobclass, String cron) {
-
/*
-
* removeJob(oldJobName);
-
* addJob(jobName, jobclass, cron);
-
* System.err.println("修改任務"+oldJobName);
-
*/
-
TriggerKey triggerKey = TriggerKey.triggerKey(oldJobName, TRIGGER_GROUP_NAME);
-
JobKey jobKey = JobKey.jobKey(oldJobName, JOB_GROUP_NAME);
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
Trigger trigger = (Trigger) sched.getTrigger(triggerKey);
-
if (trigger == null) {
-
return;
-
}
-
sched.pauseTrigger(triggerKey); // 中止觸發器
-
sched.unscheduleJob(triggerKey); // 移除觸發器
-
sched.deleteJob(jobKey); // 刪除任務
-
System.err.println( "移除任務:" + oldJobName);
-
-
JobDetail job = JobBuilder.newJob(jobclass).withIdentity(jobName, JOB_GROUP_NAME).build();
-
// 表達式調度構建器
-
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
-
// 按新的cronExpression表達式構建一個新的trigger
-
Trigger newTrigger = TriggerBuilder.newTrigger().withIdentity(jobName, TRIGGER_GROUP_NAME)
-
.withSchedule(scheduleBuilder).build();
-
-
// 交給scheduler去調度
-
sched.scheduleJob(job, newTrigger);
-
-
// 啓動
-
if (!sched.isShutdown()) {
-
sched.start();
-
System.err.println( "添加新任務:" + jobName);
-
}
-
System.err.println( "修改任務【" + oldJobName + "】爲:" + jobName);
-
} catch (Exception e) {
-
throw new RuntimeException(e);
-
}
-
-
}
-
-
-
/**
-
* @Description: 修改一個任務的觸發時間
-
* @param triggerName
-
* @param triggerGroupName
-
* @param time
-
*/
-
public static void modifyJobTime(String triggerName, String triggerGroupName, String cron) {
-
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
-
if (trigger == null) {
-
return;
-
}
-
String oldTime = trigger.getCronExpression();
-
if (!oldTime.equalsIgnoreCase(cron)) {
-
// trigger已存在,則更新相應的定時設置
-
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
-
// 按新的cronExpression表達式從新構建trigger
-
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
-
// 按新的trigger從新設置job執行
-
sched.resumeTrigger(triggerKey);
-
}
-
} catch (Exception e) {
-
throw new RuntimeException(e);
-
}
-
}
-
-
/**
-
* @Description 移除一個任務(使用默認的任務組名,觸發器名,觸發器組名)
-
* @param jobName
-
*/
-
public static void removeJob(String jobName) {
-
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME);
-
JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
Trigger trigger = (Trigger) sched.getTrigger(triggerKey);
-
if (trigger == null) {
-
return;
-
}
-
sched.pauseTrigger(triggerKey); // 中止觸發器
-
sched.unscheduleJob(triggerKey); // 移除觸發器
-
sched.deleteJob(jobKey); // 刪除任務
-
System.err.println( "移除任務:"+jobName);
-
} catch (Exception e) {
-
throw new RuntimeException(e);
-
}
-
}
-
-
/**
-
* @Description: 移除一個任務
-
* @param jobName
-
* @param jobGroupName
-
* @param triggerName
-
* @param triggerGroupName
-
*/
-
public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
-
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, triggerGroupName);
-
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
sched.pauseTrigger(triggerKey); // 中止觸發器
-
sched.unscheduleJob(triggerKey); // 移除觸發器
-
sched.deleteJob(jobKey); // 刪除任務
-
} catch (Exception e) {
-
throw new RuntimeException(e);
-
}
-
}
-
-
/**
-
* @Description:暫停一個任務(使用默認組名)
-
* @param jobName
-
*/
-
public static void pauseJob(String jobName) {
-
JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
sched.pauseJob(jobKey);
-
} catch (SchedulerException e) {
-
e.printStackTrace();
-
}
-
}
-
-
/**
-
* @Description:暫停一個任務
-
* @param jobName
-
* @param jobGroupName
-
*/
-
public static void pauseJob(String jobName, String jobGroupName) {
-
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
sched.pauseJob(jobKey);
-
} catch (SchedulerException e) {
-
e.printStackTrace();
-
}
-
}
-
-
/**
-
* @Description:恢復一個任務(使用默認組名)
-
* @param jobName
-
*/
-
public static void resumeJob(String jobName) {
-
JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
sched.resumeJob(jobKey);
-
} catch (SchedulerException e) {
-
e.printStackTrace();
-
}
-
}
-
-
/**
-
* @Description:恢復一個任務
-
* @param jobName
-
* @param jobGroupName
-
* @date 2018年5月17日 上午9:56:09
-
*/
-
public static void resumeJob(String jobName, String jobGroupName) {
-
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
sched.resumeJob(jobKey);
-
} catch (SchedulerException e) {
-
e.printStackTrace();
-
}
-
}
-
-
/**
-
* @Description:啓動全部定時任務
-
*/
-
public static void startJobs() {
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
sched.start();
-
} catch (Exception e) {
-
throw new RuntimeException(e);
-
}
-
}
-
-
/**
-
* @Description 關閉全部定時任務
-
*/
-
public static void shutdownJobs() {
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
if (!sched.isShutdown()) {
-
sched.shutdown();
-
}
-
} catch (Exception e) {
-
throw new RuntimeException(e);
-
}
-
}
-
-
/**
-
* @Description: 當即運行任務,這裏的當即運行,只會運行一次,方便測試時用。
-
* @param jobName
-
* @param jobGroupName
-
* @date 2018年5月17日 上午10:03:26
-
*/
-
public static void triggerJob(String jobName) {
-
JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
sched.triggerJob(jobKey);
-
} catch (SchedulerException e) {
-
e.printStackTrace();
-
}
-
}
-
-
/**
-
* @Description: 當即運行任務,這裏的當即運行,只會運行一次,方便測試時用。
-
* @param jobName
-
* @param jobGroupName
-
* @date 2018年5月17日 上午10:03:26
-
*/
-
public static void triggerJob(String jobName, String jobGroupName) {
-
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
sched.triggerJob(jobKey);
-
} catch (SchedulerException e) {
-
e.printStackTrace();
-
}
-
}
-
-
/**
-
* @Description: 獲取任務狀態
-
* NONE: 不存在
-
* NORMAL: 正常
-
* PAUSED: 暫停
-
* COMPLETE:完成
-
* ERROR : 錯誤
-
* BLOCKED : 阻塞
-
* @param jobName 觸發器名
-
* @date 2018年5月21日 下午2:13:45
-
*/
-
public static String getTriggerState(String jobName){
-
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME);
-
String name = null;
-
try {
-
Scheduler sched = gSchedulerFactory.getScheduler();
-
TriggerState triggerState = sched.getTriggerState(triggerKey);
-
name = triggerState.name();
-
} catch (SchedulerException e) {
-
e.printStackTrace();
-
}
-
return name;
-
}
-
-
/**
-
* @Description:獲取最近8次執行時間
-
* @param cron
-
* @date 2018年5月24日 下午5:13:03
-
*/
-
public static List<String> getRecentTriggerTime(String cron) {
-
List<String> list = new ArrayList<String>();
-
try {
-
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
-
cronTriggerImpl.setCronExpression(cron);
-
// 這個是重點,一行代碼搞定
-
List<Date> dates = TriggerUtils.computeFireTimes(cronTriggerImpl, null, 8);
-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
for (Date date : dates) {
-
list.add(dateFormat.format(date));
-
}
-
-
} catch (ParseException e) {
-
e.printStackTrace();
-
}
-
return list;
-
}
-
-
}
MyJob.java 執行的任務java
-
-
import java.text.SimpleDateFormat;
-
import java.util.Calendar;
-
-
import org.quartz.Job;
-
import org.quartz.JobExecutionContext;
-
import org.quartz.JobExecutionException;
-
-
/**
-
* @Description:
-
* @date 2018年5月16日
-
*/
-
public class MyJob implements Job {
-
-
-
public void execute(JobExecutionContext context) throws JobExecutionException {
-
-
System.err.println( "任務運行開始-------- start --------");
-
try {
-
String jobName = context.getJobDetail().getKey().getName();
-
String jobGroup = context.getJobDetail().getKey().getGroup();
-
String triggerName = context.getTrigger().getKey().getName();
-
String triggerGroup = context.getTrigger().getKey().getGroup();
-
-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒");
-
System.err.println( "觸發器Key:" + triggerName + ".." + triggerGroup + " 正在執行...");
-
System.err.println( "任務Key:" + jobName + ".." + jobGroup + " 正在執行,執行時間: "
-
+ dateFormat.format(Calendar.getInstance().getTime()));
-
-
} catch (Exception e) {
-
System.err.println( "捕獲異常==="+e);
-
}
-
System.err.println( "任務運行結束-------- end --------");
-
}
-
-
}
測試類 LoadTask.javapython
-
-
public class LoadTask {
-
-
public static void main(String[] args) {
-
System.err.println( "【系統啓動】");
-
String corn = "0/5 * * * * ?";
-
//QuartzManager.addJob("job1", "jobGooup", "trigger1", "triggerGroup", MyJob.class, corn);
-
QuartzManager.addJob( "job1", MyJob.class, corn);
-
System.err.println( "添加任務一");
-
QuartzManager.getTriggerState( "jobs");
-
-
//睡眠一分鐘
-
try {
-
Thread.sleep( 60L * 1000L);
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
-
-
QuartzManager.modifyJobTime( "job1", "0/3 * * * * ?");
-
System.out.println( "修改觸發時間");
-
-
try {
-
Thread.sleep( 15L * 1000L);
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
-
QuartzManager.removeJob( "job1");
-
//QuartzManager.removeJob("job1", "jobGooup", "trigger1", "triggerGroup");
-
System.out.println( "刪除任務");
-
-
try {
-
Thread.sleep( 5L * 1000L);
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
-
System.out.println( "【添加定時任務】");
-
QuartzManager.addJob( "job1", MyJob.class, corn);
-
//QuartzManager.shutdownJobs();
-
//System.out.println("中止全部任務");
-
try {
-
Thread.sleep( 5L * 1000L);
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
-
System.out.println( "【暫停定時任務】");
-
QuartzManager.pauseJob( "job1");
-
System.out.println( "【當即運行一次】");
-
QuartzManager.triggerJob( "job1");
-
try {
-
Thread.sleep( 5L * 1000L);
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
-
System.out.println( "【恢復定時任務】");
-
QuartzManager.resumeJob( "job1");
-
try {
-
Thread.sleep( 5L * 1000L);
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
-
QuartzManager.shutdownJobs();
-
}
-
}