quartz 任务高度的动态修改
package com.example.demo.controller;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class QuatzManager {
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
private static String JOB_GROUP_NAME = "MY_JOBGROUP_NAME";
private static String TRIGGER_GROUP_NAME = "MY_TRIGGERGROUP_NAME";
/**
* @param jobName 任务名
* @param cls 任务
* @param time 时间设置,参考quartz说明文档
* @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
*/
@SuppressWarnings({"rawtypes", "unchecked"})
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);
}
}
/**
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param jobClass 任务
* @param time 时间设置,参考quartz说明文档
* @Description: 添加一个定时任务
*/
@SuppressWarnings({"rawtypes", "unchecked"})
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);
}
}
/**
* @param jobName
* @param time
* @Description: 修改一个任务的触发时间(使用默认的任务组名 , 触发器名 , 触发器组名)
*/
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);
}
}
/**
* @param oldJobName :原任务名
* @param jobName
* @param jobclass
* @param cron
* @Description:修改任务,(可以修改任务名,任务类,触发时间) 原理:移除原来的任务,添加新的任务
* @date 2018年5月23日 上午9:13:10
*/
@SuppressWarnings({"rawtypes", "unchecked"})
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);
}
}
/**
* @param triggerName
* @param triggerGroupName
* @param time
* @Description: 修改一个任务的触发时间
*/
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);
}
}
/**
* @param jobName
* @Description 移除一个任务(使用默认的任务组名 , 触发器名 , 触发器组名)
*/
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);
}
}
/**
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
* @Description: 移除一个任务
*/
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);
}
}
/**
* @param jobName
* @Description:暂停一个任务(使用默认组名)
*/
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();
}
}
/**
* @param jobName
* @param jobGroupName
* @Description:暂停一个任务
*/
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();
}
}
/**
* @param jobName
* @Description:恢复一个任务(使用默认组名)
*/
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();
}
}
/**
* @param jobName
* @param jobGroupName
* @Description:恢复一个任务
* @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);
}
}
/**
* @param jobName
* @param jobGroupName
* @Description: 立即运行任务,这里的立即运行,只会运行一次,方便测试时用。
* @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();
}
}
/**
* @param jobName
* @param jobGroupName
* @Description: 立即运行任务,这里的立即运行,只会运行一次,方便测试时用。
* @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();
}
}
/**
* @param jobName 触发器名
* @Description: 获取任务状态
* NONE: 不存在
* NORMAL: 正常
* PAUSED: 暂停
* COMPLETE:完成
* ERROR : 错误
* BLOCKED : 阻塞
* @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();
Trigger.TriggerState triggerState = sched.getTriggerState(triggerKey);
name = triggerState.name();
} catch (SchedulerException e) {
e.printStackTrace();
}
return name;
}
/**
* @Description:
* @date 2018年5月16日
*/
public class MyJob implements Job {
@Override
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 --------");
}
}
}
技术改变一切