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

📄 crontrigger.java

📁 定时器开源项目, 相对于 jcrontab, Quartz 算是更完整的一个项目, 随著开发的版本上来, 他已经脱离只是写在程序里面的计时器, 在指定的时间或区间, 处理所指定的事件. 也加入了 se
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    /**     * <p>     * Returns the next time at which the <code>CronTrigger</code> will fire.     * If the trigger will not fire again, <code>null</code> will be     * returned. The value returned is not guaranteed to be valid until after     * the <code>Trigger</code> has been added to the scheduler.     * </p>     */    public Date getNextFireTime() {        return this.nextFireTime;    }    /**     * <p>     * Returns the previous time at which the <code>CronTrigger</code> will     * fire. If the trigger has not yet fired, <code>null</code> will be     * returned.     */    public Date getPreviousFireTime() {        return this.previousFireTime;    }    /**     * <p>     * Sets the next time at which the <code>CronTrigger</code> will fire.     * <b>This method should not be invoked by client code.</b>     * </p>     */    public void setNextFireTime(Date nextFireTime) {        this.nextFireTime = nextFireTime;    }    /**     * <p>     * Set the previous time at which the <code>SimpleTrigger</code> fired.     * </p>     *      * <p>     * <b>This method should not be invoked by client code.</b>     * </p>     */    public void setPreviousFireTime(Date previousFireTime) {        this.previousFireTime = previousFireTime;    }    /**     * <p>     * Returns the time zone for which the <code>cronExpression</code> of     * this <code>CronTrigger</code> will be resolved.     * </p>     */    public TimeZone getTimeZone() {        if (timeZone == null) timeZone = TimeZone.getDefault();        return timeZone;    }    /**     * <p>     * Sets the time zone for which the <code>cronExpression</code> of this     * <code>CronTrigger</code> will be resolved.     * </p>     */    public void setTimeZone(TimeZone timeZone) {        this.timeZone = timeZone;    }    /**     * <p>     * Returns the next time at which the <code>CronTrigger</code> will fire,     * after the given time. If the trigger will not fire after the given time,     * <code>null</code> will be returned.     * </p>     *      * <p>     * Note that the date returned is NOT validated against the related     * org.quartz.Calendar (if any)     * </p>     */    public Date getFireTimeAfter(Date afterTime) {        if (afterTime == null) afterTime = new Date();        if (startTime.after(afterTime))                afterTime = new Date(startTime.getTime() - 1000l);        Date pot = getTimeAfter(afterTime);        if (endTime != null && pot != null && pot.after(endTime)) return null;        return pot;    }    /**     * <p>     * Returns the final time at which the <code>CronTrigger</code> will     * fire.     * </p>     *      * <p>     * Note that the return time *may* be in the past. and the date returned is     * not validated against org.quartz.calendar     * </p>     */    public Date getFinalFireTime() {        if (this.endTime != null) return getTimeBefore(this.endTime);        else            return null;    }    /**     * <p>     * Determines whether or not the <code>CronTrigger</code> will occur     * again.     * </p>     */    public boolean mayFireAgain() {        return (getNextFireTime() != null);    }    protected boolean validateMisfireInstruction(int misfireInstruction) {        if (misfireInstruction < MISFIRE_INSTRUCTION_SMART_POLICY)                return false;        if (misfireInstruction > MISFIRE_INSTRUCTION_DO_NOTHING) return false;        return true;    }    /**     * <p>     * Updates the <code>CronTrigger</code>'s state based on the     * MISFIRE_INSTRUCTION_XXX that was selected when the <code>SimpleTrigger</code>     * was created.     * </p>     *      * <p>     * If the misfire instruction is set to MISFIRE_INSTRUCTION_SMART_POLICY,     * then the following scheme will be used: <br>     * <ul>     * <li>The instruction will be interpreted as <code>MISFIRE_INSTRUCTION_FIRE_ONCE_NOW</code>     * </ul>     * </p>     */    public void updateAfterMisfire(org.quartz.Calendar cal) {        int instr = getMisfireInstruction();        if (instr == MISFIRE_INSTRUCTION_SMART_POLICY)                instr = MISFIRE_INSTRUCTION_FIRE_ONCE_NOW;        if (instr == MISFIRE_INSTRUCTION_DO_NOTHING) {            Date newFireTime = getFireTimeAfter(new Date());            while (newFireTime != null && cal != null                    && !cal.isTimeIncluded(newFireTime.getTime())) {                newFireTime = getFireTimeAfter(newFireTime);            }            setNextFireTime(newFireTime);        } else if (instr == MISFIRE_INSTRUCTION_FIRE_ONCE_NOW) {            setNextFireTime(new Date());        }    }    /**     * <p>     * Determines whether the date and (optionally) time of the given Calendar      * instance falls on a scheduled fire-time of this trigger.     * </p>     *      * <p>     * Equivalent to calling <code>willFireOn(cal, false)</code>.     * </p>     *      * @param test the date to compare     *      * @see #willFireOn(Calendar, boolean)     */    public boolean willFireOn(Calendar test) {        return willFireOn(test, false);    }        /**     * <p>     * Determines whether the date and (optionally) time of the given Calendar      * instance falls on a scheduled fire-time of this trigger.     * </p>     *      * <p>     * Note that the value returned is NOT validated against the related     * org.quartz.Calendar (if any)     * </p>     *      * @param test the date to compare     * @param dayOnly if set to true, the method will only determine if the     * trigger will fire during the day represented by the given Calendar     * (hours, minutes and seconds will be ignored).     * @see #willFireOn(Calendar)     */    public boolean willFireOn(Calendar test, boolean dayOnly) {        test.set(Calendar.MILLISECOND, 0); // don't compare millis.                if(dayOnly) {            test.set(Calendar.HOUR, 0);             test.set(Calendar.MINUTE, 0);             test.set(Calendar.SECOND, 0);         }                Date testTime = test.getTime();                Date fta = getFireTimeAfter(new Date(test.getTime().getTime() - 1000));        Calendar p = Calendar.getInstance(test.getTimeZone());        p.setTime(fta);                int year = p.get(Calendar.YEAR);        int month = p.get(Calendar.MONTH);        int day = p.get(Calendar.DATE);                if(dayOnly) {            return (year == test.get(Calendar.YEAR)                     && month == test.get(Calendar.MONTH)                     && day == test.get(Calendar.DATE));        }                while(fta.before(testTime)) {            fta = getFireTimeAfter(fta);        }                if(fta.equals(testTime))            return true;        return false;    }    /**     * <p>     * Called after the <code>{@link Scheduler}</code> has executed the     * <code>{@link org.quartz.JobDetail}</code> associated with the <code>Trigger</code>     * in order to get the final instruction code from the trigger.     * </p>     *      * @param context     *          is the <code>JobExecutionContext</code> that was used by the     *          <code>Job</code>'s<code>execute(xx)</code> method.     * @param result     *          is the <code>JobExecutionException</code> thrown by the     *          <code>Job</code>, if any (may be null).     * @return one of the Trigger.INSTRUCTION_XXX constants.     *      * @see #INSTRUCTION_NOOP     * @see #INSTRUCTION_RE_EXECUTE_JOB     * @see #INSTRUCTION_DELETE_TRIGGER     * @see #INSTRUCTION_SET_TRIGGER_COMPLETE     * @see #triggered(Calendar)     */    public int executionComplete(JobExecutionContext context,            JobExecutionException result) {        if (result != null && result.refireImmediately())                return INSTRUCTION_RE_EXECUTE_JOB;        if (result != null && result.unscheduleFiringTrigger())                return INSTRUCTION_SET_TRIGGER_COMPLETE;        if (result != null && result.unscheduleAllTriggers())                return INSTRUCTION_SET_ALL_JOB_TRIGGERS_COMPLETE;        if (!mayFireAgain()) return INSTRUCTION_DELETE_TRIGGER;        return INSTRUCTION_NOOP;    }    /**     * <p>     * Called when the <code>{@link Scheduler}</code> has decided to 'fire'     * the trigger (execute the associated <code>Job</code>), in order to     * give the <code>Trigger</code> a chance to update itself for its next     * triggering (if any).     * </p>     *      * @see #executionComplete(JobExecutionContext, JobExecutionException)     */    public void triggered(org.quartz.Calendar calendar) {        previousFireTime = nextFireTime;        nextFireTime = getFireTimeAfter(nextFireTime);        while (nextFireTime != null && calendar != null                && !calendar.isTimeIncluded(nextFireTime.getTime())) {            nextFireTime = getFireTimeAfter(nextFireTime);        }    }    /**     *       * @see org.quartz.Trigger#updateWithNewCalendar(org.quartz.Calendar, long)     */    public void updateWithNewCalendar(org.quartz.Calendar calendar, long misfireThreshold)    {        nextFireTime = getFireTimeAfter(previousFireTime);                Date now = new Date();        do {            while (nextFireTime != null && calendar != null                    && !calendar.isTimeIncluded(nextFireTime.getTime())) {                nextFireTime = getFireTimeAfter(nextFireTime);            }                        if(nextFireTime != null && nextFireTime.before(now)) {                long diff = now.getTime() - nextFireTime.getTime();                if(diff >= misfireThreshold) {                    nextFireTime = getFireTimeAfter(nextFireTime);                    continue;                }            }        }while(false);    }    /**     * <p>     * Called by the scheduler at the time a <code>Trigger</code> is first     * added to the scheduler, in order to have the <code>Trigger</code>     * compute its first fire time, based on any associated calendar.     * </p>     *      * <p>     * After this method has been called, <code>getNextFireTime()</code>     * should return a valid answer.     * </p>     *      * @return the first time at which the <code>Trigger</code> will be fired     *         by the scheduler, which is also the same value <code>getNextFireTime()</code>     *         will return (until after the first firing of the <code>Trigger</code>).     *         </p>     */    public Date computeFirstFireTime(org.quartz.Calendar calendar) {        nextFireTime = getFireTimeAfter(new Date(startTime.getTime() - 1000l));        while (nextFireTime != null && calendar != null                && !calendar.isTimeIncluded(nextFireTime.getTime())) {            nextFireTime = getFireTimeAfter(nextFireTime);        }        return nextFireTime;    }    ////////////////////////////////////////////////////////////////////////////    //    // Expression Parsing Functions    //    ////////////////////////////////////////////////////////////////////////////    protected void buildExpression(String expression) throws ParseException {        expressionParsed = true;        try {            if (seconds == null) seconds = new TreeSet();            if (minutes == null) minutes = new TreeSet();            if (hours == null) hours = new TreeSet();            if (daysOfMonth == null) daysOfMonth = new TreeSet();            if (months == null) months = new TreeSet();            if (daysOfWeek == null) daysOfWeek = new TreeSet();            if (years == null) years = new TreeSet();            int exprOn = SECOND;            StringTokenizer exprsTok = new StringTokenizer(expression, " \t",                    false);            while (exprsTok.hasMoreTokens() && exprOn <= YEAR) {                String expr = exprsTok.nextToken().trim();                StringTokenizer vTok = new StringTokenizer(expr, ",");                while (vTok.hasMoreTokens()) {                    String v = vTok.nextToken();                    storeExpressionVals(0, v, exprOn);                }                exprOn++;

⌨️ 快捷键说明

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