Springboot整合quartz

时间:2025-04-05 11:27:04
@Configuration public class JobHandler { @Resource private Scheduler scheduler; /** * 添加任务 */ @SuppressWarnings("unchecked") public void addJob(JobInfo jobInfo) throws SchedulerException, ClassNotFoundException { Objects.requireNonNull(jobInfo, "任务信息不能为空"); // 生成job key JobKey jobKey = JobKey.jobKey(jobInfo.getJobName(), jobInfo.getJobGroup()); // 当前任务不存在才进行添加 if (!scheduler.checkExists(jobKey)) { Class<Job> jobClass = (Class<Job>)Class.forName(jobInfo.getClassName()); // 任务明细 JobDetail jobDetail = JobBuilder .newJob(jobClass) .withIdentity(jobKey) .withIdentity(jobInfo.getJobName(), jobInfo.getJobGroup()) .withDescription(jobInfo.getJobName()) .build(); // 配置信息 jobDetail.getJobDataMap().put("config", jobInfo.getConfig()); // 定义触发器 TriggerKey triggerKey = TriggerKey.triggerKey(jobInfo.getTriggerName(), jobInfo.getTriggerGroup()); // 设置任务的错过机制 Trigger trigger = TriggerBuilder.newTrigger() .withIdentity(triggerKey) .withSchedule(CronScheduleBuilder.cronSchedule(jobInfo.getCron()).withMisfireHandlingInstructionDoNothing()) .build(); scheduler.scheduleJob(jobDetail, trigger); } else { throw new SchedulerException(jobInfo.getJobName() + "任务已存在,无需重复添加"); } } /** * 任务暂停 */ public void pauseJob(String jobGroup, String jobName) throws SchedulerException { JobKey jobKey = JobKey.jobKey(jobName, jobGroup); if (scheduler.checkExists(jobKey)) { scheduler.pauseJob(jobKey); } } /** * 继续任务 */ public void continueJob(String jobGroup, String jobName) throws SchedulerException { JobKey jobKey = JobKey.jobKey(jobName, jobGroup); if (scheduler.checkExists(jobKey)) { scheduler.resumeJob(jobKey); } } /** * 删除任务 */ public boolean deleteJob(String jobGroup, String jobName) throws SchedulerException { JobKey jobKey = JobKey.jobKey(jobName, jobGroup); if (scheduler.checkExists(jobKey)) { // 这里还需要先删除trigger相关 //TriggerKey triggerKey = ((), ()); //() //() return scheduler.deleteJob(jobKey); } return false; } /** * 获取任务信息 */ public JobInfo getJobInfo(String jobGroup, String jobName) throws SchedulerException { JobKey jobKey = JobKey.jobKey(jobName, jobGroup); if (!scheduler.checkExists(jobKey)) { return null; } List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey); if (Objects.isNull(triggers)) { throw new SchedulerException("未获取到触发器信息"); } TriggerKey triggerKey = triggers.get(0).getKey(); Trigger.TriggerState triggerState = scheduler.getTriggerState(triggerKey); JobDetail jobDetail = scheduler.getJobDetail(jobKey); JobInfo jobInfo = new JobInfo(); jobInfo.setJobName(jobGroup); jobInfo.setJobGroup(jobName); jobInfo.setTriggerName(triggerKey.getName()); jobInfo.setTriggerGroup(triggerKey.getGroup()); jobInfo.setClassName(jobDetail.getJobClass().getName()); jobInfo.setStatus(triggerState.toString()); if (Objects.nonNull(jobDetail.getJobDataMap())) { jobInfo.setConfig(JSONObject.toJSONString(jobDetail.getJobDataMap())); } CronTrigger theTrigger = (CronTrigger) triggers.get(0); jobInfo.setCron(theTrigger.getCronExpression()); return jobInfo; } /** * 获取所有任务信息 */ public List<JSONObject> getAllJobInfo() { GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup(); Set<JobKey> jobKeys = null; List<JSONObject> jobList = new ArrayList(); Scheduler scheduler = schedulerFactoryBean.getScheduler(); try { jobKeys = scheduler.getJobKeys(matcher); for (JobKey jobKey : jobKeys) { List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey); for (Trigger trigger : triggers) { JSONObject job = new JSONObject(); job.put("jobDetailName", jobKey.getName()); job.put("setGroupName", jobKey.getGroup()); job.put("setJobCronExpression", trigger.getKey()); Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey()); job.put("setStatus", triggerState.name()); job.put("NextFireTime", trigger.getNextFireTime());//下次触发时间 if (trigger instanceof CronTrigger) { CronTrigger cronTrigger = (CronTrigger) trigger; String cronExpression = cronTrigger.getCronExpression(); job.put("setJobCronExpression", cronExpression); } jobList.add(job); } } } catch (SchedulerException e) { e.printStackTrace(); } return jobList; } }