Search in sources :

Example 1 with OperableTrigger

use of org.quartz.spi.OperableTrigger in project Saturn by vipshop.

the class JobScheduler method getNextFireTimePausePeriodEffected.

/**
 * 获取下次作业触发时间.可能被暂停时间段所影响。
 *
 * @return 下次作业触发时间
 */
public Date getNextFireTimePausePeriodEffected() {
    SaturnScheduler saturnScheduler = job.getScheduler();
    if (saturnScheduler == null) {
        return null;
    }
    Trigger trigger = saturnScheduler.getTrigger();
    if (trigger == null) {
        return null;
    }
    ((OperableTrigger) trigger).updateAfterMisfire(null);
    Date nextFireTime = trigger.getNextFireTime();
    while (nextFireTime != null && configService.isInPausePeriod(nextFireTime)) {
        nextFireTime = trigger.getFireTimeAfter(nextFireTime);
    }
    if (null == nextFireTime) {
        return null;
    }
    return nextFireTime;
}
Also used : SaturnScheduler(com.vip.saturn.job.trigger.SaturnScheduler) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) OperableTrigger(org.quartz.spi.OperableTrigger) Date(java.util.Date)

Example 2 with OperableTrigger

use of org.quartz.spi.OperableTrigger in project motech by motech.

the class MotechSchedulerServiceImpl method buildJobDetail.

private Trigger buildJobDetail(SchedulableJob schedulableJob, Date jobStartTime, Date jobEndTime, JobId jobId, JobDetail jobDetail, ScheduleBuilder scheduleBuilder) {
    Trigger trigger = newTrigger().withIdentity(triggerKey(jobId.value(), JOB_GROUP_NAME)).forJob(jobDetail).withSchedule(scheduleBuilder).startAt(jobStartTime).endAt(jobEndTime).build();
    DateTime now = now();
    if (schedulableJob.isIgnorePastFiresAtStart() && newDateTime(jobStartTime).isBefore(now)) {
        List<Date> pastTriggers = TriggerUtils.computeFireTimesBetween((OperableTrigger) trigger, null, jobStartTime, now.toDate());
        if (pastTriggers.size() > 0) {
            if (scheduleBuilder instanceof SimpleScheduleBuilder && ((RepeatingSchedulableJob) schedulableJob).getRepeatCount() != null) {
                ((SimpleScheduleBuilder) scheduleBuilder).withRepeatCount(((RepeatingSchedulableJob) schedulableJob).getRepeatCount() - pastTriggers.size());
            }
            Date newStartTime = getFirstTriggerInFuture(trigger, now);
            trigger = newTrigger().withIdentity(triggerKey(jobId.value(), JOB_GROUP_NAME)).forJob(jobDetail).withSchedule(scheduleBuilder).startAt(newStartTime).endAt(jobEndTime).build();
        }
    }
    return trigger;
}
Also used : SchedulableJobValidator.validateRepeatingSchedulableJob(org.motechproject.scheduler.validation.SchedulableJobValidator.validateRepeatingSchedulableJob) RepeatingSchedulableJob(org.motechproject.scheduler.contract.RepeatingSchedulableJob) CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Example 3 with OperableTrigger

use of org.quartz.spi.OperableTrigger in project motech by motech.

the class MotechSchedulerServiceImpl method getScheduledJobTimingsWithPrefix.

/*
     * Loads all triggers and then loops over them to find the applicable trigger using string comparison. This
     * will work regardless of the jobId being cron or repeating.
     */
@Override
public List<DateTime> getScheduledJobTimingsWithPrefix(String subject, String externalJobIdPrefix, DateTime startDate, DateTime endDate) {
    JobId jobId = new CronJobId(subject, externalJobIdPrefix);
    List<Date> messageTimings = new ArrayList<>();
    try {
        List<TriggerKey> triggerKeys = new ArrayList<TriggerKey>(scheduler.getTriggerKeys(GroupMatcher.triggerGroupContains(JOB_GROUP_NAME)));
        for (TriggerKey triggerKey : triggerKeys) {
            if (StringUtils.isNotEmpty(externalJobIdPrefix) && triggerKey.getName().contains(jobId.value())) {
                Trigger trigger = scheduler.getTrigger(triggerKey);
                messageTimings.addAll(TriggerUtils.computeFireTimesBetween((OperableTrigger) trigger, new BaseCalendar(), DateUtil.toDate(startDate), DateUtil.toDate(endDate)));
            }
        }
    } catch (SchedulerException e) {
        throw new MotechSchedulerException(String.format("Can not get scheduled job timings given subject and externalJobIdPrefix for dates : %s %s %s %s %s", subject, externalJobIdPrefix, startDate.toString(), endDate.toString(), e.getMessage()), e);
    }
    return DateUtil.datesToDateTimes(messageTimings);
}
Also used : TriggerKey(org.quartz.TriggerKey) OperableTrigger(org.quartz.spi.OperableTrigger) CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) CronJobId(org.motechproject.scheduler.contract.CronJobId) BaseCalendar(org.quartz.impl.calendar.BaseCalendar) ArrayList(java.util.ArrayList) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) RunOnceJobId(org.motechproject.scheduler.contract.RunOnceJobId) JobId(org.motechproject.scheduler.contract.JobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) RepeatingPeriodJobId(org.motechproject.scheduler.contract.RepeatingPeriodJobId) CronJobId(org.motechproject.scheduler.contract.CronJobId) Date(java.util.Date)

Example 4 with OperableTrigger

use of org.quartz.spi.OperableTrigger in project candlepin by candlepin.

the class HighlanderFactoryTest method testNewJob.

@Test
public void testNewJob() throws SchedulerException, ParseException {
    assertNotNull(hf);
    try {
        hf.newJob(null, null);
        fail("should've died with npe");
    } catch (NullPointerException npe) {
    // Expected
    }
    String crontab = "0 0 12 * * ?";
    JobDetail jd = newJob(TestJob.class).withIdentity("testjob", "group").build();
    Trigger trigger = newTrigger().withIdentity("testjob", "group").withSchedule(cronSchedule(crontab)).build();
    TriggerFiredBundle tfb = new TriggerFiredBundle(jd, (OperableTrigger) trigger, null, false, null, null, null, null);
    Job j = hf.newJob(tfb, null);
    assertNotNull(j);
}
Also used : JobDetail(org.quartz.JobDetail) OperableTrigger(org.quartz.spi.OperableTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Trigger(org.quartz.Trigger) TriggerFiredBundle(org.quartz.spi.TriggerFiredBundle) JobBuilder.newJob(org.quartz.JobBuilder.newJob) Job(org.quartz.Job) Test(org.junit.Test)

Example 5 with OperableTrigger

use of org.quartz.spi.OperableTrigger in project motech by motech.

the class MotechSchedulerServiceImpl method getScheduledJobTimings.

/*
     * Assumes that the externalJobId is non-repeating in nature. Thus the fetch is for jobId.value() and not
     * jobId.repeatingId()
     * Uses quartz API to fetch the exact triggers. Fast
     */
@Override
public List<DateTime> getScheduledJobTimings(String subject, String externalJobId, DateTime startDate, DateTime endDate) {
    JobId jobId = new CronJobId(subject, externalJobId);
    Trigger trigger;
    try {
        trigger = scheduler.getTrigger(triggerKey(jobId.value(), JOB_GROUP_NAME));
        return DateUtil.datesToDateTimes(TriggerUtils.computeFireTimesBetween((OperableTrigger) trigger, new BaseCalendar(), DateUtil.toDate(startDate), DateUtil.toDate(endDate)));
    } catch (SchedulerException e) {
        throw new MotechSchedulerException(String.format("Can not get scheduled job timings given subject and externalJobId for dates : %s %s %s %s %s", subject, externalJobId, startDate.toString(), endDate.toString(), e.getMessage()), e);
    }
}
Also used : OperableTrigger(org.quartz.spi.OperableTrigger) CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) CronJobId(org.motechproject.scheduler.contract.CronJobId) BaseCalendar(org.quartz.impl.calendar.BaseCalendar) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) RunOnceJobId(org.motechproject.scheduler.contract.RunOnceJobId) JobId(org.motechproject.scheduler.contract.JobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) RepeatingPeriodJobId(org.motechproject.scheduler.contract.RepeatingPeriodJobId) CronJobId(org.motechproject.scheduler.contract.CronJobId)

Aggregations

Trigger (org.quartz.Trigger)6 OperableTrigger (org.quartz.spi.OperableTrigger)6 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)4 Date (java.util.Date)3 CronTrigger (org.quartz.CronTrigger)3 SimpleTrigger (org.quartz.SimpleTrigger)3 CronJobId (org.motechproject.scheduler.contract.CronJobId)2 JobId (org.motechproject.scheduler.contract.JobId)2 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)2 RepeatingPeriodJobId (org.motechproject.scheduler.contract.RepeatingPeriodJobId)2 RunOnceJobId (org.motechproject.scheduler.contract.RunOnceJobId)2 MotechSchedulerException (org.motechproject.scheduler.exception.MotechSchedulerException)2 SchedulerException (org.quartz.SchedulerException)2 BaseCalendar (org.quartz.impl.calendar.BaseCalendar)2 SaturnScheduler (com.vip.saturn.job.trigger.SaturnScheduler)1 ArrayList (java.util.ArrayList)1 ExecutionTrigger (org.eclipse.scout.rt.platform.job.ExecutionTrigger)1 DateTime (org.joda.time.DateTime)1 Test (org.junit.Test)1 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)1