Java Code Examples for org.quartz.Trigger#getKey()
The following examples show how to use
org.quartz.Trigger#getKey() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptExecution.java From smarthome with Eclipse Public License 2.0 | 6 votes |
/** * helper function to create the timer * * @param instant the point in time when the code should be executed * @param closure string for job id * @param dataMap job data map, preconfigured with arguments * @return */ private static Timer makeTimer(AbstractInstant instant, String closure, JobDataMap dataMap) { Logger logger = LoggerFactory.getLogger(ScriptExecution.class); JobKey jobKey = new JobKey(instant.toString() + ": " + closure.toString()); Trigger trigger = newTrigger().startAt(instant.toDate()).build(); Timer timer = new TimerImpl(jobKey, trigger.getKey(), dataMap, instant); try { JobDetail job = newJob(TimerExecutionJob.class).withIdentity(jobKey).usingJobData(dataMap).build(); if (TimerImpl.scheduler.checkExists(job.getKey())) { TimerImpl.scheduler.deleteJob(job.getKey()); logger.debug("Deleted existing Job {}", job.getKey().toString()); } TimerImpl.scheduler.scheduleJob(job, trigger); logger.debug("Scheduled code for execution at {}", instant.toString()); return timer; } catch (SchedulerException e) { logger.error("Failed to schedule code for execution.", e); return null; } }
Example 2
Source File: TimerImpl.java From smarthome with Eclipse Public License 2.0 | 6 votes |
@Override public boolean reschedule(AbstractInstant newTime) { try { Trigger trigger = newTrigger().startAt(newTime.toDate()).build(); Date nextTriggerTime = scheduler.rescheduleJob(triggerKey, trigger); if (nextTriggerTime == null) { logger.debug("Scheduling a new job job '{}' because the original has already run", jobKey.toString()); JobDetail job = newJob(TimerExecutionJob.class).withIdentity(jobKey).usingJobData(dataMap).build(); TimerImpl.scheduler.scheduleJob(job, trigger); } this.triggerKey = trigger.getKey(); this.cancelled = false; this.terminated = false; return true; } catch (SchedulerException e) { logger.warn("An error occurred while rescheduling the job '{}': {}", jobKey.toString(), e.getMessage()); return false; } }
Example 3
Source File: SchedulerUtil.java From SuitAgent with Apache License 2.0 | 5 votes |
/** * 执行计划任务 * @param job * @param trigger * @return * @throws SchedulerException */ public static ScheduleJobResult executeScheduleJob(JobDetail job, Trigger trigger) throws SchedulerException { ScheduleJobResult scheduleJobResult = new ScheduleJobResult(); //判断是否满足计划任务的创建条件 if(job.getKey() == null || trigger.getKey() == null || job.getJobDataMap() == null){ scheduleJobResult.setScheduleJobStatus(ScheduleJobStatus.FAILED); //不满足计划任务的创建条件,返回scheduleJobResult值类 return scheduleJobResult; } scheduleJobResult.setJobDetail(job); scheduleJobResult.setTrigger(trigger); //开始分配计划任务 Scheduler scheduler = SchedulerFactory.getScheduler(); //开始判断是否存在相同的计划任务 if(scheduler.checkExists(job.getKey())){ log.info("存在相同的计划任务:{}",job.getKey()); scheduler.deleteJob(job.getKey()); scheduleJobResult.setJobKey(job.getKey()); scheduleJobResult.setTriggerKey(trigger.getKey()); scheduleJobResult.setScheduleJobStatus(ScheduleJobStatus.ISEXIST); scheduler.scheduleJob(job,trigger); scheduler.start(); }else{ scheduler.scheduleJob(job,trigger); scheduler.start(); scheduleJobResult.setJobKey(job.getKey()); scheduleJobResult.setTriggerKey(trigger.getKey()); scheduleJobResult.setScheduleJobStatus(ScheduleJobStatus.SUCCESS); } //计划任务分配成功 return scheduleJobResult; }
Example 4
Source File: AbstractTrigger.java From lams with GNU General Public License v2.0 | 5 votes |
/** * <p> * Compare the next fire time of this <code>Trigger</code> to that of * another by comparing their keys, or in other words, sorts them * according to the natural (i.e. alphabetical) order of their keys. * </p> */ public int compareTo(Trigger other) { if(other.getKey() == null && getKey() == null) return 0; if(other.getKey() == null) return -1; if(getKey() == null) return 1; return getKey().compareTo(other.getKey()); }
Example 5
Source File: AbstractTrigger.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Trigger equality is based upon the equality of the TriggerKey. * * @return true if the key of this Trigger equals that of the given Trigger. */ @Override public boolean equals(Object o) { if(!(o instanceof Trigger)) return false; Trigger other = (Trigger)o; return !(other.getKey() == null || getKey() == null) && getKey().equals(other.getKey()); }
Example 6
Source File: QuartzScheduler.java From lams with GNU General Public License v2.0 | 5 votes |
/** * <p> * Schedule the given <code>{@link org.quartz.Trigger}</code> with the * <code>Job</code> identified by the <code>Trigger</code>'s settings. * </p> * * @throws SchedulerException * if the indicated Job does not exist, or the Trigger cannot be * added to the Scheduler, or there is an internal Scheduler * error. */ public Date scheduleJob(Trigger trigger) throws SchedulerException { validateState(); if (trigger == null) { throw new SchedulerException("Trigger cannot be null"); } OperableTrigger trig = (OperableTrigger)trigger; trig.validate(); Calendar cal = null; if (trigger.getCalendarName() != null) { cal = resources.getJobStore().retrieveCalendar(trigger.getCalendarName()); if(cal == null) { throw new SchedulerException( "Calendar not found: " + trigger.getCalendarName()); } } Date ft = trig.computeFirstFireTime(cal); if (ft == null) { throw new SchedulerException( "Based on configured schedule, the given trigger '" + trigger.getKey() + "' will never fire."); } resources.getJobStore().storeTrigger(trig, false); notifySchedulerThread(trigger.getNextFireTime().getTime()); notifySchedulerListenersSchduled(trigger); return ft; }
Example 7
Source File: SchedulerUtil.java From OpenFalcon-SuitAgent with Apache License 2.0 | 5 votes |
/** * 执行计划任务 * @param job * @param trigger * @return * @throws SchedulerException */ public static ScheduleJobResult executeScheduleJob(JobDetail job, Trigger trigger) throws SchedulerException { ScheduleJobResult scheduleJobResult = new ScheduleJobResult(); //判断是否满足计划任务的创建条件 if(job.getKey() == null || trigger.getKey() == null || job.getJobDataMap() == null){ scheduleJobResult.setScheduleJobStatus(ScheduleJobStatus.FAILED); //不满足计划任务的创建条件,返回scheduleJobResult值类 return scheduleJobResult; } scheduleJobResult.setJobDetail(job); scheduleJobResult.setTrigger(trigger); //开始分配计划任务 Scheduler scheduler = SchedulerFactory.getScheduler(); //开始判断是否存在相同的计划任务 if(scheduler.checkExists(job.getKey())){ log.info("存在相同的计划任务:{}",job.getKey()); scheduler.deleteJob(job.getKey()); scheduleJobResult.setJobKey(job.getKey()); scheduleJobResult.setTriggerKey(trigger.getKey()); scheduleJobResult.setScheduleJobStatus(ScheduleJobStatus.ISEXIST); scheduler.scheduleJob(job,trigger); scheduler.start(); }else{ scheduler.scheduleJob(job,trigger); scheduler.start(); scheduleJobResult.setJobKey(job.getKey()); scheduleJobResult.setTriggerKey(trigger.getKey()); scheduleJobResult.setScheduleJobStatus(ScheduleJobStatus.SUCCESS); } //计划任务分配成功 return scheduleJobResult; }
Example 8
Source File: SchedulerAdapter.java From iaf with Apache License 2.0 | 5 votes |
public XmlBuilder triggerToXmlBuilder(Trigger trigger) { XmlBuilder xbRoot = new XmlBuilder("triggerDetail"); TriggerKey triggerKey = trigger.getKey(); xbRoot.addAttribute("fullName", triggerKey.getGroup() + "." + triggerKey.getName()); xbRoot.addAttribute("triggerName", triggerKey.getName()); xbRoot.addAttribute("triggerGroup", triggerKey.getGroup()); String cn = trigger.getCalendarName(); xbRoot.addAttribute("calendarName", (cn == null ? "none" : cn)); xbRoot.addAttribute("endTime", convertDate(trigger.getEndTime())); xbRoot.addAttribute("finalFireTime", convertDate(trigger.getFinalFireTime())); xbRoot.addAttribute("previousFireTime", convertDate(trigger.getPreviousFireTime())); xbRoot.addAttribute("nextFireTime", convertDate(trigger.getNextFireTime())); xbRoot.addAttribute("startTime", convertDate(trigger.getStartTime())); xbRoot.addAttribute("misfireInstruction", Integer.toString(trigger.getMisfireInstruction())); if (trigger instanceof CronTrigger) { xbRoot.addAttribute("triggerType", "cron"); xbRoot.addAttribute("cronExpression", ((CronTrigger)trigger).getCronExpression()); } else if (trigger instanceof SimpleTrigger) { xbRoot.addAttribute("triggerType", "simple"); xbRoot.addAttribute("repeatInterval", ((SimpleTrigger)trigger).getRepeatInterval()); } else { xbRoot.addAttribute("triggerType", "unknown"); } return xbRoot; }
Example 9
Source File: ShowScheduler.java From iaf with Apache License 2.0 | 5 votes |
private List<Map<String, Object>> getJobTriggers(List<? extends Trigger> triggers) throws ApiException { List<Map<String, Object>> jobTriggers = new ArrayList<Map<String, Object>>(); for (Trigger trigger : triggers) { Map<String, Object> triggerDetails = new HashMap<String, Object>(); TriggerKey triggerKey = trigger.getKey(); triggerDetails.put("fullName", triggerKey.getGroup() + "." + triggerKey.getName()); triggerDetails.put("name", triggerKey.getName()); triggerDetails.put("calendarName", trigger.getCalendarName()); putDateProperty(triggerDetails, "endTime", trigger.getEndTime()); putDateProperty(triggerDetails, "finalFireTime", trigger.getFinalFireTime()); putDateProperty(triggerDetails, "nextFireTime", trigger.getNextFireTime()); putDateProperty(triggerDetails, "previousFireTime", trigger.getPreviousFireTime()); putDateProperty(triggerDetails, "startTime", trigger.getStartTime()); triggerDetails.put("misfireInstruction", trigger.getMisfireInstruction()); if (trigger instanceof CronTrigger) { triggerDetails.put("triggerType", "cron"); triggerDetails.put("cronExpression", ((CronTrigger) trigger).getCronExpression()); } else if (trigger instanceof SimpleTrigger) { triggerDetails.put("triggerType", "simple"); triggerDetails.put("repeatInterval", ((SimpleTrigger) trigger).getRepeatInterval()); } else { triggerDetails.put("triggerType", "unknown"); } jobTriggers.add(triggerDetails); } return jobTriggers; }
Example 10
Source File: QuartzScheduler.java From lams with GNU General Public License v2.0 | 4 votes |
/** * <p> * Add the <code>{@link org.quartz.Job}</code> identified by the given * <code>{@link org.quartz.JobDetail}</code> to the Scheduler, and * associate the given <code>{@link org.quartz.Trigger}</code> with it. * </p> * * <p> * If the given Trigger does not reference any <code>Job</code>, then it * will be set to reference the Job passed with it into this method. * </p> * * @throws SchedulerException * if the Job or Trigger cannot be added to the Scheduler, or * there is an internal Scheduler error. */ public Date scheduleJob(JobDetail jobDetail, Trigger trigger) throws SchedulerException { validateState(); if (jobDetail == null) { throw new SchedulerException("JobDetail cannot be null"); } if (trigger == null) { throw new SchedulerException("Trigger cannot be null"); } if (jobDetail.getKey() == null) { throw new SchedulerException("Job's key cannot be null"); } if (jobDetail.getJobClass() == null) { throw new SchedulerException("Job's class cannot be null"); } OperableTrigger trig = (OperableTrigger)trigger; if (trigger.getJobKey() == null) { trig.setJobKey(jobDetail.getKey()); } else if (!trigger.getJobKey().equals(jobDetail.getKey())) { throw new SchedulerException( "Trigger does not reference given job!"); } trig.validate(); Calendar cal = null; if (trigger.getCalendarName() != null) { cal = resources.getJobStore().retrieveCalendar(trigger.getCalendarName()); } Date ft = trig.computeFirstFireTime(cal); if (ft == null) { throw new SchedulerException( "Based on configured schedule, the given trigger '" + trigger.getKey() + "' will never fire."); } resources.getJobStore().storeJobAndTrigger(jobDetail, trig); notifySchedulerListenersJobAdded(jobDetail); notifySchedulerThread(trigger.getNextFireTime().getTime()); notifySchedulerListenersSchduled(trigger); return ft; }