1.Quartz 是用来完成任务调度的。

2.Quartz 的三个核心概念:调度器、任务、触发器。

(1)Job:通过实现该接口来定义需要执行的任务。

public interface Job {

    /**
     * Called by the <code>{@link Scheduler}</code> when a <code>{@link Trigger}</code> fires that is associated with the <code>Job</code>.
     */
    void execute(JobExecutionContext context) throws JobExecutionException;
}

(2)JobDetail:Quartz 在每次执行 Job 时,都重新创建一个 Job 实例,所以它不是直接接受一个 Job 的实例,而是接受 Job 的一个实现类,以便运行时通过反射区实例化 Job。

JobDtail 就是用来描述 Job 的实现类以及其他相关的静态信息。

package org.quartz;

import java.io.Serializable;

/**
 * Conveys the detail properties of a given <code>Job</code> instance. JobDetails are
 * to be created/defined with {@link JobBuilder}.
 * 
 * <p>
 * Quartz does not store an actual instance of a <code>Job</code> class, but
 * instead allows you to define an instance of one, through the use of a <code>JobDetail</code>.
 * </p>
 * 
 * <p>
 * <code>Job</code>s have a name and group associated with them, which
 * should uniquely identify them within a single <code>{@link Scheduler}</code>.
 * </p>
 * 
 * <p>
 * <code>Trigger</code>s are the 'mechanism' by which <code>Job</code>s
 * are scheduled. Many <code>Trigger</code>s can point to the same <code>Job</code>,
 * but a single <code>Trigger</code> can only point to one <code>Job</code>.
 * </p>*/
public interface JobDetail extends Serializable, Cloneable {

    public JobKey getKey();

    public String getDescription();

    public Class<? extends Job> getJobClass();

    public JobDataMap getJobDataMap();

    public boolean isDurable();

    public boolean isPersistJobDataAfterExecution();

    public boolean isConcurrentExectionDisallowed();

    public boolean requestsRecovery();

    public Object clone();
    
    public JobBuilder getJobBuilder();

}

(3)Trigger(中文"触发"的意思):描述出发 Job 执行的时间触发规则。

package org.quartz;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Date;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.ScheduleBuilder;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;

/**
 * The base interface with properties common to all <code>Trigger</code>s -
 * use {@link TriggerBuilder} to instantiate an actual Trigger.
 * 
 * <p>
 * <code>Triggers</code>s have a {@link TriggerKey} associated with them, which
 * should uniquely identify them within a single <code>{@link Scheduler}</code>.
 * </p>
 * 
 * <p>
 * <code>Trigger</code>s are the 'mechanism' by which <code>Job</code>s
 * are scheduled. Many <code>Trigger</code>s can point to the same <code>Job</code>,
 * but a single <code>Trigger</code> can only point to one <code>Job</code>.
 * </p>
 * 
 * <p>
 * Triggers can 'send' parameters/data to <code>Job</code>s by placing contents
 * into the <code>JobDataMap</code> on the <code>Trigger</code>.
 * </p>
 *
 * @see TriggerBuilder
 * @see JobDataMap
 * @see JobExecutionContext
 * @see TriggerUtils
 * @see SimpleTrigger
 * @see CronTrigger
 * @see CalendarIntervalTrigger
 * 
 * @author James House
 */
public interface Trigger extends Serializable, Cloneable, Comparable<Trigger> {
    long serialVersionUID = -3904243490805975570L;
    int MISFIRE_INSTRUCTION_SMART_POLICY = 0;
    int MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY = -1;
    int DEFAULT_PRIORITY = 5;

    TriggerKey getKey();

    JobKey getJobKey();

    String getDescription();

    String getCalendarName();

    JobDataMap getJobDataMap();

    int getPriority();

    boolean mayFireAgain();

    Date getStartTime();

    Date getEndTime();

    Date getNextFireTime();

    Date getPreviousFireTime();

    Date getFireTimeAfter(Date var1);

    Date getFinalFireTime();

    int getMisfireInstruction();

    TriggerBuilder<? extends Trigger> getTriggerBuilder();

    ScheduleBuilder<? extends Trigger> getScheduleBuilder();

    boolean equals(Object var1);

    int compareTo(Trigger var1);

    public static class TriggerTimeComparator implements Comparator<Trigger>, Serializable {
        private static final long serialVersionUID = -3904243490805975570L;

        public TriggerTimeComparator() {
        }

        public static int compare(Date nextFireTime1, int priority1, TriggerKey key1, Date nextFireTime2, int priority2, TriggerKey key2) {
            if(nextFireTime1 != null || nextFireTime2 != null) {
                if(nextFireTime1 == null) {
                    return 1;
                }

                if(nextFireTime2 == null) {
                    return -1;
                }

                if(nextFireTime1.before(nextFireTime2)) {
                    return -1;
                }

                if(nextFireTime1.after(nextFireTime2)) {
                    return 1;
                }
            }

            int comp = priority2 - priority1;
            return comp != 0?comp:key1.compareTo(key2);
        }

        public int compare(Trigger t1, Trigger t2) {
            return compare(t1.getNextFireTime(), t1.getPriority(), t1.getKey(), t2.getNextFireTime(), t2.getPriority(), t2.getKey());
        }
    }

    public static enum CompletedExecutionInstruction {
        NOOP,
        RE_EXECUTE_JOB,
        SET_TRIGGER_COMPLETE,
        DELETE_TRIGGER,
        SET_ALL_JOB_TRIGGERS_COMPLETE,
        SET_TRIGGER_ERROR,
        SET_ALL_JOB_TRIGGERS_ERROR;

        private CompletedExecutionInstruction() {
        }
    }

    public static enum TriggerState {
        NONE,
        NORMAL,
        PAUSED,
        COMPLETE,
        ERROR,
        BLOCKED;

        private TriggerState() {
        }
    }
}
View Code

相关文章:

  • 2021-09-20
  • 2021-11-17
  • 2018-12-27
  • 2021-09-07
  • 2021-09-25
  • 2021-11-04
  • 2021-10-09
猜你喜欢
  • 2018-03-25
  • 2018-07-29
  • 2019-07-25
  • 2020-01-17
  • 2021-12-10
  • 2018-07-14
  • 2021-09-11
  • 2021-11-12
相关资源
相似解决方案