Search in sources :

Example 1 with SchedulableJob

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

the class SchedulableJobBuilder method buildJob.

/**
 * Builds a job based on the given key, data map and trigger.
 *
 * @param key  the job key
 * @param dataMap  the job data map
 * @param trigger  the job trigger
 * @return  the created job
 * @throws SchedulerException when there were problems while building job
 */
public static SchedulableJob buildJob(JobKey key, JobDataMap dataMap, Trigger trigger) throws SchedulerException {
    SchedulableJob job;
    Map<String, Object> params = dataMap.getWrappedMap();
    Map<String, Object> metadata = (Map<String, Object>) params.get(EVENT_METADATA);
    params.remove(EVENT_METADATA);
    params.putAll(metadata);
    JobDataMap map = new JobDataMap(params);
    switch(getJobType(key, map)) {
        case CRON:
            job = buildCronSchedulableJob(trigger, map);
            break;
        case REPEATING:
            job = buildRepeatingSchedulableJob(trigger, map);
            break;
        case REPEATING_PERIOD:
            job = buildRepeatingPeriodSchedulableJob(trigger, map);
            break;
        case DAY_OF_WEEK:
            job = buildDayOfWeekSchedulableJob(trigger, map);
            break;
        case RUN_ONCE:
            job = buildRunOnceSchedulableJob();
            break;
        default:
            throw new MotechSchedulerException(String.format("Unknown job type: \n %s\n %s", key.getName(), key.getGroup()));
    }
    job.setMotechEvent(new MotechEvent(dataMap.getString(EVENT_TYPE_KEY_NAME), map.getWrappedMap()));
    job.setUiDefined(map.getBoolean(UI_DEFINED));
    job.setStartDate(new DateTime(trigger.getStartTime()));
    return job;
}
Also used : RunOnceSchedulableJob(org.motechproject.scheduler.contract.RunOnceSchedulableJob) SchedulableJob(org.motechproject.scheduler.contract.SchedulableJob) RepeatingSchedulableJob(org.motechproject.scheduler.contract.RepeatingSchedulableJob) RepeatingPeriodSchedulableJob(org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob) CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) DayOfWeekSchedulableJob(org.motechproject.scheduler.contract.DayOfWeekSchedulableJob) JobDataMap(org.quartz.JobDataMap) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) Map(java.util.Map) JobDataMap(org.quartz.JobDataMap) MotechEvent(org.motechproject.event.MotechEvent) DateTime(org.joda.time.DateTime)

Aggregations

Map (java.util.Map)1 DateTime (org.joda.time.DateTime)1 MotechEvent (org.motechproject.event.MotechEvent)1 CronSchedulableJob (org.motechproject.scheduler.contract.CronSchedulableJob)1 DayOfWeekSchedulableJob (org.motechproject.scheduler.contract.DayOfWeekSchedulableJob)1 RepeatingPeriodSchedulableJob (org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob)1 RepeatingSchedulableJob (org.motechproject.scheduler.contract.RepeatingSchedulableJob)1 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)1 SchedulableJob (org.motechproject.scheduler.contract.SchedulableJob)1 MotechSchedulerException (org.motechproject.scheduler.exception.MotechSchedulerException)1 JobDataMap (org.quartz.JobDataMap)1