private static JobDetail job(final Class<? extends Job> jobType) { JobKey key = jobKey(jobType); return JobBuilder.newJob(jobType) .withIdentity(key) .build(); }
private JobDetail createJobDetail(final String jobClass) { JobDetail result = JobBuilder.newJob(LiteJob.class).withIdentity(liteJobConfig.getJobName()).build(); result.getJobDataMap().put(JOB_FACADE_DATA_MAP_KEY, jobFacade); Optional<ElasticJob> elasticJobInstance = createElasticJobInstance(); if (elasticJobInstance.isPresent()) { result.getJobDataMap().put(ELASTIC_JOB_DATA_MAP_KEY, elasticJobInstance.get()); } else if (!jobClass.equals(ScriptJob.class.getCanonicalName())) { try { result.getJobDataMap().put(ELASTIC_JOB_DATA_MAP_KEY, Class.forName(jobClass).newInstance()); } catch (final ReflectiveOperationException ex) { throw new JobConfigurationException("Elastic-Job: Job class '%s' can not initialize.", jobClass); } } return result; }
protected JobDetail getJobDetail(long id, String name, String group) { // define the job and tie it to our HelloJob class JobDetail job = newJob(jobClass) .withIdentity(JOB_MARK + SEPARATOR + name + SEPARATOR + id, GROUP_MARK + SEPARATOR + group + SEPARATOR + id) .requestRecovery() .build(); JobDataMap jobMap = job.getJobDataMap(); jobMap.put("job_group", group); jobMap.put("job_name", name); jobMap.put("job_id", id); //添加参数 if (params != null && params.size() > 0) { jobMap.putAll(params); } return job; }
JobDetail jobDetail = JobBuilder.newJob(jobClass_).withIdentity(jobKey).build();
/** * Schedule the provided job and trigger. This method will setup a default name and group for * both. * * @param jobClass A jobClass to setup and schedule. * @param configurer A callback to setup the job and trigger. * @return This quartz instance. */ public Quartz with(final Class<? extends Job> jobClass, final BiConsumer<JobBuilder, TriggerBuilder<Trigger>> configurer) { requireNonNull(jobClass, "Job class is required."); JobBuilder job = JobBuilder.newJob(jobClass) .withIdentity( JobKey.jobKey(jobClass.getSimpleName(), jobClass.getPackage().getName()) ); TriggerBuilder<Trigger> trigger = TriggerBuilder.newTrigger() .withIdentity( TriggerKey.triggerKey(jobClass.getSimpleName(), jobClass.getPackage().getName()) ); configurer.accept(job, trigger); return with(job.build(), trigger.build()); }
/** * Schedule the provided job and trigger. This method will setup a default name and group for * both. * * @param jobClass A jobClass to setup and schedule. * @param configurer A callback to setup the trigger. * @return This quartz instance. */ public Quartz with(final Class<? extends Job> jobClass, final Consumer<TriggerBuilder<Trigger>> configurer) { requireNonNull(jobClass, "Job class is required."); JobBuilder job = JobBuilder.newJob(jobClass) .withIdentity( JobKey.jobKey(jobClass.getSimpleName(), jobClass.getPackage().getName()) ); TriggerBuilder<Trigger> trigger = TriggerBuilder.newTrigger() .withIdentity( TriggerKey.triggerKey(jobClass.getSimpleName(), jobClass.getPackage().getName()) ); configurer.accept(trigger); return with(job.build(), trigger.build()); }
private JobDetail buildJobDetail(DeadlineMessage deadlineMessage, ScopeDescriptor deadlineScope, JobKey jobKey) { JobDataMap jobData = DeadlineJob.DeadlineJobDataBinder.toJobData(serializer, deadlineMessage, deadlineScope); return JobBuilder.newJob(DeadlineJob.class) .withDescription(deadlineMessage.getPayloadType().getName()) .withIdentity(jobKey) .usingJobData(jobData) .build(); }
/** Add a task to the scheduler */ public void addTask(String name, Class<? extends Task> taskclass, TaskTimer timer) throws SchedulerException, ParseException { assert timer != null : "Cannot add scheduler task " + name + " as no timer is set"; JobDetail job = JobBuilder.newJob() .withIdentity(name, Scheduler.DEFAULT_GROUP) .ofType(taskclass) .build(); // new JobDetail(name, Scheduler.DEFAULT_GROUP, taskclass); if (timer.getCronExpression() != null && !timer.getCronExpression().isEmpty()) { logger.info( "Scheduled task metadata. Task name: {}" + ", cron expression: {}", taskclass.getName(), timer.getCronExpression()); } else { logger.info("Scheduled task metadata. Task name: {}", taskclass.getName()); } scheduler.scheduleJob(job, timer.getTrigger()); }
/** * Builds the JobDetail instance for Quartz, which defines the Job that needs to be executed when the trigger * fires. * <p/> * The resulting JobDetail must be identified by the given {@code jobKey} and represent a Job that dispatches * the given {@code event}. * <p/> * This method may be safely overridden to change behavior. Defaults to a JobDetail to fire a {@link FireEventJob}. * * @param event The event to be scheduled for dispatch * @param jobKey The key of the Job to schedule * @return a JobDetail describing the Job to be executed */ protected JobDetail buildJobDetail(EventMessage event, JobKey jobKey) { JobDataMap jobData = jobDataBinder.toJobData(event); return JobBuilder.newJob(FireEventJob.class) .withDescription(event.getPayloadType().getName()) .withIdentity(jobKey) .usingJobData(jobData) .build(); }
private void scheduleNewBackupJob(BackupConfig newBackupConfig) { if (newBackupConfig == null || isBlank(newBackupConfig.getSchedule())) { return; } try { JobDetail jobDetail = newJob() .withIdentity(jobKey()) .ofType(ScheduleBackupQuartzJob.class) .usingJobData(jobDataMap()) .build(); CronTrigger trigger = newTrigger() .withIdentity(triggerKey()) .withSchedule(cronSchedule(new CronExpression(newBackupConfig.getSchedule()))) .build(); quartzScheduler.scheduleJob(jobDetail, trigger); LOG.info("Initialized backup job with schedule " + newBackupConfig.getSchedule()); clearServerHealthError(); } catch (SchedulerException e) { LOG.error("Unable to schedule backup job", e); setServerHealthError("Unable to schedule backup job.", "Check the server log for detailed errors: " + e.getMessage()); } catch (ParseException e) { LOG.error("Unable to schedule backup job", e); setServerHealthError("Unable to schedule backup job.", "Invalid cron syntax for backup configuration at offset " + e.getErrorOffset() + ": " + e.getMessage()); } }
KairosDBJob job = (KairosDBJob) guice.getInstance(bindingClass); JobDetail jobDetail = newJob(job.getClass()) .withIdentity(job.getClass().getName()).build();
/** * 创建定时任务 */ public static void createScheduleJob(Scheduler scheduler, Job 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(Job.JOB_PARAM_KEY, scheduleJob); scheduler.scheduleJob(jobDetail, trigger); // 暂停任务 if (scheduleJob.getStatus().equals(Job.ScheduleStatus.PAUSE.getValue())) { pauseJob(scheduler, scheduleJob.getJobId()); } } catch (SchedulerException e) { log.error("创建定时任务失败", e); } }
/** {@inheritDoc} */ @Override protected JobSpecSchedule doScheduleJob(JobSpec jobSpec, Runnable jobRunnable) { // Build a data map that gets passed to the job JobDataMap jobDataMap = new JobDataMap(); jobDataMap.put(JOB_SPEC_KEY, jobSpec); jobDataMap.put(JOB_RUNNABLE_KEY, jobRunnable); // Build a Quartz job JobDetail job = JobBuilder.newJob(QuartzJob.class) .withIdentity(jobSpec.getUri().toString()) .withDescription(Strings.nullToEmpty(jobSpec.getDescription())) .usingJobData(jobDataMap) .build(); Trigger jobTrigger = createTrigger(job.getKey(), jobSpec); QuartzJobSchedule jobSchedule = new QuartzJobSchedule(jobSpec, jobRunnable, jobTrigger); try { _scheduler.getScheduler().scheduleJob(job, jobTrigger); getLog().info(String.format("Scheduled job %s next two fire times: %s , %s.", jobSpec, jobTrigger.getNextFireTime(), jobTrigger.getFireTimeAfter(jobTrigger.getNextFireTime()))); } catch (SchedulerException e) { throw new RuntimeException("Scheduling failed for " + jobSpec + ":" + e, e); } return jobSchedule; }
private void scheduleJob(Scheduler scheduler, PipelineConfig pipelineConfig) { TimerConfig timer = pipelineConfig.getTimer(); if (timer != null) { try { CronTrigger trigger = newTrigger() .withIdentity(triggerKey(CaseInsensitiveString.str(pipelineConfig.name()), PIPELINE_TRIGGGER_TIMER_GROUP)) .withSchedule(cronSchedule(new CronExpression(timer.getTimerSpec()))) .build(); JobDetail jobDetail = newJob() .withIdentity(jobKey(CaseInsensitiveString.str(pipelineConfig.name()), PIPELINE_TRIGGGER_TIMER_GROUP)) .ofType(SchedulePipelineQuartzJob.class) .usingJobData(jobDataMapFor(pipelineConfig)) .build(); scheduler.scheduleJob(jobDetail, trigger); LOG.info("Initialized timer for pipeline {} with {}", pipelineConfig.name(), timer.getTimerSpec()); } catch (ParseException e) { showPipelineError(pipelineConfig, e, "Bad timer specification for timer in Pipeline: " + pipelineConfig.name(), "Cannot schedule pipeline using the timer"); } catch (SchedulerException e) { showPipelineError(pipelineConfig, e, "Could not register pipeline '" + pipelineConfig.name() + "' with timer", ""); } } }
.withIdentity(jobDescription.getJobName(), jobDescription.getGroupName()).build();
JobDetail job = newJob().withIdentity(jobTriggerName, JOB_INITIALIZATION_PLUGIN_NAME).ofType(FileScanJob.class) .usingJobData(FileScanJob.FILE_NAME, jobFile.getFileName()) .usingJobData(FileScanJob.FILE_SCAN_LISTENER_NAME, JOB_INITIALIZATION_PLUGIN_NAME + '_' + getName())
JobDetail job = newJob().withIdentity(jobTriggerName, JOB_INITIALIZATION_PLUGIN_NAME).ofType(FileScanJob.class) .usingJobData(FileScanJob.FILE_NAME, jobFile.getFileName()) .usingJobData(FileScanJob.FILE_SCAN_LISTENER_NAME, JOB_INITIALIZATION_PLUGIN_NAME + '_' + getName())
public JobBuilder getJobBuilder() { JobBuilder b = JobBuilder.newJob() .ofType(getJobClass()) .requestRecovery(requestsRecovery()) .storeDurably(isDurable()) .usingJobData(getJobDataMap()) .withDescription(getDescription()) .withIdentity(getKey()); return b; } }
public JobBuilder getJobBuilder() { JobBuilder b = JobBuilder.newJob() .ofType(getJobClass()) .requestRecovery(requestsRecovery()) .storeDurably(isDurable()) .usingJobData(getJobDataMap()) .withDescription(getDescription()) .withIdentity(getKey()); return b; } }
protected void startJob(ScheduleJobEntity jobEntity) { try { if (scheduler.checkExists(createJobKey(jobEntity))) { return; } JobDetail jobDetail = JobBuilder .newJob(DynamicJob.class) .withIdentity(createJobKey(jobEntity)) .setJobData(createJobDataMap(jobEntity.getParameters())) .usingJobData(DynamicJobFactory.JOB_ID_KEY, jobEntity.getId()) .withDescription(jobEntity.getName() + (jobEntity.getRemark() == null ? "" : jobEntity.getRemark())) .build(); MutableTrigger trigger = scheduleTriggerBuilder.buildTrigger(jobEntity.getQuartzConfig()); trigger.setKey(createTriggerKey(jobEntity)); scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) { throw new BusinessException("启动定时调度失败", e); } }