/** * Produce the <code>Trigger</code>. * * @return a Trigger that meets the specifications of the builder. */ @SuppressWarnings("unchecked") public T build() { if(scheduleBuilder == null) scheduleBuilder = SimpleScheduleBuilder.simpleSchedule(); MutableTrigger trig = scheduleBuilder.build(); trig.setCalendarName(calendarName); trig.setDescription(description); trig.setStartTime(startTime); trig.setEndTime(endTime); if(key == null) key = new TriggerKey(Key.createUniqueName(null), null); trig.setKey(key); if(jobKey != null) trig.setJobKey(jobKey); trig.setPriority(priority); if(!jobDataMap.isEmpty()) trig.setJobDataMap(jobDataMap); return (T) trig; }
protected void startJob(ScheduleJobEntity jobEntity) { try { if(scheduler.checkExists(createJobKey(jobEntity))) { return; } JobDetail jobDetail = JobBuilder .newJob(DynamicJob.class) .withIdentity(createJobKey(jobEntity)) .setJobData(createJobDataMap(jobEntity.getParameters())) .usingJobData(DynamicJobFactory.JOB_ID_KEY, jobEntity.getId()) .withDescription(jobEntity.getName() + (jobEntity.getRemark() == null ? "" : jobEntity.getRemark())) .build(); MutableTrigger trigger = scheduleTriggerBuilder.buildTrigger(jobEntity.getQuartzConfig()); trigger.setKey(createTriggerKey(jobEntity)); scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) { throw new BusinessException("启动定时调度失败", e); } }
@Override @Transactional(rollbackFor = { SchedulerException.class, ParseException.class }) public void startSniffing(final long snifferId) throws SchedulerException { logger.debug("Starting cron job for sniffer: {}", snifferId); final Sniffer sniffer = snifferPersistence.getSniffer(snifferId); if (sniffer == null) { throw new SchedulerException("Sniffer not found: " + snifferId); } stopAndDeleteAllSnifferJobs(sniffer.getId()); MutableTrigger trigger; try { trigger = CronScheduleBuilder.cronScheduleNonvalidatedExpression(sniffer.getScheduleCronExpression()) .withMisfireHandlingInstructionDoNothing().build(); } catch (final ParseException e) { throw new SchedulerException("Failed to parse cron expression", e); } trigger.setKey(getKey(sniffer, sniffer.getLogSourceId())); final JobDetail jobDetail = JobBuilder.newJob(SnifferJob.class).requestRecovery() .withIdentity(getJobKey(sniffer, sniffer.getLogSourceId())).build(); scheduler.scheduleJob(jobDetail, trigger); final ScheduleInfo scheduleInfo = scheduleInfoAccess.getScheduleInfo(snifferId); scheduleInfo.setScheduled(true); scheduleInfoAccess.updateScheduleInfo(snifferId, scheduleInfo); logger.info("Scheduled cron job for sniffer {} and log source {} with trigger {}", sniffer, sniffer.getLogSourceId(), trigger); }
/** * Build the actual Trigger -- NOT intended to be invoked by end users, * but will rather be invoked by a TriggerBuilder which this * ScheduleBuilder is given to. * * @see TriggerBuilder#withSchedule(ScheduleBuilder) */ @Override public MutableTrigger build() { SimpleTriggerImpl st = new SimpleTriggerImpl(); st.setRepeatInterval(interval); st.setRepeatCount(repeatCount); st.setMisfireInstruction(misfireInstruction); return st; }
private Map<JobKey, List<MutableTrigger>> buildTriggersByFQJobNameMap(List<MutableTrigger> triggers) { Map<JobKey, List<MutableTrigger>> triggersByFQJobName = new HashMap<JobKey, List<MutableTrigger>>(); for(MutableTrigger trigger: triggers) { List<MutableTrigger> triggersOfJob = triggersByFQJobName.get(trigger.getJobKey()); if(triggersOfJob == null) { triggersOfJob = new LinkedList<MutableTrigger>(); triggersByFQJobName.put(trigger.getJobKey(), triggersOfJob); } triggersOfJob.add(trigger); } return triggersByFQJobName; }
/** * Build the actual Trigger -- NOT intended to be invoked by end users, but * will rather be invoked by a TriggerBuilder which this ScheduleBuilder is * given to. * * @see TriggerBuilder#withSchedule(ScheduleBuilder) */ @Override public MutableTrigger build() { CronTriggerImpl ct = new CronTriggerImpl(); ct.setCronExpression(cronExpression); ct.setTimeZone(cronExpression.getTimeZone()); ct.setMisfireInstruction(misfireInstruction); return ct; }
/** * Build the actual Trigger -- NOT intended to be invoked by end users, * but will rather be invoked by a TriggerBuilder which this * ScheduleBuilder is given to. * * @see TriggerBuilder#withSchedule(ScheduleBuilder) */ @Override public MutableTrigger build() { DailyTimeIntervalTriggerImpl st = new DailyTimeIntervalTriggerImpl(); st.setRepeatInterval(interval); st.setRepeatIntervalUnit(intervalUnit); st.setMisfireInstruction(misfireInstruction); st.setRepeatCount(repeatCount); if(daysOfWeek != null) st.setDaysOfWeek(daysOfWeek); else st.setDaysOfWeek(ALL_DAYS_OF_THE_WEEK); if(startTimeOfDay != null) st.setStartTimeOfDay(startTimeOfDay); else st.setStartTimeOfDay(TimeOfDay.hourAndMinuteOfDay(0, 0)); if(endTimeOfDay != null) st.setEndTimeOfDay(endTimeOfDay); else st.setEndTimeOfDay(TimeOfDay.hourMinuteAndSecondOfDay(23, 59, 59)); return st; }
/** * Build the actual Trigger -- NOT intended to be invoked by end users, * but will rather be invoked by a TriggerBuilder which this * ScheduleBuilder is given to. * * @see TriggerBuilder#withSchedule(ScheduleBuilder) */ @Override public MutableTrigger build() { CalendarIntervalTriggerImpl st = new CalendarIntervalTriggerImpl(); st.setRepeatInterval(interval); st.setRepeatIntervalUnit(intervalUnit); st.setMisfireInstruction(misfireInstruction); st.setTimeZone(timeZone); st.setPreserveHourOfDayAcrossDaylightSavings(preserveHourOfDayAcrossDaylightSavings); st.setSkipDayIfHourDoesNotExist(skipDayIfHourDoesNotExist); return st; }
@Override public MutableTrigger buildTrigger(String config) { JSONObject configObj = JSON.parseObject(config); switch (configObj.getString("type")) { case "cron": String cron = configObj.getString("config"); return CronScheduleBuilder.cronSchedule(cron) .build(); default: throw new UnsupportedOperationException(config); } }
@Override @JsonIgnore public ScheduleBuilder getScheduleBuilder() { return new ScheduleBuilder<SimpleTrigger>() { @Override protected MutableTrigger build() { return SimpleScheduleBuilder.simpleSchedule() .withRepeatCount(repeatCount) .withIntervalInMilliseconds(repeatInterval) .withMisfireHandlingInstructionNextWithRemainingCount() .build(); } }; }
/** * Build the actual Trigger -- NOT intended to be invoked by end users, * but will rather be invoked by a TriggerBuilder which this * ScheduleBuilder is given to. */ @Override public MutableTrigger build() { PeriodIntervalTriggerImpl pt = new PeriodIntervalTriggerImpl(); pt.setRepeatPeriod(repeatPeriod); pt.setMisfireInstruction(misfireInstruction); return pt; }
protected void addTriggerToSchedule(MutableTrigger trigger) { loadedTriggers.add(trigger); }
@Override protected MutableTrigger build() { OnDemandTrigger onDemandTrigger = new OnDemandTrigger(); onDemandTrigger.setMisfireInstruction(MISFIRE_INSTRUCTION_FIRE_NOW); return onDemandTrigger; }
/** * Returns a <code>List</code> of triggers loaded from the xml file. * <p/> * * @return a <code>List</code> of triggers. */ protected List<MutableTrigger> getLoadedTriggers() { return Collections.unmodifiableList(loadedTriggers); }
protected abstract MutableTrigger build();
MutableTrigger buildTrigger(String config);