private void scheduleAllJobsWithOnAnnotation() throws SchedulerException { List<Class<? extends Job>> onJobClasses = getJobClasses(On.class); log.info("Jobs with @On annotation: " + onJobClasses); for (Class<? extends org.quartz.Job> clazz : onJobClasses) { On annotation = clazz.getAnnotation(On.class); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(annotation.value()); Trigger trigger = TriggerBuilder.newTrigger().withSchedule(scheduleBuilder).build(); JobBuilder jobBuilder = JobBuilder.newJob(clazz); scheduler.scheduleJob(jobBuilder.build(), trigger); } }
private void scheduleAllJobsWithEveryAnnotation() throws SchedulerException { List<Class<? extends Job>> everyJobClasses = getJobClasses(Every.class); log.info("Jobs with @Every annotation: " + everyJobClasses); for (Class<? extends org.quartz.Job> clazz : everyJobClasses) { Every annotation = clazz.getAnnotation(Every.class); int secondDelay = TimeParserUtil.parseDuration(annotation.value()); SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(secondDelay).repeatForever(); Trigger trigger = TriggerBuilder.newTrigger().withSchedule(scheduleBuilder).build(); JobBuilder jobBuilder = JobBuilder.newJob(clazz); scheduler.scheduleJob(jobBuilder.build(), trigger); } }
private void startJobs() { try { // Get a new scheduler scheduler = new StdSchedulerFactory().getScheduler(); // Start it up. This won't start any jobs though. scheduler.start(); for (GuanxiJobConfig gxJob : gxJobs) { // Need a new JobDetail to hold custom data to send to the job we're controlling JobDetail jobDetail = new JobDetail( gxJob.getKey(), Scheduler.DEFAULT_GROUP, Class.forName(gxJob.getJobClass())); // Create a new JobDataMap for custom data to be sent to the job... JobDataMap jobDataMap = new JobDataMap(); // ...and add the job's custom config object jobDataMap.put(GuanxiJobConfig.JOB_KEY_JOB_CONFIG, gxJob); // Put the job's custom data in it's JobDetail jobDetail.setJobDataMap(jobDataMap); /* Tell the scheduler when this job will run. Nothing will happen * until the start method is called. */ Trigger trigger = new CronTrigger(gxJob.getKey(), Scheduler.DEFAULT_GROUP, gxJob.getCronLine()); // Start the job scheduler.scheduleJob(jobDetail, trigger); if (gxJob.isStartImmediately()) { scheduler.triggerJob(gxJob.getKey(), Scheduler.DEFAULT_GROUP); } } } catch (ClassNotFoundException cnfe) { logger.error("Error locating job class", cnfe); } catch (SchedulerException se) { logger.error("Job scheduling error", se); } catch (ParseException pe) { logger.error("Error parsing job cronline", pe); } }
/** Initialize each job. */ protected void initializeJobs(Session jcrSession) { Collection<KKStoreConfig> kkStoreConfigs = HippoModuleConfig.getConfig().getStoresConfig().values(); if (kkStoreConfigs.size() == 0) { log.error("No store config has been found. Please check the konakart:konakart configuration"); return; } // Get the first one. KKStoreConfig kkStoreConfig = kkStoreConfigs.iterator().next(); try { String triggerName = MASS_SYNC_JOB_TRIGGER; String jobName = MASS_SYNC_JOB_ONCE; JobDetail jobDetail = new JobDetail(jobName, MASS_SYNC_JOB_GROUP, Class.forName(kkStoreConfig.getJobClass())); JobDataMap dataMap = new JobDataMap(); jobDetail.setJobDataMap(dataMap); // Create a trigger that fires immediately, and run only once Trigger trigger = new SimpleTrigger(MASS_SYNC_JOB_TRIGGER_ONCE); trigger.setStartTime(new Date(System.currentTimeMillis() + 10000)); if (!triggerExists(trigger)) { resourceScheduler.scheduleJob(jobDetail, trigger); } jobName = MASS_SYNC_JOB; jobDetail = new JobDetail(jobName, MASS_SYNC_JOB_GROUP, Class.forName(kkStoreConfig.getJobClass())); // Create the cron task. The first launch is scheduled after 30 minutes if (StringUtils.isNotEmpty(kkStoreConfig.getCronExpression())) { trigger = new CronTrigger( triggerName, MASS_SYNC_JOB_TRIGGER_GROUP, jobName, MASS_SYNC_JOB_GROUP, kkStoreConfig.getCronExpression()); trigger.setStartTime(new Date(System.currentTimeMillis() + 1000)); if (triggerExists(trigger)) { if (triggerChanged(trigger)) { resourceScheduler.rescheduleJob( MASS_SYNC_JOB_TRIGGER, MASS_SYNC_JOB_TRIGGER_GROUP, trigger); } } else { resourceScheduler.scheduleJob(jobDetail, trigger); } } } catch (ClassNotFoundException e) { log.error("Failed to find the Job class named - " + kkStoreConfig.getJobClass(), e); } catch (ParseException e) { log.error("Failed to parse the cron expression - " + kkStoreConfig.getCronExpression(), e); } catch (SchedulerException e) { log.error("Failed to start the job", e); } }