Search in sources :

Example 86 with TriggerKey

use of org.quartz.TriggerKey in project PublicCMS-preview by sanluan.

the class ScheduledTask method create.

/**
 * 创建任务计划
 *
 * @param site
 * @param id
 * @param cronExpression
 */
public void create(SysSite site, Integer id, String cronExpression) {
    if (CommonUtils.notEmpty(id) && CommonUtils.notEmpty(cronExpression)) {
        Date startTime = CommonUtils.getDate();
        String taskName = getTaskName(id);
        TriggerKey triggerKey = TriggerKey.triggerKey(taskName);
        try {
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(site.getId() % 60 + BLANK_SPACE + cronExpression);
            if (null == trigger) {
                JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class).withIdentity(taskName).build();
                jobDetail.getJobDataMap().put(ID, id);
                trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(scheduleBuilder).startNow().build();
                scheduler.scheduleJob(jobDetail, trigger);
            } else {
                trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).startNow().build();
                scheduler.rescheduleJob(triggerKey, trigger);
            }
        } catch (SchedulerException e) {
            sysTaskService.updateStatus(id, TASK_STATUS_ERROR);
            logTaskService.save(new LogTask(site.getId(), id, startTime, CommonUtils.getDate(), false, e.getMessage()));
        }
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) CronTrigger(org.quartz.CronTrigger) CronScheduleBuilder(org.quartz.CronScheduleBuilder) JobDetail(org.quartz.JobDetail) SchedulerException(org.quartz.SchedulerException) Date(java.util.Date) LogTask(com.publiccms.entities.log.LogTask)

Example 87 with TriggerKey

use of org.quartz.TriggerKey in project jeesuite-libs by vakinge.

the class AbstractJob method init.

public void init() {
    triggerName = jobName + "Trigger";
    triggerKey = new TriggerKey(triggerName, group);
    JobConfig jobConfg = new JobConfig(group, jobName, cronExpr);
    // 从持久化配置合并
    if (JobContext.getContext().getConfigPersistHandler() != null) {
        JobContext.getContext().getConfigPersistHandler().merge(jobConfg);
    }
    JobContext.getContext().getRegistry().register(jobConfg);
    logger.info("Initialized Job_{} OK!!", jobName);
}
Also used : TriggerKey(org.quartz.TriggerKey) JobConfig(com.jeesuite.scheduler.model.JobConfig)

Example 88 with TriggerKey

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

the class TimeScheduler method resumeProgramSchedule.

public void resumeProgramSchedule(ProgramSchedule schedule) throws NotFoundException, SchedulerException {
    try {
        Collection<TriggerKey> triggerKeys = getGroupedTriggerKeys(schedule);
        // Must assert all trigger keys exist before processing each trigger key
        assertTriggerKeysExist(triggerKeys);
        for (TriggerKey triggerKey : triggerKeys) {
            if (triggerKey.getGroup().equals(PAUSED_NEW_TRIGGERS_GROUP)) {
                Trigger neverScheduledTrigger = scheduler.getTrigger(triggerKey);
                TriggerBuilder<? extends Trigger> triggerBuilder = neverScheduledTrigger.getTriggerBuilder();
                // move this key from TimeScheduler#PAUSED_NEW_TRIGGERS_GROUP to the Key#DEFAULT_GROUP group
                // (when no group name is provided default is used)
                Trigger resumedTrigger = triggerBuilder.withIdentity(triggerKey.getName()).build();
                scheduler.rescheduleJob(neverScheduledTrigger.getKey(), resumedTrigger);
                triggerKey = resumedTrigger.getKey();
            }
            scheduler.resumeTrigger(triggerKey);
        }
    } catch (org.quartz.SchedulerException e) {
        throw new SchedulerException(e);
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) SatisfiableTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) ProtoTrigger(co.cask.cdap.proto.ProtoTrigger) Trigger(org.quartz.Trigger) AbstractSatisfiableCompositeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) TimeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)

Example 89 with TriggerKey

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

the class TimeScheduler method suspendProgramSchedule.

public void suspendProgramSchedule(ProgramSchedule schedule) throws NotFoundException, SchedulerException {
    try {
        Collection<TriggerKey> triggerKeys = getGroupedTriggerKeys(schedule);
        // Must assert all trigger keys exist before processing each trigger key
        assertTriggerKeysExist(triggerKeys);
        for (TriggerKey triggerKey : triggerKeys) {
            scheduler.pauseTrigger(triggerKey);
        }
    } catch (org.quartz.SchedulerException e) {
        throw new SchedulerException(e);
    }
}
Also used : TriggerKey(org.quartz.TriggerKey)

Example 90 with TriggerKey

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

the class LogPrintingJob method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    try {
        LOG.info("Received Trigger at {}", context.getScheduledFireTime().toString());
        JobDataMap triggerMap = context.getTrigger().getJobDataMap();
        JobDataMap map = context.getMergedJobDataMap();
        String[] keys = map.getKeys();
        TriggerKey triggerKey = context.getTrigger().getKey();
        if (triggerKey.getName().equalsIgnoreCase("g2")) {
            Preconditions.checkArgument(triggerMap.getString(KEY).equals(VALUE));
        } else {
            Preconditions.checkArgument(!triggerMap.containsKey(KEY));
        }
        Preconditions.checkArgument(keys != null);
        Preconditions.checkArgument(keys.length > 0);
        LOG.info("Number of parameters {}", keys.length);
        for (String key : keys) {
            LOG.info("Parameter key: {}, value: {}", key, map.get(key));
        }
    } catch (Throwable e) {
        throw Throwables.propagate(e);
    }
    throw new JobExecutionException("exception");
}
Also used : TriggerKey(org.quartz.TriggerKey) JobDataMap(org.quartz.JobDataMap) JobExecutionException(org.quartz.JobExecutionException)

Aggregations

TriggerKey (org.quartz.TriggerKey)112 Trigger (org.quartz.Trigger)49 JobKey (org.quartz.JobKey)30 SchedulerException (org.quartz.SchedulerException)30 CronTrigger (org.quartz.CronTrigger)25 JobDetail (org.quartz.JobDetail)22 Test (org.junit.Test)18 Scheduler (org.quartz.Scheduler)18 SimpleTrigger (org.quartz.SimpleTrigger)15 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)11 ArrayList (java.util.ArrayList)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 HashMap (java.util.HashMap)9 Date (java.util.Date)6 List (java.util.List)5 CronScheduleBuilder (org.quartz.CronScheduleBuilder)5 BigDecimal (java.math.BigDecimal)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 ParseException (java.text.ParseException)4