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);
}
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);
}
}
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());
}
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);
}
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());
}
Aggregations