Search in sources :

Example 1 with SimpleScheduleBuilder

use of org.quartz.SimpleScheduleBuilder in project Dempsy by Dempsy.

the class OutputQuartzHelper method getSimpleTrigger.

/**
 * Gets the simple trigger.
 *
 * @param timeUnit the time unit
 * @param timeInterval the time interval
 * @return the simple trigger
 */
public Trigger getSimpleTrigger(TimeUnit timeUnit, int timeInterval) {
    SimpleScheduleBuilder simpleScheduleBuilder = null;
    simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
    switch(timeUnit) {
        case MILLISECONDS:
            simpleScheduleBuilder.withIntervalInMilliseconds(timeInterval).repeatForever();
            break;
        case SECONDS:
            simpleScheduleBuilder.withIntervalInSeconds(timeInterval).repeatForever();
            break;
        case MINUTES:
            simpleScheduleBuilder.withIntervalInMinutes(timeInterval).repeatForever();
            break;
        case HOURS:
            simpleScheduleBuilder.withIntervalInHours(timeInterval).repeatForever();
            break;
        case DAYS:
            simpleScheduleBuilder.withIntervalInHours(timeInterval * 24).repeatForever();
            break;
        default:
            // default 1 sec
            simpleScheduleBuilder.withIntervalInSeconds(1).repeatForever();
    }
    Trigger simpleTrigger = TriggerBuilder.newTrigger().withSchedule(simpleScheduleBuilder).build();
    return simpleTrigger;
}
Also used : Trigger(org.quartz.Trigger) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder)

Example 2 with SimpleScheduleBuilder

use of org.quartz.SimpleScheduleBuilder 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 SimpleScheduleBuilder

use of org.quartz.SimpleScheduleBuilder in project motech by motech.

the class MotechSchedulerServiceImpl method scheduleRunOnceJob.

private void scheduleRunOnceJob(RunOnceSchedulableJob job, boolean update) {
    logObjectIfNotNull(job);
    validateRunOnceSchedulableJob(job);
    MotechEvent motechEvent = job.getMotechEvent();
    JobId jobId = new RunOnceJobId(motechEvent);
    JobDetail jobDetail = newJob(MotechScheduledJob.class).withIdentity(jobId.value(), JOB_GROUP_NAME).build();
    putMotechEventDataToJobDataMap(jobDetail.getJobDataMap(), motechEvent);
    Map<String, Object> metadata = new HashMap<>();
    metadata.put(UI_DEFINED, job.isUiDefined());
    metadata.putAll(motechEvent.getMetadata());
    jobDetail.getJobDataMap().put(EVENT_METADATA, metadata);
    SimpleScheduleBuilder simpleSchedule = simpleSchedule().withRepeatCount(0).withIntervalInSeconds(0).withMisfireHandlingInstructionFireNow();
    Trigger trigger = newTrigger().withIdentity(triggerKey(jobId.value(), JOB_GROUP_NAME)).forJob(jobDetail).withSchedule(simpleSchedule).startAt(DateUtil.toDate(job.getStartDate())).build();
    scheduleJob(jobDetail, trigger, update);
}
Also used : JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) HashMap(java.util.HashMap) MotechEvent(org.motechproject.event.MotechEvent) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) 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) RunOnceJobId(org.motechproject.scheduler.contract.RunOnceJobId)

Example 4 with SimpleScheduleBuilder

use of org.quartz.SimpleScheduleBuilder in project Dempsy by Dempsy.

the class QuartzHelper method getSimpleTrigger.

/**
 * Gets the simple trigger.
 *
 * @param timeUnit the time unit
 * @param timeInterval the time interval
 * @return the simple trigger
 */
public static Trigger getSimpleTrigger(final TimeUnit timeUnit, final int timeInterval, final boolean ignoreMisfires) {
    SimpleScheduleBuilder simpleScheduleBuilder = null;
    simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
    switch(timeUnit) {
        case MILLISECONDS:
            simpleScheduleBuilder.withIntervalInMilliseconds(timeInterval).repeatForever();
            break;
        case SECONDS:
            simpleScheduleBuilder.withIntervalInSeconds(timeInterval).repeatForever();
            break;
        case MINUTES:
            simpleScheduleBuilder.withIntervalInMinutes(timeInterval).repeatForever();
            break;
        case HOURS:
            simpleScheduleBuilder.withIntervalInHours(timeInterval).repeatForever();
            break;
        case DAYS:
            simpleScheduleBuilder.withIntervalInHours(timeInterval * 24).repeatForever();
            break;
        default:
            // default 1 sec
            simpleScheduleBuilder.withIntervalInSeconds(1).repeatForever();
    }
    if (ignoreMisfires)
        simpleScheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
    final Trigger simpleTrigger = TriggerBuilder.newTrigger().withSchedule(simpleScheduleBuilder).build();
    return simpleTrigger;
}
Also used : Trigger(org.quartz.Trigger) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder)

Example 5 with SimpleScheduleBuilder

use of org.quartz.SimpleScheduleBuilder in project Dempsy by Dempsy.

the class RelativeOutputSchedule method getSimpleTrigger.

/**
   * Gets the simple trigger.
   *
   * @param timeUnit the time unit
   * @param timeInterval the time interval
   * @return the simple trigger
   */
private Trigger getSimpleTrigger(TimeUnit timeUnit, int timeInterval) {
    SimpleScheduleBuilder simpleScheduleBuilder = null;
    simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
    switch(timeUnit) {
        case MILLISECONDS:
            simpleScheduleBuilder.withIntervalInMilliseconds(timeInterval).repeatForever();
            break;
        case SECONDS:
            simpleScheduleBuilder.withIntervalInSeconds(timeInterval).repeatForever();
            break;
        case MINUTES:
            simpleScheduleBuilder.withIntervalInMinutes(timeInterval).repeatForever();
            break;
        case HOURS:
            simpleScheduleBuilder.withIntervalInHours(timeInterval).repeatForever();
            break;
        case DAYS:
            simpleScheduleBuilder.withIntervalInHours(timeInterval * 24).repeatForever();
            break;
        default:
            //default 1 sec
            throw new RuntimeException("The " + RelativeOutputSchedule.class.getSimpleName() + " cannot handle a time unit of " + timeUnit);
    }
    Trigger simpleTrigger = TriggerBuilder.newTrigger().withSchedule(simpleScheduleBuilder).build();
    return simpleTrigger;
}
Also used : Trigger(org.quartz.Trigger) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder)

Aggregations

SimpleScheduleBuilder (org.quartz.SimpleScheduleBuilder)9 Trigger (org.quartz.Trigger)9 CronTrigger (org.quartz.CronTrigger)5 JobDetail (org.quartz.JobDetail)5 SimpleTrigger (org.quartz.SimpleTrigger)5 CronScheduleBuilder (org.quartz.CronScheduleBuilder)3 Scheduler (org.quartz.Scheduler)3 SchedulerException (org.quartz.SchedulerException)3 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)3 OperableTrigger (org.quartz.spi.OperableTrigger)3 Date (java.util.Date)2 DateTime (org.joda.time.DateTime)2 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)2 MotechEvent (org.motechproject.event.MotechEvent)2 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 Job (org.quartz.Job)2