Search in sources :

Example 1 with ScheduleBuilder

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

the class MdsSchedulerServiceImpl method scheduleRepeatingJob.

@Override
public void scheduleRepeatingJob(long interval) {
    Date jobStartTime = DateUtil.nowUTC().toDate();
    if (interval == 0) {
        throw new IllegalArgumentException("Invalid RepeatingSchedulableJob. The job repeat interval can not be 0");
    }
    JobDetail jobDetail = newJob(MdsScheduledJob.class).withIdentity(jobKey(EMPTY_TRASH_JOB, JOB_GROUP_NAME)).build();
    ScheduleBuilder scheduleBuilder = simpleSchedule().withIntervalInMilliseconds(interval).withRepeatCount(MAX_REPEAT_COUNT);
    Trigger trigger = buildJobDetail(jobStartTime, jobDetail, scheduleBuilder);
    scheduleJob(jobDetail, trigger);
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) ScheduleBuilder(org.quartz.ScheduleBuilder) Date(java.util.Date)

Example 2 with ScheduleBuilder

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

the class MotechSchedulerServiceImpl method scheduleRepeatingJob.

private void scheduleRepeatingJob(RepeatingSchedulableJob job, boolean update) {
    logObjectIfNotNull(job);
    validateRepeatingSchedulableJob(job);
    MotechEvent motechEvent = job.getMotechEvent();
    DateTime jobStartTime = job.getStartDate();
    DateTime jobEndTime = job.getEndDate();
    Integer repeatIntervalInSeconds = job.getRepeatIntervalInSeconds();
    Integer jobRepeatCount = job.getRepeatCount();
    if (null == jobRepeatCount) {
        jobRepeatCount = MAX_REPEAT_COUNT;
    }
    JobId jobId = new RepeatingJobId(motechEvent);
    JobDetail jobDetail = newJob(MotechScheduledJob.class).withIdentity(jobKey(jobId.value(), JOB_GROUP_NAME)).build();
    putMotechEventDataToJobDataMap(jobDetail.getJobDataMap(), motechEvent);
    jobDetail.getJobDataMap().put(EVENT_METADATA, createMetadataForMisfireSchedulableJob(job, motechEvent));
    try {
        if (scheduler.getTrigger(triggerKey(jobId.value(), JOB_GROUP_NAME)) != null) {
            unscheduleJob(jobId);
        }
    } catch (SchedulerException e) {
        throw new MotechSchedulerException(format("Schedule or reschedule the job: %s.\n%s", jobId, e.getMessage()), "scheduler.error.cantRescheduleJob", Arrays.asList(jobId.value(), e.getMessage()), e);
    }
    ScheduleBuilder scheduleBuilder;
    if (!job.isUseOriginalFireTimeAfterMisfire()) {
        SimpleScheduleBuilder simpleSchedule = simpleSchedule().withIntervalInSeconds(repeatIntervalInSeconds).withRepeatCount(jobRepeatCount);
        simpleSchedule = setMisfirePolicyForSimpleTrigger(simpleSchedule, schedulerSettings.getProperty("scheduler.repeating.trigger.misfire.policy"));
        scheduleBuilder = simpleSchedule;
    } else {
        if (job.getRepeatCount() != null) {
            final double half = 0.5;
            jobEndTime = new DateTime((long) (job.getStartDate().getMillis() + repeatIntervalInSeconds * MILLISECOND * (job.getRepeatCount() + half)));
        }
        scheduleBuilder = CalendarIntervalScheduleBuilder.calendarIntervalSchedule().withIntervalInSeconds(repeatIntervalInSeconds).withMisfireHandlingInstructionFireAndProceed();
    }
    Trigger trigger = buildJobDetail(job, DateUtil.toDate(jobStartTime), DateUtil.toDate(jobEndTime), jobId, jobDetail, scheduleBuilder);
    scheduleJob(jobDetail, trigger, update);
}
Also used : JobDetail(org.quartz.JobDetail) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) PeriodIntervalScheduleBuilder(org.motechproject.scheduler.trigger.PeriodIntervalScheduleBuilder) ScheduleBuilder(org.quartz.ScheduleBuilder) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) CalendarIntervalScheduleBuilder(org.quartz.CalendarIntervalScheduleBuilder) CronScheduleBuilder(org.quartz.CronScheduleBuilder) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) MotechEvent(org.motechproject.event.MotechEvent) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) 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)

Example 3 with ScheduleBuilder

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

the class MotechSchedulerServiceImpl method scheduleRepeatingPeriodJob.

private void scheduleRepeatingPeriodJob(RepeatingPeriodSchedulableJob job, boolean update) {
    logObjectIfNotNull(job);
    validateRepeatingPeriodSchedulableJob(job);
    MotechEvent motechEvent = job.getMotechEvent();
    JobId jobId = new RepeatingPeriodJobId(motechEvent);
    JobDetail jobDetail = newJob(MotechScheduledJob.class).withIdentity(jobKey(jobId.value(), JOB_GROUP_NAME)).build();
    putMotechEventDataToJobDataMap(jobDetail.getJobDataMap(), motechEvent);
    jobDetail.getJobDataMap().put(EVENT_METADATA, createMetadataForMisfireSchedulableJob(job, motechEvent));
    ScheduleBuilder scheduleBuilder = PeriodIntervalScheduleBuilder.periodIntervalSchedule().withRepeatPeriod(job.getRepeatPeriod()).withMisfireHandlingInstructionFireAndProceed();
    Trigger trigger = buildJobDetail(job, DateUtil.toDate(job.getStartDate()), DateUtil.toDate(job.getEndDate()), jobId, jobDetail, scheduleBuilder);
    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) PeriodIntervalScheduleBuilder(org.motechproject.scheduler.trigger.PeriodIntervalScheduleBuilder) ScheduleBuilder(org.quartz.ScheduleBuilder) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) CalendarIntervalScheduleBuilder(org.quartz.CalendarIntervalScheduleBuilder) CronScheduleBuilder(org.quartz.CronScheduleBuilder) RepeatingPeriodJobId(org.motechproject.scheduler.contract.RepeatingPeriodJobId) MotechEvent(org.motechproject.event.MotechEvent) 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

JobDetail (org.quartz.JobDetail)3 ScheduleBuilder (org.quartz.ScheduleBuilder)3 Trigger (org.quartz.Trigger)3 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)3 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 PeriodIntervalScheduleBuilder (org.motechproject.scheduler.trigger.PeriodIntervalScheduleBuilder)2 CalendarIntervalScheduleBuilder (org.quartz.CalendarIntervalScheduleBuilder)2 CronScheduleBuilder (org.quartz.CronScheduleBuilder)2 CronTrigger (org.quartz.CronTrigger)2 SimpleScheduleBuilder (org.quartz.SimpleScheduleBuilder)2 SimpleTrigger (org.quartz.SimpleTrigger)2 OperableTrigger (org.quartz.spi.OperableTrigger)2 Date (java.util.Date)1 DateTime (org.joda.time.DateTime)1 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)1