Search in sources :

Example 6 with JobId

use of org.motechproject.scheduler.contract.JobId 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 7 with JobId

use of org.motechproject.scheduler.contract.JobId in project motech by motech.

the class MotechSchedulerServiceImpl method rescheduleJob.

@Override
public void rescheduleJob(String subject, String externalId, String cronExpression) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(subject + " " + externalId + " " + cronExpression);
    }
    assertArgumentNotNull("Subject", subject);
    assertArgumentNotNull("ExternalId", externalId);
    assertArgumentNotNull("Cron expression", cronExpression);
    JobId jobId = new CronJobId(subject, externalId);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Rescheduling the Job: %s new cron expression: %s", jobId, cronExpression));
    }
    CronTrigger trigger;
    JobDetail job;
    try {
        trigger = (CronTrigger) scheduler.getTrigger(triggerKey(jobId.value(), JOB_GROUP_NAME));
        if (trigger == null) {
            throw new MotechSchedulerException(format("Can not reschedule the job: %s The job does not exist (not scheduled)", jobId));
        }
        job = scheduler.getJobDetail(trigger.getJobKey());
    } catch (SchedulerException e) {
        throw new MotechSchedulerException(String.format("Can not reschedule the job: %s.\n Can not get a trigger associated with that job %s", jobId, e.getMessage()), e);
    } catch (ClassCastException e) {
        throw new MotechSchedulerException(String.format("Can not reschedule the job: %s.\n The trigger associated with that job is not a CronTrigger", jobId), e);
    }
    CronScheduleBuilder newCronSchedule;
    try {
        newCronSchedule = cronSchedule(cronExpression);
    } catch (RuntimeException e) {
        throw new MotechSchedulerException(String.format("Can not reschedule the job: %s Invalid Cron expression: %s", jobId, cronExpression), e);
    }
    CronTrigger newTrigger = newTrigger().withIdentity(trigger.getKey()).forJob(job).withSchedule(newCronSchedule).startAt(trigger.getStartTime()).endAt(trigger.getEndTime()).build();
    try {
        scheduler.rescheduleJob(triggerKey(jobId.value(), JOB_GROUP_NAME), newTrigger);
    } catch (SchedulerException e) {
        throw new MotechSchedulerException(String.format("Can not reschedule the job: %s %s", jobId, e.getMessage()), e);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) JobDetail(org.quartz.JobDetail) CronScheduleBuilder(org.quartz.CronScheduleBuilder) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) CronJobId(org.motechproject.scheduler.contract.CronJobId) 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)

Example 8 with JobId

use of org.motechproject.scheduler.contract.JobId in project motech by motech.

the class JobIdTest method initializeUsingMotechEvent.

@Test
public void initializeUsingMotechEvent() {
    MotechEvent motechEvent = new MotechEvent(SUBJECT_VALUE);
    motechEvent.getParameters().put(MotechSchedulerService.JOB_ID_KEY, JOB_ID_VALUE);
    JobId jobId = new CronJobId(motechEvent);
    assertEquals(String.format("%s-%s", SUBJECT_VALUE, JOB_ID_VALUE), jobId.value());
    jobId = new RepeatingJobId(motechEvent);
    assertEquals(String.format("%s-%s%s", SUBJECT_VALUE, JOB_ID_VALUE, RepeatingJobId.SUFFIX_REPEATJOBID), jobId.value());
}
Also used : CronJobId(org.motechproject.scheduler.contract.CronJobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) MotechEvent(org.motechproject.event.MotechEvent) JobId(org.motechproject.scheduler.contract.JobId) CronJobId(org.motechproject.scheduler.contract.CronJobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) Test(org.junit.Test)

Example 9 with JobId

use of org.motechproject.scheduler.contract.JobId in project motech by motech.

the class MotechSchedulerListener method handleUnscheduleRepeatingJobEvent.

/**
 * Handles the motech event unscheduling an existing repeating job.
 *
 * @param event the event to be handled
 */
@MotechListener(subjects = { UNSCHEDULE_REPEATING_JOB })
public void handleUnscheduleRepeatingJobEvent(MotechEvent event) {
    Map<String, Object> metadata = event.getMetadata();
    String jobSubject = (String) metadata.get(JOB_SUBJECT);
    MotechEvent jobEvent = new MotechEvent(jobSubject, null);
    JobId jobId = new RepeatingJobId(jobEvent);
    schedulerService.unscheduleJob(jobId);
}
Also used : RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) MotechEvent(org.motechproject.event.MotechEvent) JobId(org.motechproject.scheduler.contract.JobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) MotechListener(org.motechproject.event.listener.annotations.MotechListener)

Example 10 with JobId

use of org.motechproject.scheduler.contract.JobId in project motech by motech.

the class JobIdTest method repeatingId.

@Test
public void repeatingId() {
    JobId jobId = new RepeatingJobId(SUBJECT_VALUE, JOB_ID_VALUE);
    assertEquals(String.format("%s-%s%s", SUBJECT_VALUE, JOB_ID_VALUE, RepeatingJobId.SUFFIX_REPEATJOBID), jobId.value());
}
Also used : RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) JobId(org.motechproject.scheduler.contract.JobId) CronJobId(org.motechproject.scheduler.contract.CronJobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) Test(org.junit.Test)

Aggregations

JobId (org.motechproject.scheduler.contract.JobId)17 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)17 CronJobId (org.motechproject.scheduler.contract.CronJobId)16 RepeatingPeriodJobId (org.motechproject.scheduler.contract.RepeatingPeriodJobId)13 RunOnceJobId (org.motechproject.scheduler.contract.RunOnceJobId)13 MotechSchedulerException (org.motechproject.scheduler.exception.MotechSchedulerException)9 CronTrigger (org.quartz.CronTrigger)7 MotechEvent (org.motechproject.event.MotechEvent)6 SimpleTrigger (org.quartz.SimpleTrigger)6 Trigger (org.quartz.Trigger)6 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)6 OperableTrigger (org.quartz.spi.OperableTrigger)6 JobDetail (org.quartz.JobDetail)5 SchedulerException (org.quartz.SchedulerException)5 CronScheduleBuilder (org.quartz.CronScheduleBuilder)4 Test (org.junit.Test)3 SimpleScheduleBuilder (org.quartz.SimpleScheduleBuilder)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 DateTime (org.joda.time.DateTime)2