package com.topsec.tsm.util;
import org.quartz.*; import org.quartz.impl.StdSchedulerFactory;
import java.text.ParseException; import java.util.Date; import java.util.HashMap; import java.util.Map;
import static org.quartz.CronScheduleBuilder.cronSchedule; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger;
/** * Created by zjx on 2017/8/23. */ public class JobManager { public static final int INTERVAL = 1; public static final int TIMEOUT = 2; public static final int NOW = 3;
public static Map<String, TriggerKey> triggerKeyMap = new HashMap<>(); public static Map<String, JobKey> jobKeyMap = new HashMap<>();
public static Map<String, CycleConfig> cycleTimeMap = new HashMap<>();
//任务调度器 private Scheduler scheduler;
//调度器工厂 private SchedulerFactory schedulerFactory;
//数据监听 private JobManager() throwsSchedulerException { schedulerFactory = new StdSchedulerFactory(); scheduler = schedulerFactory.getScheduler(); }
public SchedulergetScheduler() { return this.scheduler; }
private static JobManager jobManager;
public static JobManager getInstance() throws SchedulerException{ if (jobManager == null) { synchronized ("JobManager"){ if (jobManager== null){ jobManager = new JobManager(); } } } return jobManager; }
public void resumeAll() throws SchedulerException { scheduler.resumeAll(); }
public void shutdown() throws SchedulerException { scheduler.shutdown(); }
public void start() throws SchedulerException { scheduler.start(); }
public boolean isInStandbyMode() throws SchedulerException{ return scheduler.isInStandbyMode(); }
public boolean isShutdown() throwsSchedulerException { return scheduler.isShutdown(); }
public boolean isStarted() throwsSchedulerException { return scheduler.isStarted(); }
public void pauseAll() throws SchedulerException { scheduler.pauseAll(); }
public void standby() throws SchedulerException { scheduler.standby(); }
public void scheduleJob(JobDetail jobDetail, CronTrigger trigger) throws SchedulerException { scheduler.scheduleJob(jobDetail, trigger);
}
public JobDetailgetJobDetail(String uniqueid) throws SchedulerException { return scheduler.getJobDetail(JobKey.jobKey(uniqueid,uniqueid)); }
public void deleteJob(String uniqueid) throwsSchedulerException { boolean result = scheduler.deleteJob(JobKey.jobKey(uniqueid,uniqueid)); if (result){ cycleTimeMap.remove(uniqueid); triggerKeyMap.remove(uniqueid); jobKeyMap.remove(uniqueid); } }
public void pauseJob(String uniqueid) throwsSchedulerException { scheduler.pauseJob(JobKey.jobKey(uniqueid, uniqueid)); }
public void resumeJob(String uniqueid) throwsSchedulerException { scheduler.resumeJob(JobKey.jobKey(uniqueid, uniqueid)); } //设置cron表达式 public StringsetCron(CycleConfig cycleConfig) { String type =cycleConfig.gettype(); String mon =cycleConfig.getmonth(); String week =cycleConfig.getweek(); String day =cycleConfig.getday(); String hour =cycleConfig.gethour(); String min = cycleConfig.getmin(); String str = null; if ("year".equals(type)) { str = "0" + "" + min + " " + hour + " " + day+ " " + mon + " " + "?"+ " " + "*"; } else if ("month".equals(type)) { str = "0" + "" + min + " " + hour + " " + day+ " " + "*" + "" + "?" + "" + "*"; } else if ("week".equals(type)) { str = "0" + "" + min + " " + hour + " " + "?" + "" + "*" + "" + week + " " + "*"; } else if ("day".equals(type)) { str = "0" + "" + min + " " + hour + " " + "*" + "" + "*" + "" + "?" + "" + "*"; } else if ("hour".equals(type)) { str = "0" + "" + min + " " + "*"+ " " + "*"+ " " + "*"+ " " + "?"+ " " + "*"; }else if ("min".equals(type)) { str = "0" + " " +"0/1" + " " + "*"+ " " + "*"+ " " + "*"+ " " + "?"; } return str; }
/** * 周期任务 * @param cycleConfig * @param clazz * @throws ParseException */ public void setTask(CycleConfig cycleConfig, Class<? extends Job> clazz) throws ParseException {
String str =setCron(cycleConfig); JobDetail jobDetail = newJob(clazz).withIdentity(cycleConfig.getUniqueid(),cycleConfig.getUniqueid()).build(); jobDetail.getJobDataMap().put("id", cycleConfig.getUniqueid());
cycleTimeMap.put(cycleConfig.getUniqueid(), cycleConfig); CronScheduleBuilderscheduleBuilder = cronSchedule(str);//"0/5 * * * * ?" CronTrigger trigger = newTrigger().withIdentity(cycleConfig.getUniqueid(),cycleConfig.getUniqueid()).withSchedule(scheduleBuilder).build(); try { scheduler.scheduleJob(jobDetail, trigger); scheduler.start();
triggerKeyMap.put(cycleConfig.getUniqueid(), trigger.getKey()); jobKeyMap.put(cycleConfig.getUniqueid(), jobDetail.getKey()); } catch (Exceptione) { e.printStackTrace(); } }
/** * 定时任务 * * @param id 任务ID * @param clazz 要执行的Job * @param startTime 定时时间 * @throws org.quartz.SchedulerException * @throws ParseException */ public void setTask(String id, Class<? extends Job> clazz, Date startTime) throws SchedulerException,ParseException { JobDetail jobDetail = newJob(clazz).withIdentity(id,id).usingJobData("type", JobManager.TIMEOUT).build(); jobDetail.getJobDataMap().put("id", id); Trigger trigger = newTrigger().withSchedule(simpleSchedule().withRepeatCount(0)) .startAt(startTime).withIdentity(id, id).build(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); triggerKeyMap.put(id, trigger.getKey()); }
/** * 即时任务 * * @param id * @param clazz * @throws org.quartz.SchedulerException * @throws ParseException */ public void setTask(String id, Class<? extendsJob> clazz) throws SchedulerException,ParseException { JobDetail jobDetail = newJob(clazz).withIdentity(id,id).usingJobData("type", JobManager.NOW).build(); jobDetail.getJobDataMap().put("id", id); Trigger trigger = newTrigger().withSchedule(simpleSchedule().withRepeatCount(0)) .startNow().withIdentity(id, id).build(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); }
public boolean stopTask(String projectId) throws ParseException{
TriggerKey triggerKey = null; JobKey jobKey = null; boolean stopFlag = true; try {
SchedulerFactorygSchedulerFactory = new StdSchedulerFactory(); Scheduler scheduler =gSchedulerFactory.getScheduler();
if (triggerKeyMap.containsKey(projectId)) {
triggerKey = triggerKeyMap.get(projectId); scheduler.pauseTrigger(triggerKey);// 停止触发器 stopFlag =scheduler.unscheduleJob(triggerKey);// 移除触发器
return stopFlag;
// //判断是否找到触发器并成功停止 // if(jobKeyMap.containsKey(projectId)&& stopFlag){ // jobKey =jobKeyMap.get(projectId); // //// scheduler.interrupt(jobKey); // stopFlag =scheduler.interrupt(jobKey); // stopFlag =scheduler.deleteJob(jobKey);// 删除任务 // // return stopFlag; // } else { // return false; // } }
} catch (Exceptione) { e.printStackTrace(); } return false; }
} |