packagecom.imaster.admin.web.schemajob;importcom.imaster.admin.model.vo.TaskVo;importcom.imaster.admin.shared.service.version2.ExhibitorReserveExhibitionService;importcom.imaster.constant.ApplicationConfig;importcom.imaster.shared.enums.TaskTypeEnum;importcom.imaster.shared.model.ExhibitorReserveExhibition;importorg.quartz.JobDataMap;importorg.quartz.JobDetail;importorg.quartz.Scheduler;importorg.quartz.SchedulerException;importorg.slf4j.Logger;importorg.springframework.beans.BeansException;importorg.springframework.beans.factory.BeanFactory;importorg.springframework.beans.factory.BeanFactoryAware;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.scheduling.quartz.CronTriggerBean;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importjava.util.Map;/*** @Author wanghh
* 2020/5/9 11:45*/
public class QuartzManager implementsBeanFactoryAware {private static final Logger logger =org.slf4j.LoggerFactory.getLogger(ApplicationConfig.SERVER_NAME);privateScheduler scheduler;private staticBeanFactory beanFactory;
@Autowired
ExhibitorReserveExhibitionService reserveExhibitionService;/*** 定时要执行的方法类。*/
public voidreScheduleJob() {//1.读取数据库中的任务列表。//获取短信定时任务
List list = reserveExhibitionService.selectList(null);for(ExhibitorReserveExhibition item : list
) {
TaskVo taskVo= newTaskVo();
taskVo.setTaskId(item.getId());
taskVo.setTaskType(TaskTypeEnum.EXHIBITOR_RESERVE.getCode());
taskVo.setCronExpression(String.format("0 %s %s * * ? ",item.getAcceptEmailMinute(),item.getAcceptEmailHour()));if (item.getDeleted() == 1 || item.getEnabled() == 0||item.getAcceptEmailRuleTime()==0) {//去掉过时的任务
removeExpireTasks(taskVo);
}else{
configSchedul(taskVo);
}
}
}/*** 移除过期任务
*
*@parambo*/
private voidremoveExpireTasks(TaskVo bo) {try{
CronTriggerBean trigger= (CronTriggerBean) scheduler.getTrigger(bo.getTaskId() + "name" +bo.getTaskType(), Scheduler.DEFAULT_GROUP);if (trigger != null) {
logger.info("==移除任务==" + bo.getTaskId() + "name" +bo.getTaskType());
scheduler.pauseTrigger(trigger.getName(), trigger.getGroup());//停止触发器
scheduler.unscheduleJob(trigger.getName(), trigger.getGroup());//移除触发器
scheduler.deleteJob(trigger.getJobName(), trigger.getJobGroup());//删除任务
}
}catch(SchedulerException e) {
logger.error("移除任务失败...");
e.printStackTrace();
}
}/*** 配置任务列表
*
*@parambo*/
private voidconfigSchedul(TaskVo bo) {try{
CronTriggerBean trigger= (CronTriggerBean) scheduler.getTrigger(bo.getTaskId() + "name" +bo.getTaskType(), Scheduler.DEFAULT_GROUP);if (trigger == null) {//说明schedule中不存在该定时任务
createTriggerTask(bo);
}else{
updateTriggerTask(bo, trigger);
}
}catch(SchedulerException e) {
logger.error("获取触发器trigger失败...");
e.printStackTrace();
}
}/*** 更新任务列表
*
*@parambo*/
private voidupdateTriggerTask(TaskVo bo, CronTriggerBean trigger) {try{//判断从DB中取得的任务时间和现在的quartz线程中的任务时间是否相等//如果相等,则表示用户并没有重新设定数据库中的任务时间,这种情况不需要重新rescheduleJob
if (trigger.getCronExpression() != null &&
!trigger.getCronExpression().equalsIgnoreCase(bo.getCronExpression())) {
logger.info("=真正更新方法:=" + bo.getTaskId() + "name" +bo.getTaskType());
trigger.setCronExpression(bo.getCronExpression());
scheduler.rescheduleJob(bo.getTaskId()+ "name" +bo.getTaskType(), Scheduler.DEFAULT_GROUP, trigger);
logger.info("更新任务时间失败...");
}
}catch(Exception e) {
logger.error("更新任务时间失败...");
e.printStackTrace();
}
}/*** 创建任务列表
*
*@parambo*/
private voidcreateTriggerTask(TaskVo bo) {
logger.info("=创建:=" + bo.getTaskId() + "name" +bo.getTaskType());try{
Class clazz= QuartzJobFactory.class;//执行计划任务的类
JobDetail jobDetail = new JobDetail(bo.getTaskId() + "", clazz);
Map map= newHashMap();
map.put("task", bo);
jobDetail.setJobDataMap(newJobDataMap(map));
jobDetail.setName(bo.getTaskId()+ "name" +bo.getTaskType());
scheduler.addJob(jobDetail,true); //将Job添加到管理类//新一个基于Spring的时间类
CronTriggerBean c = newCronTriggerBean();
c.setCronExpression(bo.getCronExpression());//设置时间表达式
c.setName(bo.getTaskId() + "name" + bo.getTaskType());//设置名称
c.setJobDetail(jobDetail);//注入Job
c.setJobName(bo.getTaskId() + "name" + bo.getTaskType());//设置Job名称
scheduler.scheduleJob(c);//注入到管理类
scheduler.rescheduleJob(bo.getTaskId() + "name" + bo.getTaskType(), Scheduler.DEFAULT_GROUP, c);//刷新管理类
} catch(Exception e) {
logger.error("创建" + bo.getTaskId() + "name" + bo.getTaskType() + "任务失败...");
e.printStackTrace();
}
}
@Overridepublic void setBeanFactory(BeanFactory beanFactory) throwsBeansException {
QuartzManager.beanFactory=beanFactory;
}publicScheduler getScheduler() {returnscheduler;
}publicQuartzManager setScheduler(Scheduler scheduler) {this.scheduler =scheduler;return this;
}public staticBeanFactory getBeanFactory() {returnbeanFactory;
}
}