根据Quartz2.2.1版本封装的工具类,记录与此用于备忘:
package com.yida.framework.base.util.quartz;
import java.util.ArrayList;
import java.util.List;
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.SchedulerException;
import org.quartz.Trigger.TriggerState;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import com.yida.framework.base.util.AppUtil;
import com.yida.framework.modules.po.Task;
import com.yida.framework.modules.po.TaskGroup;
import com.yida.framework.modules.po.TaskSchedule;
/**
* @ClassName: SchedulerUtils
* @Description: Quartz任务计划调度工具类
* @author Lanxiaowei(736031305@qq.com)
* @date 2015年12月10日 下午4:47:12
*
*/
public class SchedulerUtils {
//Trigger各种状态:
//None:Trigger已经完成,且不会在执行,或者找不到该触发器,或者Trigger已经被删除
//NORMAL:正常状态
//PAUSED:暂停状态
//COMPLETE:触发器完成,但是任务可能还正在执行中
//BLOCKED:线程阻塞状态
//ERROR:出现错误
/**
* @throws ClassNotFoundException
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: addTaskSchedule
* @Description: 添加任务计划
* @param @param taskSchedule
* @param @return
* @param @throws SchedulerException
* @return int
* @throws
*/
public static int addTaskSchedule(TaskSchedule taskSchedule) throws SchedulerException, ClassNotFoundException {
Task task = taskSchedule.getTask();
if(null == task) {
//任务计划尚未绑定任务
return -1;
}
TaskGroup group = task.getGroup();
String groupName = null;
if(null != group) {
groupName = group.getTaskGroupName();
} else {
groupName = BaseTask.DEFAULT_GROUP;
}
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(taskSchedule.getScheduleName(),
groupName);
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
if (null == trigger) {
//这里是任务类的class
JobDetail jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(task.getTaskClass()))
.withIdentity(taskSchedule.getScheduleName(), groupName).build();
String uniqueKey = taskSchedule.getScheduleName() + "#$#" + task.getTaskName()
+ "#$#" + groupName;
jobDetail.getJobDataMap().put(uniqueKey, taskSchedule);
//表达式调度构建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(taskSchedule
.getCronExpression());
trigger = TriggerBuilder.newTrigger().withIdentity(taskSchedule.getScheduleName(), groupName)
.withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
} else {
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(taskSchedule
.getCronExpression());
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)
.withSchedule(scheduleBuilder).build();
scheduler.rescheduleJob(triggerKey, trigger);
}
return 1;
}
/**
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: pauseJob
* @Description: 暂停任务计划
* @param @param taskScheduleName 任务计划名称
* @param @param taskGroupName 任务分组名称
* @param @throws SchedulerException
* @return void
* @throws
*/
public static void pauseJob(String taskScheduleName,String taskGroupName) throws SchedulerException {
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
JobKey jobKey = JobKey.jobKey(taskScheduleName, taskGroupName);
scheduler.pauseJob(jobKey);
}
/**
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: pauseJob
* @Description: 批量暂停任务计划
* @param @param groupMatcher
* @param @throws SchedulerException
* @return void
* @throws
*/
public static void pauseJob(GroupMatcher<JobKey> groupMatcher) throws SchedulerException {
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
//GroupMatcher创建示例,groupContains即任务分组名称中包含指定字符的任务计划全部暂停
//GroupMatcher<JobKey> groupMatcher = GroupMatcher.groupContains("xxxx");
scheduler.pauseJobs(groupMatcher);
}
/**
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: pauseAllJob
* @Description: 暂停所有任务计划
* @param @throws SchedulerException
* @return void
* @throws
*/
public static void pauseAllJob() throws SchedulerException {
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.pauseAll();
}
/**
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: resumeJob
* @Description: 恢复任务计划
* @param @param taskScheduleName 任务计划名称
* @param @param taskGroupName 任务分组名称
* @param @throws SchedulerException
* @return void
* @throws
*/
public static void resumeJob(String taskScheduleName,String taskGroupName) throws SchedulerException {
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
JobKey jobKey = JobKey.jobKey(taskScheduleName, taskGroupName);
scheduler.resumeJob(jobKey);
}
/**
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: resumeAllJob
* @Description: 恢复所有任务计划
* @param @throws SchedulerException
* @return void
* @throws
*/
public static void resumeAllJob() throws SchedulerException {
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.resumeAll();
}
/**
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: deleteJob
* @Description: 删除任务计划
* @param @param taskScheduleName
* @param @param taskGroupName
* @param @throws SchedulerException
* @return void
* @throws
*/
public static void deleteJob(String taskScheduleName,String taskGroupName) throws SchedulerException {
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
JobKey jobKey = JobKey.jobKey(taskScheduleName, taskGroupName);
scheduler.deleteJob(jobKey);
}
/**
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: deleteJob
* @Description: 批量删除任务计划
* @param @param taskScheduleList
* @param @throws SchedulerException
* @return void
* @throws
*/
public static void deleteJob(List<TaskSchedule> taskScheduleList) throws SchedulerException {
if(null == taskScheduleList || taskScheduleList.size() <= 0) {
return;
}
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
List<JobKey> jobKeyList = new ArrayList<JobKey>();
for(TaskSchedule taskSchedule : taskScheduleList) {
Task task = taskSchedule.getTask();
if(null == task) {
continue;
}
String taskGroupName = null;
TaskGroup group = task.getGroup();
if(null != group) {
taskGroupName = group.getTaskGroupName();
} else {
taskGroupName = BaseTask.DEFAULT_GROUP;
}
String taskScheduleName = taskSchedule.getScheduleName();
JobKey jobKey = JobKey.jobKey(taskScheduleName, taskGroupName);
jobKeyList.add(jobKey);
}
scheduler.deleteJobs(jobKeyList);
}
/**
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: cancleJob
* @Description: 取消任务计划
* @param @param taskScheduleName
* @param @param taskGroupName
* @param @throws SchedulerException
* @return void
* @throws
*/
public static void cancleJob(String taskScheduleName,String taskGroupName) throws SchedulerException {
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(taskScheduleName,taskGroupName);
scheduler.unscheduleJob(triggerKey);
}
/**
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: canleJob
* @Description: 批量取消任务计划
* @param @param taskScheduleList
* @param @throws SchedulerException
* @return void
* @throws
*/
public static void canleJob(List<TaskSchedule> taskScheduleList) throws SchedulerException {
if(null == taskScheduleList || taskScheduleList.size() <= 0) {
return;
}
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
List<TriggerKey> triggerKeyList = new ArrayList<TriggerKey>();
for(TaskSchedule taskSchedule : taskScheduleList) {
Task task = taskSchedule.getTask();
if(null == task) {
continue;
}
String taskGroupName = null;
TaskGroup group = task.getGroup();
if(null != group) {
taskGroupName = group.getTaskGroupName();
} else {
taskGroupName = BaseTask.DEFAULT_GROUP;
}
String taskScheduleName = taskSchedule.getScheduleName();
TriggerKey triggerKey = TriggerKey.triggerKey(taskScheduleName,taskGroupName);
triggerKeyList.add(triggerKey);
}
scheduler.unscheduleJobs(triggerKeyList);
}
/**
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: checkJobExist
* @Description: 检测任务计划是否存在
* @param @param taskScheduleName
* @param @param taskGroupName
* @param @return
* @param @throws SchedulerException
* @return boolean
* @throws
*/
public static boolean checkJobExist(String taskScheduleName,String taskGroupName) throws SchedulerException {
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
JobKey jobKey = JobKey.jobKey(taskScheduleName, taskGroupName);
return scheduler.checkExists(jobKey);
}
/**
* @throws SchedulerException
* @Author: Lanxiaowei(736031305@qq.com)
* @Title: returnJobState
* @Description: 返回任务计划的运行状态
* BLOCKED:5
* ERROR:4
* COMPLETE:3
* PAUSED:2
* NORMAL:1
* NONE:0
* @param @param taskScheduleName
* @param @param taskGroupName
* @param @return
* @return int
* @throws
*/
public static int returnJobState(String taskScheduleName,String taskGroupName) throws SchedulerException {
SchedulerFactoryBean schedulerFactory =
(SchedulerFactoryBean)AppUtil.getBean("schedulerFactory");
Scheduler scheduler = schedulerFactory.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(taskScheduleName,taskGroupName);
return scheduler.getTriggerState(triggerKey).ordinal();
}
}