public void endpointActivation(MessageEndpointFactory endpointFactory, ActivationSpec spec) throws ResourceException { log.debug("endpointActivation, spec=" + spec); QuartzActivationSpec quartzSpec = (QuartzActivationSpec) spec; // allocate instance of endpoint to figure out its endpoint interface Class clazz = QuartzJob.class; MessageEndpoint tmpMe = endpointFactory.createEndpoint(null); if (tmpMe instanceof StatefulJob) clazz = StatefulQuartzJob.class; tmpMe.release(); try { JobDetail jobDetail = new JobDetail( quartzSpec.getJobName(), quartzSpec.getJobGroup(), clazz, true, false, false); jobDetail.getJobDataMap().setAllowsTransientData(true); jobDetail.getJobDataMap().put("endpointFactory", endpointFactory); log.debug("adding job: " + quartzSpec); CronTrigger trigger = new CronTrigger( quartzSpec.getTriggerName(), quartzSpec.getTriggerGroup(), quartzSpec.getCronTrigger()); sched.scheduleJob(jobDetail, trigger); } catch (Exception e) { log.error(e); throw new ResourceException(e); } }
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."); }
/** * Refreshes the job schedule with the current list of enabled jobs of the type to be scheduled. * * @param scheduler The scheduler to update. * @return The updated scheduler. */ protected Scheduler _refreshJobSchedule(Scheduler scheduler) { _disposeScheduler(scheduler); String schedulerName = null; Properties props = new Properties(); // Set quartz worker thread properties props.put( QUARTZ_THREADPOOL_COUNT, _configuration.getValue( Property.QUARTZ_THREADPOOL_COUNT.getName(), Property.QUARTZ_THREADPOOL_COUNT.getDefaultValue())); props.put(QUARTZ_THREAD_PRIORITY, QUARTZ_THREAD_PRIORITY_VALUE); props.put( StdSchedulerFactory .PROP_SCHED_SCHEDULER_THREADS_INHERIT_CONTEXT_CLASS_LOADER_OF_INITIALIZING_THREAD, true); /* Have multiple scheduler instances for different job types, so that when * we stop the previous instance of a scheduler during the refresh cycle it does not affect another scheduler. */ switch (Thread.currentThread().getName()) { case "schedule-alerts": default: schedulerName = "AlertScheduler"; } props.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, schedulerName); SchedulerFactory schedulerFactory; Scheduler result = null; try { schedulerFactory = new StdSchedulerFactory(props); result = schedulerFactory.getScheduler(); } catch (Exception e) { _logger.error("Exception in setting up scheduler: {}", e); return result; } for (CronJob job : getEnabledJobs()) { _logger.debug("Adding job to scheduler: {}", job); try { // Convert from linux cron to quartz cron expression String quartzCronEntry = "0 " + job.getCronEntry().substring(0, job.getCronEntry().length() - 1) + "?"; JobDetail jobDetail = JobBuilder.newJob(RunnableJob.class).build(); CronTrigger cronTrigger = TriggerBuilder.newTrigger() .withSchedule(CronScheduleBuilder.cronSchedule(quartzCronEntry)) .build(); // Pass parameter to quartz worker threads jobDetail.getJobDataMap().put(RunnableJob.CRON_JOB, job); jobDetail.getJobDataMap().put(RunnableJob.LOCK_TYPE, lockType); jobDetail.getJobDataMap().put("AlertService", _alertService); jobDetail.getJobDataMap().put("AuditService", _auditService); result.scheduleJob(jobDetail, cronTrigger); } catch (Exception ex) { String msg = "Failed to schedule job {0} : {1}"; JPAEntity entity = JPAEntity.class.cast(job); _auditService.createAudit(msg, entity, entity, ex.getMessage()); _logger.error("Failed to schedule job {} : {}", job, ex.getMessage()); } } try { result.start(); } catch (SchedulerException e) { _logger.error("Exception in starting scheduler: {}", e); } _logger.info("Job schedule refreshed."); return result; }