| | |
| | | import com.doumee.dao.timer.entity.QuartzJob; |
| | | import org.quartz.*; |
| | | import org.quartz.CronTrigger; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | |
| | | */ |
| | | @Component |
| | | public class QuartzManage { |
| | | |
| | | private static final Logger log = LoggerFactory.getLogger(QuartzManage.class); |
| | | |
| | | private static final String SCHEDULE_NAME = "BOOT_JOB_" ; |
| | | |
| | |
| | | public CronTrigger getCronTrigger (Integer jobId){ |
| | | try { |
| | | return (CronTrigger) this.scheduler.getTrigger(getTriggerKey(jobId)) ; |
| | | } catch (SchedulerException e){ |
| | | throw new RuntimeException("getCronTrigger Fail",e) ; |
| | | }catch (Exception e){ |
| | | log.error("getCronTrigger失败, jobId={}", jobId, e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | |
| | | JobDetail jobDetail = JobBuilder.newJob(QuartzRecord.class) |
| | | .withIdentity(getJobKey(quartzJob.getId())).build() ; |
| | | |
| | | |
| | | // 构建Cron调度器 |
| | | CronScheduleBuilder scheduleBuilder = CronScheduleBuilder |
| | | .cronSchedule(quartzJob.getCronExpres()) |
| | | .withMisfireHandlingInstructionDoNothing() ; |
| | | .withMisfireHandlingInstructionFireAndProceed() ; |
| | | |
| | | // 任务触发器 |
| | | CronTrigger trigger = TriggerBuilder.newTrigger() |
| | | .withIdentity(getTriggerKey(quartzJob.getId())) |
| | |
| | | scheduler.scheduleJob(jobDetail,trigger) ; |
| | | // 状态校验 |
| | | checkStop(quartzJob) ; |
| | | } catch (SchedulerException e){ |
| | | throw new RuntimeException("createJob Fail",e) ; |
| | | } catch (Exception e){ |
| | | log.error("createJob失败, jobId={}", quartzJob.getId(), e); |
| | | } |
| | | } |
| | | |
| | |
| | | // 构建Cron调度器 |
| | | CronScheduleBuilder scheduleBuilder = CronScheduleBuilder |
| | | .cronSchedule(quartzJob.getCronExpres()) |
| | | .withMisfireHandlingInstructionDoNothing(); |
| | | .withMisfireHandlingInstructionFireAndProceed(); |
| | | // 任务触发器 |
| | | CronTrigger trigger = getCronTrigger(quartzJob.getId()) |
| | | .getTriggerBuilder().withIdentity(triggerKey) |
| | |
| | | scheduler.rescheduleJob(triggerKey, trigger); |
| | | // 状态校验 |
| | | checkStop(quartzJob) ; |
| | | } catch (SchedulerException e) { |
| | | throw new RuntimeException("updateJob Fail",e) ; |
| | | }catch (Exception e){ |
| | | log.error("updateJob失败, jobId={}", quartzJob.getId(), e); |
| | | } |
| | | } |
| | | |
| | |
| | | JobDataMap dataMap = new JobDataMap() ; |
| | | dataMap.put(QuartzJob.JOB_PARAM_KEY,quartzJob); |
| | | this.scheduler.triggerJob(getJobKey(quartzJob.getId()),dataMap); |
| | | } catch (SchedulerException e){ |
| | | throw new RuntimeException("run Fail",e) ; |
| | | } catch (Exception e){ |
| | | log.error("run失败, jobId={}", quartzJob.getId(), e); |
| | | } |
| | | } |
| | | |
| | |
| | | if(quartzJob.getState() != JobState.JOB_RUN.getStatus()){ |
| | | this.scheduler.pauseJob(getJobKey(quartzJob.getId())); |
| | | } |
| | | } catch (SchedulerException e){ |
| | | throw new RuntimeException("pauseJob Fail",e) ; |
| | | } catch (Exception e){ |
| | | log.error("checkStop失败, jobId={}", quartzJob.getId(), e); |
| | | } |
| | | } |
| | | |