@SuppressWarnings({ "unchecked", "rawtypes" }) private Trigger buildTrigger(String confId, JobScheduling conf, JobKey job) { TriggerKey key = this.buildTiggerKey(confId); TriggerBuilder trigger = TriggerBuilder.newTrigger().withIdentity(key); // Set CRON value if (StringUtils.isNotEmpty(conf.getValue())) { trigger.withSchedule(CronScheduleBuilder.cronSchedule(conf.getValue())); } // Set DATE value if (conf.getStartAt() > 0) { trigger.startAt(new Date(conf.getStartAt())); } if (job != null) { trigger.forJob(job); } return trigger.build(); }
/** * The user may configure this binding to update the internal clock of * FHT80b devices via rf command. The method takes care of scheduling this * job. */ private JobKey scheduleJob(Class<? extends Job> jobClass, String cronExpression) { JobKey jobKey = null; try { Scheduler sched = StdSchedulerFactory.getDefaultScheduler(); JobDetail detail = JobBuilder.newJob(jobClass).withIdentity("FHT "+jobClass.getSimpleName(), "cul").build(); detail.getJobDataMap().put(FHTBinding.class.getName(), this); CronTrigger trigger = TriggerBuilder.newTrigger().forJob(detail) .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build(); jobKey = detail.getKey(); sched.scheduleJob(detail, trigger); } catch (SchedulerException e) { logger.error("Can't schedule time update job", e); } return jobKey; }
@Override public void create(ScheduleJob scheduleJob) throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); Class<? extends Job> jobClass = null; try { jobClass = (Class<? extends Job>) Class.forName(scheduleJob.getJobClassName()); } catch (ClassNotFoundException e) { throw new SchedulerException(e); } if (null != jobClass) { JobDetail jobDetail = JobBuilder.newJob(jobClass) .withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroupName()) .withDescription(scheduleJob.getJobDescription()).build(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity(scheduleJob.getTriggerName(), scheduleJob.getTriggerGroupName()) .withDescription(scheduleJob.getTriggerDescription()) .withSchedule(CronScheduleBuilder.cronSchedule(scheduleJob.getTriggerCronExpression())).startNow() .build(); scheduler.scheduleJob(jobDetail, trigger); } }
/** * 增加一个调度任务(cron版) * * @param name * 任务名称 * @param job * 执行内容 * @param cronExpression * cron表达式 * @throws SchedulerException */ public Trigger addSchedule(String name, Class<? extends Job> task, String cronExpression, JobDataMap param) throws SchedulerException { Scheduler sched = SF.getScheduler(); JobBuilder builder = JobBuilder.newJob(task); builder.withIdentity(name, Scheduler.DEFAULT_GROUP); if (param != null) { builder.usingJobData(param); } Trigger trigger = TriggerBuilder.newTrigger().withIdentity(name, Scheduler.DEFAULT_GROUP) .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build(); sched.scheduleJob(builder.build(), trigger); if (!sched.isShutdown()) sched.start(); return trigger; }
/** * Persist the trigger in the Quartz scheduler. */ private VmSchedule persistTrigger(final VmSchedule schedule) throws SchedulerException { // The trigger for the common VM Job will the following convention : // schedule.id-subscription.id final String id = VmJob.format(schedule); final JobDetailImpl object = (JobDetailImpl) vmJobDetailFactoryBean.getObject(); object.getJobDataMap().put("vmServicePlugin", this); final Trigger trigger = TriggerBuilder.newTrigger().withIdentity(id, SCHEDULE_TRIGGER_GROUP) .withSchedule(CronScheduleBuilder.cronSchedule(schedule.getCron()).inTimeZone(DateUtils.getApplicationTimeZone())) .forJob(object).usingJobData("subscription", schedule.getSubscription().getId()) .usingJobData("operation", schedule.getOperation().name()).usingJobData("schedule", schedule.getId()).build(); // Add this trigger vmSchedulerFactoryBean.getObject().scheduleJob(trigger); return schedule; }
/** * Get a {@link ScheduleBuilder} that is configured to produce a * schedule identical to this trigger's schedule. * * @see #getTriggerBuilder() */ @Override public ScheduleBuilder<CronTrigger> getScheduleBuilder() { CronScheduleBuilder cb = CronScheduleBuilder.cronSchedule(getCronExpression()) .inTimeZone(getTimeZone()); switch(getMisfireInstruction()) { case MISFIRE_INSTRUCTION_DO_NOTHING : cb.withMisfireHandlingInstructionDoNothing(); break; case MISFIRE_INSTRUCTION_FIRE_ONCE_NOW : cb.withMisfireHandlingInstructionFireAndProceed(); break; } return cb; }
/** * @Description: 添加一个定时任务 * @param jobName 任务名 * @param jobGroupName 任务组名 * @param triggerName 触发器名 * @param triggerGroupName 触发器组名 * @param jobClass 任务 * @param time 时间设置,参考quartz说明文档 * */ @SuppressWarnings("unchecked") public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); JobDetail job = newJob(jobClass) .withIdentity(jobName, jobGroupName) .build(); // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(time); // 按新的cronExpression表达式构建一个新的trigger Trigger trigger = TriggerBuilder .newTrigger() .withIdentity(triggerName, triggerGroupName) .withSchedule(scheduleBuilder).build(); sched.scheduleJob(job, trigger); // 启动 if (!sched.isShutdown()) { sched.start(); } } catch (Exception e) { throw new RuntimeException(e); } }
/** * @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名) * @param jobName * @param time * */ @SuppressWarnings("unchecked") public static void modifyJobTime(String jobName, String time) { TriggerKey triggerKey = TriggerKey.triggerKey( jobName, TRIGGER_GROUP_NAME); try { Scheduler sched = gSchedulerFactory.getScheduler(); CronTrigger trigger =(CronTrigger) sched.getTrigger(triggerKey); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { CronScheduleBuilder scheduleBuilder =CronScheduleBuilder.cronSchedule(time); //按新的cronExpression表达式重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build(); //按新的trigger重新设置job执行 sched.rescheduleJob(triggerKey, trigger); } } catch (Exception e) { throw new RuntimeException(e); } }
/** * @Description: 修改一个任务的触发时间 * @param triggerName * @param triggerGroupName * @param time * * */ public static void modifyJobTime(String triggerName, String triggerGroupName, String time) { TriggerKey triggerKey = TriggerKey.triggerKey( triggerName, triggerGroupName); try { Scheduler sched = gSchedulerFactory.getScheduler(); CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { // trigger已存在,则更新相应的定时设置 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder .cronSchedule(time); // 按新的cronExpression表达式重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build(); // 按新的trigger重新设置job执行 sched.resumeTrigger(triggerKey); } } catch (Exception e) { throw new RuntimeException(e); } }
@Transactional @Override public boolean addJobTrigger(ScheduleJobEntity job) { try { Scheduler scheduler = schedulerFactoryBean.getScheduler(); // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder .cronSchedule(job.getCronExpression()); // 按新的cronExpression表达式构建一个新的trigger CronTrigger trigger = newTrigger() .forJob(job.getJobKey()) //绑定job .withIdentity(job.getTriggerKey()) .startAt(job.getStartDate()) // job开始日期 .endAt(job.getEndDate())// job结束日期 .withSchedule(scheduleBuilder).build(); // 将trigger添加到quartz的scheduler容器 scheduler.scheduleJob(trigger); return Boolean.TRUE; } catch (SchedulerException e) { throw new ServiceException(e); } }
public serverTasks() throws SchedulerException, ParseException{ /* * 循环式的 */ SchedulerFactory factory = new StdSchedulerFactory(); Scheduler scheduler = factory.getScheduler(); //设置启动时间 DateBuilder.evenMinuteDate(new Date()); JobDetail job = JobBuilder.newJob(job.class).withIdentity("job1", "group1").build(); //job.getJobDataMap().put("a", true);//实现传参 // @NOTICE // 与SimpleTrigger对比:类不同了,现在的是Trigger的子类CronTrigger;withSchedule中的参数变为CronScheduleBuilder了 // CronScheduleBuilder可以通过类似"0/13 * * * * ?"这种表达式来创建定时任务 // 当前这个表达式的定义是30分钟执行一次 CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(CronScheduleBuilder.cronSchedule("1 0/30 * * * ?")).build(); scheduler.scheduleJob(job, trigger); scheduler.start(); //scheduler.shutdown(true); }
@Override public Trigger convert(Cron trigger) { CronScheduleBuilder schedule = cronSchedule(trigger.getCronExpression()); if (trigger.getMisfireInstruction() == Cron.MisfireInstruction.DROP) { schedule.withMisfireHandlingInstructionDoNothing(); } else if (trigger.getMisfireInstruction() == Cron.MisfireInstruction.FIRE_ONCE) { schedule.withMisfireHandlingInstructionFireAndProceed(); } return newTrigger() .forJob(trigger.getJobKey().getName(), trigger.getJobKey().getGroup()) .withDescription(trigger.getDescription()) .withIdentity(trigger.getTriggerKey().getName(), trigger.getTriggerKey().getGroup()) .withSchedule(schedule) .usingJobData(new JobDataMap(trigger.getTriggerData())) .build(); }
private void addJob(ScheduleJob scheduleJob) throws Exception{ checkNotNull(scheduleJob); Preconditions.checkNotNull(StringUtils.isEmpty(scheduleJob.getCronExpression()), "CronExpression is null"); TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); if(trigger != null){ throw new Exception("job already exists!"); } // simulate job info db persist operation scheduleJob.setJobId(String.valueOf(QuartzJobFactory.jobList.size()+1)); QuartzJobFactory.jobList.add(scheduleJob); JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class).withIdentity(scheduleJob.getJobName(),scheduleJob.getJobGroup()).build(); jobDetail.getJobDataMap().put("scheduleJob", scheduleJob); CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()); trigger = TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).withSchedule(cronScheduleBuilder).build(); scheduler.scheduleJob(jobDetail, trigger); }
private boolean prvateAddQuartz(Class<? extends Job> jobClass,JobKey jobKey, CronScheduleBuilder builder, SimpleScheduleBuilder repeatForever) { if(scheduler==null){ init(); } //创建一个任务计划生成器 设置任务名称与分组 创建任务计划 JobDetail job = JobBuilder.newJob(jobClass).withIdentity(jobKey).build(); //创建一个触发生成器 设置触发器名称与分组 设置触发器出发条件 创建触发器 Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobKey.getName(),jobKey.getGroup()).withSchedule(builder==null?repeatForever:builder).build(); try { scheduler.scheduleJob(job, trigger); outLog("添加任务计划成功!"); return true; } catch (SchedulerException e) { outLog("添加任务计划失败!"); } return false; }
/** * 创建定时任务 */ public static void createScheduleJob(Scheduler scheduler, ScheduleJobEntity scheduleJob) { try { //构建job信息 JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(getJobKey(scheduleJob.getJobId())).build(); //表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()) .withMisfireHandlingInstructionDoNothing(); //按新的cronExpression表达式构建一个新的trigger CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(scheduleJob.getJobId())).withSchedule(scheduleBuilder).build(); //放入参数,运行时的方法可以获取 jobDetail.getJobDataMap().put(ScheduleJobEntity.JOB_PARAM_KEY, new Gson().toJson(scheduleJob)); scheduler.scheduleJob(jobDetail, trigger); //暂停任务 if(scheduleJob.getStatus() == ScheduleStatus.PAUSE.getValue()){ pauseJob(scheduler, scheduleJob.getJobId()); } } catch (SchedulerException e) { throw new RRException("创建定时任务失败", e); } }
/** * 创建定时任务 */ public static void createScheduleJob(Scheduler scheduler, ScheduleJobEntity scheduleJob) { try { //构建job信息 JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(getJobKey(scheduleJob.getJobId())).build(); //表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()); //按新的cronExpression表达式构建一个新的trigger CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(scheduleJob.getJobId())).withSchedule(scheduleBuilder).build(); //放入参数,运行时的方法可以获取 jobDetail.getJobDataMap().put(ScheduleJobEntity.JOB_PARAM_KEY, scheduleJob); scheduler.scheduleJob(jobDetail, trigger); //暂停任务 if(scheduleJob.getStatus() == ScheduleStatus.PAUSE.getValue()){ pauseJob(scheduler, scheduleJob.getJobId()); } } catch (SchedulerException e) { throw new RRException("创建定时任务失败", e); } }
public void start(final AbstractNotifier notifier) { // if no interval is defined then don't start the notifier if (StringUtils.isNotBlank(notifier.getCronExpression())) { try { final JobDataMap jobDataMap = new JobDataMap(); jobDataMap.put(NotifierJob.JOB_DATA_KEY_NOTIFIER, notifier); final JobDetail jobDetail = JobBuilder.newJob(NotifierJob.class).setJobData(jobDataMap) .withIdentity("Job-" + notifier.getName()).build(); final CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule(notifier.getCronExpression()) .inTimeZone(TimeZone.getTimeZone("UTC")); final Trigger trigger = TriggerBuilder.newTrigger().withIdentity("Trigger-" + notifier.getName()) .withSchedule(cronSchedule).forJob(jobDetail).build(); scheduler.scheduleJob(jobDetail, trigger); } catch (final SchedulerException e) { logger.error("Error scheduling notifier to start {}", notifier.getName(), e); } } }
public static void createSchedule(InputDataConfig inputDataConfig) throws SchedulerException { Set<Trigger> triggerList = new HashSet<Trigger>(); JobDataMap jobDataMap = new JobDataMap(); String inputDataConfigString = ObjectConversionUtil.POJOToJSON(inputDataConfig); jobDataMap.put("inputDataConfigString", inputDataConfigString); JobDetail job = newJob(ESReportJob.class).withIdentity(inputDataConfig.getReport().getName(), quartzClientProperties.getProperty("group")).setJobData(jobDataMap).build(); int i = 0; for (String cronExpression : inputDataConfig.getReportAccess().getScheduleReport().getCronExpressionList()) { String triggerName = inputDataConfig.getReport().getName() + "_" + String.valueOf(i); Trigger trigger = newTrigger().withIdentity(triggerName, quartzClientProperties.getProperty("group")).startNow().withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build(); i++; triggerList.add(trigger); } scheduler.scheduleJob(job, triggerList, false); }
public static void updateScheduleReport(InputDataConfig inputDataConfig) throws SchedulerException { Set<Trigger> triggerList = new HashSet<Trigger>(); JobDataMap jobDataMap = new JobDataMap(); String inputDataConfigString = ObjectConversionUtil.POJOToJSON(inputDataConfig); jobDataMap.put("inputDataConfigString", inputDataConfigString); JobDetail job = newJob(ESReportJob.class).withIdentity(inputDataConfig.getReport().getName(), quartzClientProperties.getProperty("group")).setJobData(jobDataMap).build(); int i = 0; for (String cronExpression : inputDataConfig.getReportAccess().getScheduleReport().getCronExpressionList()) { String triggerName = inputDataConfig.getReport().getName() + "_" + String.valueOf(i); Trigger trigger = newTrigger().withIdentity(triggerName, quartzClientProperties.getProperty("group")).startNow().withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build(); i++; triggerList.add(trigger); } scheduler.scheduleJob(job, triggerList, true); }
private void schedule( String expr, String name, Class<? extends Job> jobClass, JobDataMap m ) throws SchedulerException { JobDataMap map = m == null ? new JobDataMap() : m; map.put( "bean", name ); String uid = name + ":" + (jid++); Trigger trigger = TriggerBuilder.newTrigger() .withSchedule( CronScheduleBuilder.cronSchedule( expr ) ) .withIdentity( uid ) .build(); JobDetail detail = JobBuilder.newJob() .ofType( jobClass ) .withIdentity( uid ) .usingJobData( map ) .build(); scheduler.scheduleJob( detail, trigger ); LOG.log( Level.INFO, () -> "Scheduled " + name + " with cron " + expr ); }
/** * 新增单个任务 Cron */ public void scheduleCronNewJob(ScheduleJob scheduleJob) throws SchedulerException, ClassNotFoundException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); // 获取trigger,即在spring配置文件中定义的bean id="myTrigger" CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); // 不存在,创建一个 JobDetail jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(scheduleJob.getJobClass())) .withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).withDescription( scheduleJob.getDesc()).build(); // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()); // 按新的cronExpression表达式构建一个新的trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail, trigger); }
private void addJob(JobType jobType, JobKey jk) throws ClassNotFoundException, SchedulerException { JobDetail jd; JobBuilder jb = JobBuilder.newJob((Class<? extends Job>) Class.forName(jobType.getClassName())); for (ParamType pt : jobType.getParam()) { jb.usingJobData(pt.getName(), pt.getValue()); } jb.withIdentity(jk); jd = jb.build(); StringBuffer cron = new StringBuffer(); cron.append(jobType.getCronSchedule().getSeconds()).append(' ') .append(jobType.getCronSchedule().getMinutes()).append(' ') .append(jobType.getCronSchedule().getHours()).append(' ') .append(jobType.getCronSchedule().getDayOfMonth()).append(' ') .append(jobType.getCronSchedule().getMonth()).append(' ') .append(jobType.getCronSchedule().getDayOfWeek()).append(' ') .append(jobType.getCronSchedule().getYear()); TriggerBuilder tb = TriggerBuilder.newTrigger() .withIdentity("trigger_" + jobType.getName(),jobType.getGroup()) .withSchedule(CronScheduleBuilder.cronSchedule(cron.toString()).withMisfireHandlingInstructionFireAndProceed());; this.scheduler.scheduleJob(jd, tb.build()); }
/** * 创建定时任务 * * @param scheduler * the scheduler * @param jobName * the job name * @param jobGroup * the job group * @param cronExpression * the cron expression * @param isSync * the is sync * @param param * the param */ public static boolean createScheduleJob(Scheduler scheduler, String jobName, String jobGroup, String cronExpression, boolean isSync, Object param) { boolean result = false; // 同步或异步 Class<? extends Job> jobClass = JobFactory.getInstance(isSync); // 构建job信息 JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroup).build(); // 放入参数,运行时的方法可以获取,资料:http://www.cnblogs.com/wyqtomorrow/archive/2007/04/28/730963.html jobDetail.getJobDataMap().put(ScheduleJobBo.JOB_PARAM_KEY, param); // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression); // 按新的cronExpression表达式构建一个新的trigger CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroup).withSchedule(scheduleBuilder) .build(); try { Date nextExexuteDate = scheduler.scheduleJob(jobDetail, trigger);// 任务创建成功,会返回下一次执行的时间 if (nextExexuteDate != null) { result = true; } } catch (SchedulerException e) { LOG.error("创建定时任务失败", e); return false; } return result; }
/** * 更新定时任务 * * @param scheduler * the scheduler * @param jobName * the job name * @param jobGroup * the job group * @param cronExpression * the cron expression * @param isSync * the is sync * @param param * the param */ public static boolean updateScheduleJob(Scheduler scheduler, String jobName, String jobGroup, String cronExpression, boolean isSync, Object param) { boolean result = false; try { TriggerKey triggerKey = ScheduleJobUtils.getTriggerKey(jobName, jobGroup); // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); // 按新的cronExpression表达式重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); // 按新的trigger重新设置job执行 Date nextExcuteDate = scheduler.rescheduleJob(triggerKey, trigger);// 任务的下一次执行时间,更新成功会返回 if (nextExcuteDate != null) { result = true; } } catch (SchedulerException e) { LOG.error("更新定时任务失败", e); return false; } return result; }
private void scheduleReconnect() { Scheduler sched; try { sched = StdSchedulerFactory.getDefaultScheduler(); JobDetail job = newJob(ReconnectJob.class) .withIdentity("Reconnect", MPD_SCHEDULER_GROUP) .build(); CronTrigger trigger = newTrigger() .withIdentity("Reconnect", MPD_SCHEDULER_GROUP) .withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?")) .build(); sched.scheduleJob(job, trigger); logger.debug("Scheduled a daily MPD Reconnect of all MPDs"); } catch (SchedulerException se) { logger.warn("scheduling MPD Reconnect failed", se); } }
/** * Schedules either a job handling the Upload (<code>LOCAL_TO_DROPBOX</code>) * or Download (<code>DROPBOX_TO_LOCAL</code>) direction depending on * <code>isUpload</code>. * * @param interval the Trigger interval as cron expression * @param isUpload */ private void schedule(String interval, boolean isUpload) { String direction = isUpload ? "Upload" : "Download"; try { Scheduler sched = StdSchedulerFactory.getDefaultScheduler(); JobDetail job = newJob(SynchronizationJob.class) .withIdentity(direction, DROPBOX_SCHEDULER_GROUP) .build(); CronTrigger trigger = newTrigger() .withIdentity(direction, DROPBOX_SCHEDULER_GROUP) .withSchedule(CronScheduleBuilder.cronSchedule(interval)) .build(); sched.scheduleJob(job, trigger); logger.debug("Scheduled synchronization job (direction={}) with cron expression '{}'", direction, interval); } catch (SchedulerException e) { logger.warn("Could not create synchronization job: {}", e.getMessage()); } }
@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); }
@SuppressWarnings("unchecked") public static TaskBase createCronTask(@Nonnull Class<? extends TaskCallback> commandClass, @Nonnull String cronExpression) { checkCommandClass(commandClass, false); Class<QuartzCommand> quartzCommand = (Class<QuartzCommand>) commandClass; JobDetail jobDetail = JobBuilder.newJob(quartzCommand).withIdentity(generateUniqueName(quartzCommand), QuartzTask.ARTIFACTORY_GROUP).build(); Trigger trigger = TriggerBuilder.newTrigger().forJob(jobDetail) .withIdentity(generateUniqueName(quartzCommand)) .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)) .build(); return fillProperties(QuartzTask.createQuartzTask(commandClass, trigger, jobDetail), false); }
private void scheduleDayOfWeekJob(DayOfWeekSchedulableJob job, boolean update) { logObjectIfNotNull(job); validateDayOfWeekSchedulableJob(job); MotechEvent motechEvent = job.getMotechEvent(); Time time = job.getTime(); CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.atHourAndMinuteOnGivenDaysOfWeek(time.getHour(), time.getMinute(), job.getCronDays() .toArray(new Integer[job.getCronDays().size()])); CronTriggerImpl cronTrigger = (CronTriggerImpl) cronScheduleBuilder.build(); CronSchedulableJob cronSchedulableJob = new CronSchedulableJob(motechEvent, cronTrigger.getCronExpression(), job.getStartDate(), job.getEndDate(), job.isIgnorePastFiresAtStart(), job.isUiDefined()); scheduleCronJob(cronSchedulableJob, true, update); }
private CronScheduleBuilder setMisfirePolicyForCronTrigger(CronScheduleBuilder cronSchedule, String motechMisfireProperty) { Integer misfirePolicyAsInt = cronTriggerMisfirePolicies.get(motechMisfireProperty); if (misfirePolicyAsInt == null || misfirePolicyAsInt.equals(CronTrigger.MISFIRE_INSTRUCTION_SMART_POLICY)) { return cronSchedule; } if (misfirePolicyAsInt.equals(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING)) { return cronSchedule.withMisfireHandlingInstructionDoNothing(); } if (misfirePolicyAsInt.equals(CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW)) { return cronSchedule.withMisfireHandlingInstructionFireAndProceed(); } if (misfirePolicyAsInt.equals(CronTrigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY)) { return cronSchedule.withMisfireHandlingInstructionIgnoreMisfires(); } return cronSchedule; }
private void scheduleAJob(IJavaCCJob jobInfo) { logger.info("scheduling job [" + jobInfo.getId() + "]"); if (scheduledJobCache.containsKey(jobInfo.getId())) { logger.info("JavaCCJob [" + jobInfo.getId() + "] already scheduled. ignore this schedule request!"); return; } JobDetail job = JobBuilder.newJob(DispatcherJob.class) .withIdentity(getJobNameFromJobInfo(jobInfo), DEFAULT_GROUP).build(); CronTrigger trigger = TriggerBuilder.newTrigger() .withIdentity(getTriggerNameFromJobInfo(jobInfo), DEFAULT_GROUP) .withSchedule(CronScheduleBuilder.cronSchedule(getCronExpressionFromJobInfo(jobInfo))).build(); trigger.getJobDataMap().put(DispatcherJob.JOB_DISPATCHER, this); trigger.getJobDataMap().put(DispatcherJob.JOB_INFO, jobInfo); try { scheduler.scheduleJob(job, trigger); scheduledJobCache.put(jobInfo.getId(), trigger); } catch (SchedulerException e) { logger.warn("error schedule job", e); } }
private void scheduleJobs( List< JobDetail > jobs, String cronExpression, String settopId ) throws SchedulerException { if ( jobs != null && !jobs.isEmpty() ) { for ( JobDetail job : jobs ) { Trigger trigger = newTrigger().withIdentity( "CronTrigger:" + settopId + ":" + job.getKey() ) .withPriority( 6 ).forJob( job ) .withSchedule( CronScheduleBuilder.cronSchedule( cronExpression ) ).build(); schedule( job, trigger ); } } else { logger.warn( "No Monitoring Jobs found: " ); } }
private void scheduleJob(JobContext jobContext, AlertConfigDTO alertConfig) throws SchedulerException { LOG.info("Starting {}", jobContext.getJobName()); String triggerKey = String.format("alert_scheduler_trigger_%d", alertConfig.getId()); String cronSchedule = alertConfig.getCronExpression(); if (!StringUtils.isBlank(alertConfig.getHolidayCronExpression())) { cronSchedule = alertConfig.getHolidayCronExpression(); } CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey) .withSchedule(CronScheduleBuilder.cronSchedule(cronSchedule)).build(); String jobKey = jobContext.getJobName(); JobDetail job = JobBuilder.newJob(AlertJobRunnerV2.class).withIdentity(jobKey).build(); job.getJobDataMap().put(AlertJobRunnerV2.ALERT_JOB_CONTEXT_V2, jobContext); quartzScheduler.scheduleJob(job, trigger); LOG.info("Started {}: {}", jobKey, alertConfig); }
/** * Schedules new quartz scheduler jobs for committing transactions and * backing up the database */ private void scheduleJob() { try { Scheduler sched = StdSchedulerFactory.getDefaultScheduler(); // schedule commit-job JobDetail job = newJob(CommitJob.class).withIdentity("Commit_Transaction", SCHEDULER_GROUP).build(); SimpleTrigger trigger = newTrigger().withIdentity("Commit_Transaction", SCHEDULER_GROUP) .withSchedule(repeatSecondlyForever(commitInterval)).build(); sched.scheduleJob(job, trigger); logger.debug("Scheduled Commit-Job with interval {}sec.", commitInterval); // schedule backup-job JobDetail backupJob = newJob(BackupJob.class).withIdentity("Backup_DB", SCHEDULER_GROUP).build(); CronTrigger backupTrigger = newTrigger().withIdentity("Backup_DB", SCHEDULER_GROUP) .withSchedule(CronScheduleBuilder.cronSchedule(backupInterval)).build(); sched.scheduleJob(backupJob, backupTrigger); logger.debug("Scheduled Backup-Job with cron expression '{}'", backupInterval); } catch (SchedulerException e) { logger.warn("Could not create Job: {}", e.getMessage()); } }
/** * The user may configure this binding to update the internal clock of * FHT80b devices via rf command. The method takes care of scheduling this * job. */ private JobKey scheduleJob(Class<? extends Job> jobClass, String cronExpression) { JobKey jobKey = null; try { Scheduler sched = StdSchedulerFactory.getDefaultScheduler(); JobDetail detail = JobBuilder.newJob(jobClass).withIdentity("FHT " + jobClass.getSimpleName(), "cul") .build(); detail.getJobDataMap().put(FHTBinding.class.getName(), this); CronTrigger trigger = TriggerBuilder.newTrigger().forJob(detail) .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build(); jobKey = detail.getKey(); sched.scheduleJob(detail, trigger); } catch (SchedulerException e) { logger.error("Can't schedule time update job", e); } return jobKey; }