public void run() throws Exception { Log log = LogFactory.getLog(JobExceptionExample.class); log.info("------- Initializing ----------------------"); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete ------------"); log.info("------- Scheduling Jobs -------------------"); // jobs can be scheduled before start() has been called // get a "nice round" time a few seconds in the future... long ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime(); // badJob1 will run every three seconds // this job will throw an exception and refire // immediately JobDetail job = new JobDetail("badJob1", "group1", BadJob1.class); SimpleTrigger trigger = new SimpleTrigger( "trigger1", "group1", new Date(ts), null, SimpleTrigger.REPEAT_INDEFINITELY, 3000L); Date ft = sched.scheduleJob(job, trigger); log.info( job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // badJob2 will run every three seconds // this job will throw an exception and never // refire job = new JobDetail("badJob2", "group1", BadJob2.class); trigger = new SimpleTrigger( "trigger2", "group1", new Date(ts), null, SimpleTrigger.REPEAT_INDEFINITELY, 3000L); ft = sched.scheduleJob(job, trigger); log.info( job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); log.info("------- Starting Scheduler ----------------"); // jobs don't start firing until start() has been called... sched.start(); log.info("------- Started Scheduler -----------------"); try { // sleep for 60 seconds Thread.sleep(60L * 1000L); } catch (Exception e) { } log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.numJobsExecuted() + " jobs."); }
public void run() throws Exception { Logger log = LoggerFactory.getLogger(SimpleTriggerExample.class); log.info("------- Initializing -------------------"); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete --------"); log.info("------- Scheduling Jobs ----------------"); // jobs can be scheduled before sched.start() has been called // get a "nice round" time a few seconds in the future... long ts = TriggerUtils.getNextGivenSecondDate(null, 5).getTime(); sched.start(); log.info("------- Started Scheduler -----------------"); // jobs can also be scheduled after start() has been called... // job7 will repeat 20 times, repeat every 3 sec JobDetail job = new JobDetail("job7", "group1", SimpleJob.class); SimpleTrigger trigger = new SimpleTrigger("trigger7", "group1", "job7", "group1", new Date(ts), null, 30, 2000L); Date ft = sched.scheduleJob(job, trigger); log.info( job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); log.info("------- Waiting 15 seconds... --------------"); try { Thread.sleep(15L * 1000L); } catch (Exception e) { } // jobs can be re-scheduled... // job 7 will run immediately and repeat 10 times for every second log.info("------- Rescheduling... --------------------"); trigger = new SimpleTrigger("trigger7", "group1", "job7", "group1", new Date(), null, 10, 1000L); ft = sched.rescheduleJob("trigger7", "group1", trigger); log.info("job7 rescheduled to run at: " + ft); log.info("------- Waiting five minutes... ------------"); try { Thread.sleep(300L * 1000L); } catch (Exception e) { } log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); // display some stats about the schedule that just ran SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); }
public void run() throws Exception { Logger log = LoggerFactory.getLogger(SimpleTriggerExample.class); log.info("------- Initializing -------------------"); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete --------"); log.info("------- Scheduling Jobs ----------------"); // jobs can be scheduled before sched.start() has been called // get a "nice round" time a few seconds in the future... Date startTime = DateBuilder.nextGivenSecondDate(null, 15); // job1 will only fire once at date/time "ts" JobDetail job = newJob(SimpleJob.class).withIdentity("job1", "group1").build(); SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1").startAt(startTime).build(); // schedule it to run! Date ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job2 will only fire once at date/time "ts" job = newJob(SimpleJob.class).withIdentity("job2", "group1").build(); trigger = (SimpleTrigger) newTrigger().withIdentity("trigger2", "group1").startAt(startTime).build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job3 will run 11 times (run once and repeat 10 more times) // job3 will repeat every 10 seconds job = newJob(SimpleJob.class).withIdentity("job3", "group1").build(); trigger = newTrigger() .withIdentity("trigger3", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(10)) .build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // the same job (job3) will be scheduled by a another trigger // this time will only repeat twice at a 70 second interval trigger = newTrigger() .withIdentity("trigger3", "group2") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(2)) .forJob(job) .build(); ft = sched.scheduleJob(trigger); log.info( job.getKey() + " will [also] run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job4 will run 6 times (run once and repeat 5 more times) // job4 will repeat every 10 seconds job = newJob(SimpleJob.class).withIdentity("job4", "group1").build(); trigger = newTrigger() .withIdentity("trigger4", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(5)) .build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job5 will run once, five minutes in the future job = newJob(SimpleJob.class).withIdentity("job5", "group1").build(); trigger = (SimpleTrigger) newTrigger() .withIdentity("trigger5", "group1") .startAt(futureDate(5, IntervalUnit.MINUTE)) .build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job6 will run indefinitely, every 40 seconds job = newJob(SimpleJob.class).withIdentity("job6", "group1").build(); trigger = newTrigger() .withIdentity("trigger6", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(40).repeatForever()) .build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); log.info("------- Starting Scheduler ----------------"); // All of the jobs have been added to the scheduler, but none of the jobs // will run until the scheduler has been started sched.start(); log.info("------- Started Scheduler -----------------"); // jobs can also be scheduled after start() has been called... // job7 will repeat 20 times, repeat every five minutes job = newJob(SimpleJob.class).withIdentity("job7", "group1").build(); trigger = newTrigger() .withIdentity("trigger7", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInMinutes(5).withRepeatCount(20)) .build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // jobs can be fired directly... (rather than waiting for a trigger) job = newJob(SimpleJob.class).withIdentity("job8", "group1").storeDurably().build(); sched.addJob(job, true); log.info("'Manually' triggering job8..."); sched.triggerJob(jobKey("job8", "group1")); log.info("------- Waiting 30 seconds... --------------"); try { // wait 33 seconds to show jobs Thread.sleep(30L * 1000L); // executing... } catch (Exception e) { } // jobs can be re-scheduled... // job 7 will run immediately and repeat 10 times for every second log.info("------- Rescheduling... --------------------"); trigger = newTrigger() .withIdentity("trigger7", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInMinutes(5).withRepeatCount(20)) .build(); ft = sched.rescheduleJob(trigger.getKey(), trigger); log.info("job7 rescheduled to run at: " + ft); log.info("------- Waiting five minutes... ------------"); try { // wait five minutes to show jobs Thread.sleep(300L * 1000L); // executing... } catch (Exception e) { } log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); // display some stats about the schedule that just ran SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); }
public void run() throws Exception { Logger log = LoggerFactory.getLogger(JobStateExample.class); log.info("------- Initializing -------------------"); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete --------"); log.info("------- Scheduling Jobs ----------------"); // get a "nice round" time a few seconds in the future.... Date startTime = nextGivenSecondDate(null, 10); // job1 will only run 5 times (at start time, plus 4 repeats), every 10 seconds JobDetail job1 = newJob(ColorJob.class).withIdentity("job1", "group1").build(); SimpleTrigger trigger1 = newTrigger() .withIdentity("trigger1", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(4)) .build(); // pass initialization parameters into the job job1.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Green"); job1.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1); // schedule the job to run Date scheduleTime1 = sched.scheduleJob(job1, trigger1); log.info( job1.getKey() + " will run at: " + scheduleTime1 + " and repeat: " + trigger1.getRepeatCount() + " times, every " + trigger1.getRepeatInterval() / 1000 + " seconds"); // job2 will also run 5 times, every 10 seconds JobDetail job2 = newJob(ColorJob.class).withIdentity("job2", "group1").build(); SimpleTrigger trigger2 = newTrigger() .withIdentity("trigger2", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(4)) .build(); // pass initialization parameters into the job // this job has a different favorite color! job2.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Red"); job2.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1); // schedule the job to run Date scheduleTime2 = sched.scheduleJob(job2, trigger2); log.info( job2.getKey().toString() + " will run at: " + scheduleTime2 + " and repeat: " + trigger2.getRepeatCount() + " times, every " + trigger2.getRepeatInterval() / 1000 + " seconds"); log.info("------- Starting Scheduler ----------------"); // All of the jobs have been added to the scheduler, but none of the jobs // will run until the scheduler has been started sched.start(); log.info("------- Started Scheduler -----------------"); log.info("------- Waiting 60 seconds... -------------"); try { // wait five minutes to show jobs Thread.sleep(60L * 1000L); // executing... } catch (Exception e) { // } log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); }
@RequestMapping("/cancelScheduledJob") public String cancelScheduledJob( HttpServletRequest request, HttpServletResponse response, @RequestParam("theJobName") String theJobName, @RequestParam("theJobGroupName") String theJobGroupName, @RequestParam("theTriggerName") String triggerName, @RequestParam("theTriggerGroupName") String triggerGroupName, @RequestParam("redirection") String redirection, ModelMap model) throws SchedulerException { scheduler.getJobDetail(theJobName, theJobGroupName); logger.debug("About to pause the job-->" + theJobName + "Job Group Name -->" + theJobGroupName); SimpleTrigger oldTrigger = (SimpleTrigger) scheduler.getTrigger(triggerName, triggerGroupName); if (oldTrigger != null) { Date startTime = new Date(oldTrigger.getStartTime().getTime() + oldTrigger.getRepeatInterval()); if (triggerGroupName.equals(ExtractController.TRIGGER_GROUP_NAME)) { interruptQuartzJob(scheduler, theJobName, theJobGroupName); } scheduler.pauseJob(theJobName, theJobGroupName); SimpleTrigger newTrigger = new SimpleTrigger(triggerName, triggerGroupName); newTrigger.setJobName(theJobName); newTrigger.setJobGroup(theJobGroupName); newTrigger.setJobDataMap(oldTrigger.getJobDataMap()); newTrigger.setVolatility(false); newTrigger.setRepeatCount(oldTrigger.getRepeatCount()); newTrigger.setRepeatInterval(oldTrigger.getRepeatInterval()); newTrigger.setMisfireInstruction( SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT); newTrigger.setStartTime(startTime); newTrigger.setRepeatInterval(oldTrigger.getRepeatInterval()); scheduler.unscheduleJob( triggerName, triggerGroupName); // these are the jobs which are from extract data and are not not // required to be rescheduled. ArrayList<String> pageMessages = new ArrayList<String>(); if (triggerGroupName.equals(ExtractController.TRIGGER_GROUP_NAME)) { scheduler.rescheduleJob(triggerName, triggerGroupName, newTrigger); pageMessages.add("The Job " + theJobName + " has been cancelled"); } else if (triggerGroupName.equals(XsltTriggerService.TRIGGER_GROUP_NAME)) { JobDetailBean jobDetailBean = new JobDetailBean(); jobDetailBean.setGroup(XsltTriggerService.TRIGGER_GROUP_NAME); jobDetailBean.setName(newTrigger.getName()); jobDetailBean.setJobClass(org.akaza.openclinica.job.XsltStatefulJob.class); jobDetailBean.setJobDataMap(newTrigger.getJobDataMap()); jobDetailBean.setDurability(true); // need durability? jobDetailBean.setVolatility(false); scheduler.deleteJob(theJobName, theJobGroupName); scheduler.scheduleJob(jobDetailBean, newTrigger); pageMessages.add("The Job " + theJobName + " has been rescheduled"); } request.setAttribute("pageMessages", pageMessages); logger.debug("jobDetails>" + scheduler.getJobDetail(theJobName, theJobGroupName)); } sdvUtil.forwardRequestFromController(request, response, "/pages/" + redirection); return null; }
protected SchedulerResponse getScheduledJob(Scheduler scheduler, JobKey jobKey) throws Exception { JobDetail jobDetail = scheduler.getJobDetail(jobKey); if (jobDetail == null) { return null; } SchedulerResponse schedulerResponse = new SchedulerResponse(); JobDataMap jobDataMap = jobDetail.getJobDataMap(); String description = jobDataMap.getString(SchedulerEngine.DESCRIPTION); schedulerResponse.setDescription(description); String destinationName = jobDataMap.getString(SchedulerEngine.DESTINATION_NAME); schedulerResponse.setDestinationName(destinationName); Message message = getMessage(jobDataMap); JobState jobState = getJobState(jobDataMap); message.put(SchedulerEngine.JOB_STATE, jobState); schedulerResponse.setMessage(message); StorageType storageType = StorageType.valueOf(jobDataMap.getString(SchedulerEngine.STORAGE_TYPE)); schedulerResponse.setStorageType(storageType); String jobName = jobKey.getName(); String groupName = jobKey.getGroup(); TriggerKey triggerKey = new TriggerKey(jobName, groupName); Trigger trigger = scheduler.getTrigger(triggerKey); if (trigger == null) { schedulerResponse.setGroupName(groupName); schedulerResponse.setJobName(jobName); return schedulerResponse; } message.put(SchedulerEngine.END_TIME, trigger.getEndTime()); message.put(SchedulerEngine.FINAL_FIRE_TIME, trigger.getFinalFireTime()); message.put(SchedulerEngine.NEXT_FIRE_TIME, trigger.getNextFireTime()); message.put(SchedulerEngine.PREVIOUS_FIRE_TIME, trigger.getPreviousFireTime()); message.put(SchedulerEngine.START_TIME, trigger.getStartTime()); if (trigger instanceof CalendarIntervalTrigger) { CalendarIntervalTrigger calendarIntervalTrigger = CalendarIntervalTrigger.class.cast(trigger); IntervalUnit intervalUnit = calendarIntervalTrigger.getRepeatIntervalUnit(); schedulerResponse.setTrigger( new IntervalTrigger( jobName, groupName, calendarIntervalTrigger.getStartTime(), calendarIntervalTrigger.getEndTime(), calendarIntervalTrigger.getRepeatInterval(), TimeUnit.valueOf(intervalUnit.name()))); } else if (trigger instanceof CronTrigger) { CronTrigger cronTrigger = CronTrigger.class.cast(trigger); schedulerResponse.setTrigger( new com.liferay.portal.kernel.scheduler.CronTrigger( jobName, groupName, cronTrigger.getStartTime(), cronTrigger.getEndTime(), cronTrigger.getCronExpression())); } else if (trigger instanceof SimpleTrigger) { SimpleTrigger simpleTrigger = SimpleTrigger.class.cast(trigger); schedulerResponse.setTrigger( new IntervalTrigger( jobName, groupName, simpleTrigger.getStartTime(), simpleTrigger.getEndTime(), (int) simpleTrigger.getRepeatInterval(), TimeUnit.MILLISECOND)); } return schedulerResponse; }