1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| package com.byron.quartz; import cn.hutool.core.date.DateUtil; import com.byron.quartz.exception.CustomException; import org.apache.commons.lang3.StringUtils; import org.quartz.*; import org.quartz.impl.matchers.GroupMatcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Set;
@Service public class ScheduleManager {
public static final Logger log = LoggerFactory.getLogger(ScheduleManager.class); @Resource private Scheduler scheduler;
public void startJob(Class<? extends Job> jobClass, String jobName, String jobGroup, Integer type, Date timeConfig, JobDataMap jobDataMap) throws Exception { String jobInfo = getJobInfo(jobName, jobGroup); if (StringUtils.isBlank(jobInfo)) { startJob(scheduler, jobClass, jobName, jobGroup, type, timeConfig, jobDataMap); } else { modifyJob(jobName, jobGroup, type, timeConfig); } }
public String getJobInfo(String name, String group) throws Exception { TriggerKey triggerKey = new TriggerKey(name, group); Trigger trigger = scheduler.getTrigger(triggerKey); if (null == trigger) { return null; } return String.format("time:%s,state:%s", DateUtil.format(trigger.getStartTime(), "yyyy-MM-dd HH:mm:ss"), scheduler.getTriggerState(triggerKey).name()); }
public boolean modifyJob(String name, String group, Integer type, Date timeConfig) throws SchedulerException { TriggerKey triggerKey = new TriggerKey(name, group); Trigger trigger = scheduler.getTrigger(triggerKey); Date oldStartTime = trigger.getStartTime(); Date currentTime = new Date(); if (type == 0) { JobKey jobKey = new JobKey(name, group); scheduler.triggerJob(jobKey); return true; } else { if (timeConfig.after(currentTime) || timeConfig.equals(currentTime)) { Trigger newTrigger = TriggerBuilder.newTrigger() .withIdentity(name, group) .startAt(timeConfig) .build(); scheduler.rescheduleJob(triggerKey, newTrigger); return true; } } return false; }
public void deleteJob(String name, String group) throws Exception { JobKey jobKey = new JobKey(name, group); JobDetail jobDetail = scheduler.getJobDetail(jobKey); if (jobDetail == null) return; scheduler.deleteJob(jobKey); }
private void startJob(Scheduler scheduler, Class<? extends Job> jobClass, String jobName, String jobGroup, Integer type, Date timeConfig, JobDataMap dataMap) throws SchedulerException { JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroup).usingJobData(dataMap).build(); if (type == 0) { Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroup).startNow().build(); scheduler.scheduleJob(jobDetail, trigger); } else { if (timeConfig.after(new Date())) { Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroup).startAt(timeConfig).build(); scheduler.scheduleJob(jobDetail, trigger); } else { throw new CustomException(jobName + "时间已经过期,任务无法执行"); }
}
}
public void pauseJob(String jobName, String group) throws Exception { JobKey jobKey = JobKey.jobKey(jobName, group); JobDetail jobDetail = scheduler.getJobDetail(jobKey); if (jobDetail == null) return; scheduler.pauseJob(jobKey); }
public Object listScheduleJob() throws Exception { GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup(); Set<JobKey> jobKeys = null; List<QuartzJobsVO> jobList = new ArrayList(); try { jobKeys = scheduler.getJobKeys(matcher); for (JobKey jobKey : jobKeys) { List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey); for (Trigger trigger : triggers) { QuartzJobsVO job = new QuartzJobsVO(); job.setJobName(jobKey.getName()); job.setJobGroup(jobKey.getGroup()); job.setJobTrigger("触发器:" + trigger.getKey()); Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey()); job.setStatus(triggerState.name()); if (trigger instanceof SimpleTrigger) { SimpleTrigger simpleTrigger = (SimpleTrigger) trigger; Date startTime = simpleTrigger.getStartTime(); String time = DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss"); job.setTimeOrCronConfig(time); } if (trigger instanceof CronTrigger) { CronTrigger cronTrigger = (CronTrigger) trigger; String cronExpression = cronTrigger.getCronExpression(); job.setTimeOrCronConfig(cronExpression); } jobList.add(job); } }
} catch (SchedulerException e) { e.printStackTrace(); } return jobList; } }
|