Search in sources :

Example 1 with TriggerBuilder

use of org.quartz.TriggerBuilder in project cdap by caskdata.

the class TimeScheduler method scheduleJob.

private void scheduleJob(ProgramId program, SchedulableProgramType programType, String scheduleName, String cronEntry, JobDetail job, Map<String, String> properties) throws SchedulerException {
    try {
        TriggerKey triggerKey = getGroupedTriggerKey(program, programType, scheduleName);
        LOG.debug("Scheduling job {} with cron {}", scheduleName, cronEntry);
        TriggerBuilder trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey.getName(), PAUSED_NEW_TRIGGERS_GROUP).forJob(job).withSchedule(CronScheduleBuilder.cronSchedule(Schedulers.getQuartzCronExpression(cronEntry)).withMisfireHandlingInstructionDoNothing());
        addProperties(trigger, properties);
        scheduler.scheduleJob(trigger.build());
    } catch (org.quartz.SchedulerException e) {
        throw new SchedulerException(e);
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) TriggerBuilder(org.quartz.TriggerBuilder)

Example 2 with TriggerBuilder

use of org.quartz.TriggerBuilder in project cdap by caskdata.

the class TimeScheduler method scheduleJob.

private void scheduleJob(TriggerKey triggerKey, String scheduleName, String cronEntry, JobDetail job) throws SchedulerException {
    try {
        LOG.debug("Scheduling job {} with cron {}", scheduleName, cronEntry);
        TriggerBuilder trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey.getName(), PAUSED_NEW_TRIGGERS_GROUP).forJob(job).withSchedule(CronScheduleBuilder.cronSchedule(Schedulers.getQuartzCronExpression(cronEntry)).withMisfireHandlingInstructionDoNothing());
        scheduler.scheduleJob(trigger.build());
    } catch (org.quartz.SchedulerException e) {
        throw new SchedulerException(e);
    }
}
Also used : TriggerBuilder(org.quartz.TriggerBuilder)

Example 3 with TriggerBuilder

use of org.quartz.TriggerBuilder in project BRFS by zhangnianli.

the class DefaultBaseSchedulers method addTask.

@Override
public boolean addTask(SumbitTaskInterface task) throws ParamsErrorException {
    // 1.检查任务的有效性
    checkTask(task);
    // 2.线程池处于暂停时,不提交任务
    if (this.pausePoolFlag) {
        LOG.warn("Thread pool is paused !!!");
        return false;
    }
    try {
        Scheduler scheduler = this.ssf.getScheduler(this.instanceName);
        // 当线程池不处于运行时,将不添加任务
        if (!isNormal()) {
            LOG.warn("Thread pool is not normal");
            return false;
        }
        // 当线程池满了也不会添加任务
        if (this.poolSize <= getSumbitTaskCount()) {
            LOG.warn("thread pool is full !!!");
            return false;
        }
        // 1.设置job的名称及执行的class
        Class<? extends Job> clazz = (Class<? extends Job>) Class.forName(task.getClassInstanceName());
        String taskName = task.getTaskName();
        String taskGroup = task.getTaskGroupName();
        JobBuilder jobBuilder = JobBuilder.newJob(clazz).withIdentity(taskName, taskGroup);
        // 2.设置任务需要的数据
        Map<String, String> tmp = task.getTaskContent();
        if (tmp != null && !tmp.isEmpty()) {
            JobDataMap jobData = new JobDataMap();
            jobData.putAll(tmp);
            jobBuilder.usingJobData(jobData);
        }
        // 3.生成jobDetail
        JobDetail jobDetail = jobBuilder.build();
        // 4.判断触发器的类型 0 cron任务,1 simple任务
        int taskType = task.getTaskKind();
        String cycleContent = task.getCycleContent();
        Trigger trigger = null;
        if (taskType == 0) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cycleContent);
            trigger = TriggerBuilder.newTrigger().withIdentity(taskName, taskGroup).withSchedule(cronScheduleBuilder).build();
        } else if (taskType == 1) {
            String[] cycles = BrStringUtils.getSplit(cycleContent, ",");
            if (cycles == null || cycles.length == 0) {
                throw new NullPointerException("simple trigger cycle time is empty !!! content : " + cycleContent);
            }
            if (cycles.length != 5) {
                throw new NullPointerException("simple trigger cycle time is error !!! content : " + cycleContent);
            }
            long interval = Long.parseLong(cycles[0]);
            int repeateCount = Integer.parseInt(cycles[1]);
            repeateCount = repeateCount - 1;
            long delayTime = Long.parseLong(cycles[2]);
            boolean rightNow = Boolean.valueOf(cycles[3]);
            boolean cycleFlag = Boolean.valueOf(cycles[4]);
            SimpleScheduleBuilder builder = SimpleScheduleBuilder.simpleSchedule();
            builder.withIntervalInMilliseconds(interval);
            if (cycleFlag) {
                builder.repeatForever();
            } else if (repeateCount >= 0) {
                builder.withRepeatCount(repeateCount);
            } else {
                LOG.warn("repeated count is zero !!!!");
                return false;
            }
            TriggerBuilder trigBuilder = TriggerBuilder.newTrigger().withIdentity(taskName, taskGroup).withSchedule(builder);
            if (!rightNow && delayTime > 0) {
                long current = System.currentTimeMillis() + delayTime;
                Date date = new Date(current);
                trigBuilder.startAt(date);
            } else {
                trigBuilder.startNow();
            }
            trigger = trigBuilder.build();
        }
        if (trigger == null || jobDetail == null) {
            LOG.warn(" create task message is null");
            return false;
        }
        scheduler.scheduleJob(jobDetail, trigger);
        return true;
    } catch (NumberFormatException | ClassNotFoundException | ParseException | SchedulerException e) {
        LOG.error("add task {}", e);
    }
    return false;
}
Also used : JobDataMap(org.quartz.JobDataMap) CronScheduleBuilder(org.quartz.CronScheduleBuilder) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler) JobBuilder(org.quartz.JobBuilder) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) TriggerBuilder(org.quartz.TriggerBuilder) Date(java.util.Date) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) ParseException(java.text.ParseException) Job(org.quartz.Job)

Example 4 with TriggerBuilder

use of org.quartz.TriggerBuilder in project kylo by Teradata.

the class QuartzScheduler method scheduleJob.

public void scheduleJob(JobIdentifier jobIdentifier, TriggerIdentifier triggerIdentifier, Class<? extends QuartzJobBean> clazz, String cronExpression, Map<String, Object> jobData, boolean fireImmediately) throws SchedulerException {
    if (jobData == null) {
        jobData = new HashMap<>();
    }
    JobDataMap jobDataMap = new JobDataMap(jobData);
    JobDetail job = newJob(clazz).withIdentity(jobIdentifier.getName(), jobIdentifier.getGroup()).requestRecovery(false).setJobData(jobDataMap).build();
    TriggerBuilder triggerBuilder = newTrigger().withIdentity(triggerIdentifier.getName(), triggerIdentifier.getGroup()).withSchedule(cronSchedule(cronExpression).inTimeZone(TimeZone.getTimeZone("UTC")).withMisfireHandlingInstructionFireAndProceed()).forJob(job.getKey());
    if (fireImmediately) {
        Date previousTriggerTime = null;
        try {
            previousTriggerTime = CronExpressionUtil.getPreviousFireTime(cronExpression);
            if (previousTriggerTime != null) {
                triggerBuilder.startAt(previousTriggerTime);
            }
        } catch (ParseException e) {
        }
    }
    Trigger trigger = triggerBuilder.build();
    scheduleJob(job, trigger);
}
Also used : JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) ParseException(java.text.ParseException) TriggerBuilder(org.quartz.TriggerBuilder) Date(java.util.Date)

Aggregations

TriggerBuilder (org.quartz.TriggerBuilder)4 ParseException (java.text.ParseException)2 Date (java.util.Date)2 JobDataMap (org.quartz.JobDataMap)2 JobDetail (org.quartz.JobDetail)2 Trigger (org.quartz.Trigger)2 CronScheduleBuilder (org.quartz.CronScheduleBuilder)1 CronTrigger (org.quartz.CronTrigger)1 Job (org.quartz.Job)1 JobBuilder (org.quartz.JobBuilder)1 Scheduler (org.quartz.Scheduler)1 SchedulerException (org.quartz.SchedulerException)1 SimpleScheduleBuilder (org.quartz.SimpleScheduleBuilder)1 SimpleTrigger (org.quartz.SimpleTrigger)1 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)1 TriggerKey (org.quartz.TriggerKey)1