⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpletrigger.java

📁 定时器开源项目, 相对于 jcrontab, Quartz 算是更完整的一个项目, 随著开发的版本上来, 他已经脱离只是写在程序里面的计时器, 在指定的时间或区间, 处理所指定的事件. 也加入了 se
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright James House (c) 2001-2004, *  * All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: 1. * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. *  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *   */package org.quartz;import java.util.Date;import org.quartz.helpers.TriggerUtils;/** * <p> * A concrete <code>{@link Trigger}</code> that is used to fire a <code>{@link org.quartz.JobDetail}</code> * at a given moment in time, and optionally repeated at a specified interval. * </p> *  * @see Trigger * @see CronTrigger *  * @author James House, with contributions by Lieven Govaerts of Ebitec Nv, *         Belgium. */public class SimpleTrigger extends Trigger {    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Constants.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    /**     * <p>     * Instructs the <code>{@link Scheduler}</code> that upon a mis-fire     * situation, the <code>{@link SimpleTrigger}</code> wants to be fired     * now by <code>Scheduler</code>.     * </p>     *      * <p>     * <i>NOTE:</i> This instruction should typically only be used for     * 'one-shot' (non-repeating) Triggers. If it is used on a trigger with a     * repeat count > 0 then it is equivalent to the instruction <code>{@link #MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT}     * </code>.     * </p>     */    public static final int MISFIRE_INSTRUCTION_FIRE_NOW = 1;    /**     * <p>     * Instructs the <code>{@link Scheduler}</code> that upon a mis-fire     * situation, the <code>{@link SimpleTrigger}</code> wants to be     * re-scheduled to 'now' (even if the associated <code>{@link Calendar}</code>     * excludes 'now') with the repeat count left as-is.     * </p>     *      * <p>     * <i>NOTE:</i> Use of this instruction causes the trigger to 'forget'     * the start-time and repeat-count that it was originally setup with (this     * is only an issue if you for some reason wanted to be able to tell what     * the original values were at some later time).     * </p>     *      * <p>     * <i>NOTE:</i> This instruction could cause the <code>Trigger</code>     * to go to the 'COMPLETE' state after firing 'now', if all the     * repeat-fire-times where missed.     * </p>     */    public static final int MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT = 2;    /**     * <p>     * Instructs the <code>{@link Scheduler}</code> that upon a mis-fire     * situation, the <code>{@link SimpleTrigger}</code> wants to be     * re-scheduled to 'now' (even if the associated <code>{@link Calendar}</code>     * excludes 'now') with the repeat count set to what it would be, if it had     * not missed any firings.     * </p>     *      * <p>     * <i>NOTE:</i> Use of this instruction causes the trigger to 'forget'     * the start-time and repeat-count that it was originally setup with (this     * is only an issue if you for some reason wanted to be able to tell what     * the original values were at some later time).     * </p>     *      * <p>     * <i>NOTE:</i> This instruction could cause the <code>Trigger</code>     * to go to the 'COMPLETE' state after firing 'now', if all the     * repeat-fire-times where missed.     * </p>     */    public static final int MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT = 3;    /**     * <p>     * Instructs the <code>{@link Scheduler}</code> that upon a mis-fire     * situation, the <code>{@link SimpleTrigger}</code> wants to be     * re-scheduled to the next scheduled time after 'now' - taking into     * account any associated <code>{@link Calendar}</code>, and with the     * repeat count set to what it would be, if it had not missed any firings.     * </p>     *      * <p>     * <i>NOTE/WARNING:</i> This instruction could cause the <code>Trigger</code>     * to go directly to the 'COMPLETE' state if all fire-times where missed.     * </p>     */    public static final int MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT = 4;    /**     * <p>     * Instructs the <code>{@link Scheduler}</code> that upon a mis-fire     * situation, the <code>{@link SimpleTrigger}</code> wants to be     * re-scheduled to the next scheduled time after 'now' - taking into     * account any associated <code>{@link Calendar}</code>, and with the     * repeat count left unchanged.     * </p>     *      * <p>     * <i>NOTE:</i> Use of this instruction causes the trigger to 'forget'     * the repeat-count that it was originally setup with (this is only an     * issue if you for some reason wanted to be able to tell what the original     * values were at some later time).     * </p>     *      * <p>     * <i>NOTE/WARNING:</i> This instruction could cause the <code>Trigger</code>     * to go directly to the 'COMPLETE' state if all fire-times where missed.     * </p>     */    public static final int MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT = 5;    /**     * <p>     * Used to indicate the 'repeat count' of the trigger is indefinite. Or in     * other words, the trigger should repeat continually until the trigger's     * ending timestamp.     * </p>     */    public static int REPEAT_INDEFINITELY = -1;    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Data members.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    private Date startTime = null;    private Date endTime = null;    private Date nextFireTime = null;    private Date previousFireTime = null;    private int repeatCount = 0;    private long repeatInterval = 0;    private int timesTriggered = 0;    private boolean complete = false;    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Constructors.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    /**     * <p>     * Create a <code>SimpleTrigger</code> with no settings.     * </p>     */    public SimpleTrigger() {        super();    }    /**     * <p>     * Create a <code>SimpleTrigger</code> that will occur immediately, and     * not repeat.     * </p>     */    public SimpleTrigger(String name, String group) {        this(name, group, new Date(), null, 0, 0);    }    /**     * <p>     * Create a <code>SimpleTrigger</code> that will occur immediately, and     * repeat at the the given interval the given number of times.     * </p>     */    public SimpleTrigger(String name, String group, int repeatCount,            long repeatInterval) {        this(name, group, new Date(), null, repeatCount, repeatInterval);    }    /**     * <p>     * Create a <code>SimpleTrigger</code> that will occur at the given time,     * and not repeat.     * </p>     */    public SimpleTrigger(String name, String group, Date startTime) {        this(name, group, startTime, null, 0, 0);    }    /**     * <p>     * Create a <code>SimpleTrigger</code> that will occur at the given time,     * and repeat at the the given interval the given number of times, or until     * the given end time.     * </p>     *      * @param startTime     *          A <code>Date</code> set to the time for the <code>Trigger</code>     *          to fire.     * @param endTime     *          A <code>Date</code> set to the time for the <code>Trigger</code>     *          to quit repeat firing.     * @param repeatCount     *          The number of times for the <code>Trigger</code> to repeat     *          firing, use {@link #REPEAT_INDEFINITELY}for unlimitted times.     * @param repeatInterval     *          The number of milliseconds to pause between the repeat firing.     */    public SimpleTrigger(String name, String group, Date startTime,            Date endTime, int repeatCount, long repeatInterval) {        super(name, group);        setStartTime(startTime);        setEndTime(endTime);        setRepeatCount(repeatCount);        setRepeatInterval(repeatInterval);    }    /**     * <p>     * Create a <code>SimpleTrigger</code> that will occur at the given time,     * fire the identified <code>Job</code> and repeat at the the given     * interval the given number of times, or until the given end time.     * </p>     *      * @param startTime     *          A <code>Date</code> set to the time for the <code>Trigger</code>     *          to fire.     * @param endTime     *          A <code>Date</code> set to the time for the <code>Trigger</code>     *          to quit repeat firing.     * @param repeatCount     *          The number of times for the <code>Trigger</code> to repeat     *          firing, use {@link #REPEAT_INDEFINITELY}for unlimitted times.     * @param repeatInterval     *          The number of milliseconds to pause between the repeat firing.     */    public SimpleTrigger(String name, String group, String jobName,            String jobGroup, Date startTime, Date endTime, int repeatCount,            long repeatInterval) {        super(name, group, jobName, jobGroup);        setStartTime(startTime);        setEndTime(endTime);        setRepeatCount(repeatCount);        setRepeatInterval(repeatInterval);    }    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Interface.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    /**     * <p>     * Get the time at which the <code>SimpleTrigger</code> should occur.     * </p>     */    public Date getStartTime() {        return startTime;    }    /**     * <p>     * Set the time at which the <code>SimpleTrigger</code> should occur.     * </p>     *      * @exception IllegalArgumentException     *              if startTime is <code>null</code>.     */    public void setStartTime(Date startTime) {        if (startTime == null)                throw new IllegalArgumentException("Start time cannot be null");        Date eTime = getEndTime();        if (eTime != null && startTime != null && eTime.before(startTime))            throw new IllegalArgumentException(            "End time cannot be before start time");        this.startTime = startTime;    }    /**     * <p>     * Get the time at which the <code>SimpleTrigger</code> should quit     * repeating - even if repeastCount isn't yet satisfied.     * </p>     *      * @see #getFinalFireTime()     */    public Date getEndTime() {        return endTime;    }    /**     * <p>     * Set the time at which the <code>SimpleTrigger</code> should quit     * repeating (and be automatically deleted).     * </p>     *      * @exception IllegalArgumentException     *              if endTime is before start time.     */    public void setEndTime(Date endTime) {        Date sTime = getStartTime();        if (sTime != null && endTime != null && sTime.after(endTime))                throw new IllegalArgumentException(                        "End time cannot be before start time");        this.endTime = endTime;    }    /**     * <p>     * Get the the number of times the <code>SimpleTrigger</code> should     * repeat, after which it will be automatically deleted.     * </p>     *      * @see #REPEAT_INDEFINITELY     */    public int getRepeatCount() {        return repeatCount;    }    /**     * <p>     * Set the the number of time the <code>SimpleTrigger</code> should     * repeat, after which it will be automatically deleted.     * </p>     *      * @see #REPEAT_INDEFINITELY     * @exception IllegalArgumentException     *              if repeatCount is < 0     */    public void setRepeatCount(int repeatCount) {        if (repeatCount < 0 && repeatCount != REPEAT_INDEFINITELY)                throw new IllegalArgumentException(                        "Repeat count must be >= 0, use the "                                + "constant REPEAT_INDEFINITELY for infinite.");        this.repeatCount = repeatCount;    }    /**     * <p>     * Get the the time interval (in milliseconds) at which the <code>SimpleTrigger</code>     * should repeat.     * </p>     */    public long getRepeatInterval() {        return repeatInterval;    }    /**     * <p>     * Set the the time interval (in milliseconds) at which the <code>SimpleTrigger</code>     * should repeat.     * </p>     *      * @exception IllegalArgumentException     *              if repeatInterval is <= 0     */    public void setRepeatInterval(long repeatInterval) {        if (repeatInterval < 0)                throw new IllegalArgumentException(                        "Repeat interval must be >= 0");        this.repeatInterval = repeatInterval;    }    /**     * <p>     * Get the number of times the <code>SimpleTrigger</code> has already

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -