JobBuilder.java 5.16 KB
package com.yoho.quartz.job;

import com.yoho.quartz.contants.Contants;
import com.yoho.quartz.domain.JobDetail;
import com.yoho.quartz.job.inner.YohoInternalJob;
import com.yoho.quartz.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.quartz.*;
import org.quartz.spi.OperableTrigger;

import java.util.*;
import java.util.Calendar;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * Author:yanzhang.fu
 * Date:2018/1/9
 * Description:
 * Modified By:
 **/
public class JobBuilder {

    public OperableTrigger buildTrigger(JobDetail jobDetail) {
        Trigger trigger = null;

        if (jobDetail.getMisfiredPolicy() == null) {
            jobDetail.setMisfiredPolicy(0);
        }
        if (jobDetail.getPriority() == null) {
            jobDetail.setPriority(5);
        }
        if (jobDetail.getJobType() == Contants.JobType.CRON.getType()) {
            CronScheduleBuilder builder = cronSchedule(jobDetail.getCron());
            switch (jobDetail.getMisfiredPolicy()) {
                case CronTrigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY:
                    builder = builder.withMisfireHandlingInstructionIgnoreMisfires();
                    break;
                case CronTrigger.MISFIRE_INSTRUCTION_SMART_POLICY:

                    break;
                case CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING:

                    builder = builder.withMisfireHandlingInstructionDoNothing();
                    break;
                case CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW:

                    builder = builder.withMisfireHandlingInstructionFireAndProceed();
                    break;
                default:
                    break;
            }
            Date startDate = new Date();
            if (StringUtils.isNotEmpty(jobDetail.getEndDate())) {
                if (DateUtils.string2Date(jobDetail.getEndDate(), DateUtils.DATE_TIME_FORMAT).before(new Date())) {
                    startDate = DateUtils.getBeforeTime(DateUtils.string2Date(jobDetail.getEndDate(), DateUtils.DATE_TIME_FORMAT), 1);
                }
            }
            trigger = newTrigger().withIdentity(jobDetail.getId(), jobDetail.getJobGroup()).startAt(startDate).endAt(DateUtils.string2Date(jobDetail.getEndDate(), DateUtils.DATE_TIME_FORMAT)).withSchedule(builder).withPriority(jobDetail.getPriority()).build();

        } else {
            SimpleScheduleBuilder builder = simpleSchedule().simpleSchedule().withIntervalInSeconds(jobDetail.getInterval())
                    .withRepeatCount(jobDetail.getRepeatCount());
            switch (jobDetail.getMisfiredPolicy()) {
                case Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY:
                    builder.withMisfireHandlingInstructionIgnoreMisfires();
                    break;
                case SimpleTrigger.MISFIRE_INSTRUCTION_SMART_POLICY:
                    break;
                case SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW:
                    builder.withMisfireHandlingInstructionFireNow();
                    break;
                case SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT:
                    builder.withMisfireHandlingInstructionNowWithExistingCount();
                    break;
                case SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT:
                    builder.withMisfireHandlingInstructionNowWithRemainingCount();
                    break;
                case SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT:
                    builder.withMisfireHandlingInstructionNextWithRemainingCount();
                    break;
                case SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT:
                    builder.withMisfireHandlingInstructionNextWithExistingCount();
                    break;
                default:
                    break;
            }

            trigger = newTrigger().withIdentity(jobDetail.getId(), jobDetail.getJobGroup())
                    .startAt(DateUtils.string2Date(jobDetail.getStartDate(), DateUtils.DATE_TIME_FORMAT)).withSchedule(builder).withPriority(jobDetail.getPriority()).build();

        }
        OperableTrigger trig = (OperableTrigger) trigger;
        if (null == trigger.getJobKey()) {
            trig.setJobKey(new JobKey(jobDetail.getId(), jobDetail.getJobGroup()));
        }
        trig.computeFirstFireTime(null);
        return trig;
    }

    public org.quartz.JobDetail buildJobDetail(JobDetail jobDetail) {
        org.quartz.JobDetail job = null;

        job = newJob(YohoInternalJob.class)
                .withIdentity(jobDetail.getId(), jobDetail.getJobGroup()).withDescription(jobDetail.getDesc()).storeDurably()
                .build();
        job.getJobDataMap().put(Contants.JobDataMapKey.PROCESSOR, jobDetail.getJobClass());
        job.getJobDataMap().put(Contants.JobDataMapKey.JOB_CONTEXT, jobDetail.getJobContext());
        job.getJobDataMap().put(Contants.JobDataMapKey.MODULE_KEY, jobDetail.getModule());
        return job;
    }

}