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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| @Slf4j @Service public class QuartzServiceImpl implements QuartzService {
@Autowired private Scheduler scheduler;
@PostConstruct public void startScheduler() { try { scheduler.start(); } catch (SchedulerException e) { e.printStackTrace(); } }
@Override public void addSimpleJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int jobTime, int repeatCount, Map<String, Object> jobData) { try { JobDetail jobDetail = JobBuilder.newJob(jobClass) .withIdentity(jobName, jobGroupName) .build(); if (jobData != null && jobData.size() > 0) { jobDetail.getJobDataMap().putAll(jobData); } SimpleScheduleBuilder scheduleBuilder; if (repeatCount < 0) { scheduleBuilder = SimpleScheduleBuilder .repeatSecondlyForever(1) .withIntervalInSeconds(jobTime); } else { scheduleBuilder = SimpleScheduleBuilder .repeatSecondlyForever(1) .withIntervalInSeconds(jobTime) .withRepeatCount(repeatCount); } Trigger trigger = TriggerBuilder.newTrigger() .withIdentity(jobName, jobGroupName) .withSchedule(scheduleBuilder) .startNow().build(); scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) { e.printStackTrace(); throw new RuntimeException("add simple job error!"); } }
@Override public void addCronJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, String cron, Map<String, Object> jobData) { try { JobDetail jobDetail = JobBuilder.newJob(jobClass) .withIdentity(jobName, jobGroupName) .build(); if (jobData != null && jobData.size() > 0) { jobDetail.getJobDataMap().putAll(jobData); } Trigger trigger = TriggerBuilder.newTrigger() .withIdentity(jobName, jobGroupName) .startAt(DateBuilder.futureDate(1, DateBuilder.IntervalUnit.SECOND)) .withSchedule(CronScheduleBuilder.cronSchedule(cron)) .startNow() .build(); scheduler.scheduleJob(jobDetail, trigger); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("add cron job error"); } }
@Override public void updateCronJob(String jobName, String jobGroupName, String cron) { try { TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); trigger = trigger.getTriggerBuilder() .withIdentity(triggerKey) .withSchedule(CronScheduleBuilder.cronSchedule(cron)) .build(); scheduler.rescheduleJob(triggerKey, trigger); } catch (SchedulerException e) { e.printStackTrace(); throw new RuntimeException("update job error"); } }
@Override public void deleteJob(String jobName, String jobGroupName) { try { scheduler.deleteJob(new JobKey(jobName, jobGroupName)); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("delete job error"); } }
@Override public void pauseJob(String jobName, String jobGroupName) { try { JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); scheduler.pauseJob(jobKey); } catch (SchedulerException e) { e.printStackTrace(); throw new RuntimeException("pause job error"); } }
@Override public void resumeJob(String jobName, String jobGroupName) { try { JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); scheduler.resumeJob(jobKey); } catch (SchedulerException e) { e.printStackTrace(); throw new RuntimeException("resume job error"); } }
@Override public void runJobNow(String jobName, String jobGroupName) { try { JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); scheduler.triggerJob(jobKey); } catch (SchedulerException e) { e.printStackTrace(); throw new RuntimeException("run job error"); } }
@Override public List<Map<String, Object>> queryAllJob() { List<Map<String, Object>> jobList; try { Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.anyJobGroup()); jobList = new ArrayList<>(); for (JobKey jobKey : jobKeys) { log.info("maps: {}", scheduler.getJobDetail(jobKey).getJobDataMap().getWrappedMap()); List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey); for (Trigger trigger : triggers) { Map<String, Object> map = new HashMap<>(); map.put("jobName", jobKey.getName()); map.put("jobGroupName", jobKey.getGroup()); map.put("description", "触发器:" + trigger.getKey()); Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey()); map.put("jobStatus", triggerState.name()); if (trigger instanceof CronTrigger) { CronTrigger cronTrigger = (CronTrigger) trigger; String cronExpression = cronTrigger.getCronExpression(); map.put("jobTime", cronExpression); } jobList.add(map); } } } catch (SchedulerException e) { e.printStackTrace(); throw new RuntimeException("query all jobs error"); } return jobList; }
@Override public List<Map<String, Object>> queryRunningJob() { List<Map<String, Object>> jobList; try { List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs(); jobList = new ArrayList<>(executingJobs.size()); for (JobExecutionContext executingJob : executingJobs) { Map<String, Object> map = new HashMap<>(); JobDetail jobDetail = executingJob.getJobDetail(); JobKey jobKey = jobDetail.getKey(); Trigger trigger = executingJob.getTrigger(); map.put("jobName", jobKey.getName()); map.put("jobGroupName", jobKey.getGroup()); map.put("description", "触发器:" + trigger.getKey()); Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey()); map.put("jobStatus", triggerState.name()); if (trigger instanceof CronTrigger) { CronTrigger cronTrigger = (CronTrigger) trigger; String cronExpression = cronTrigger.getCronExpression(); map.put("jobTime", cronExpression); } jobList.add(map); } } catch (SchedulerException e) { e.printStackTrace(); throw new RuntimeException("query run jobs error"); } return jobList; } }
|