quartz misfire机制

参数解释

org.quartz.jobStore.misfireThreshold:
scheduler可以忍受未被调度的时间与下一次执行的时间的阈值,如果超出这个阈值,就不会被重新调用,默认一分钟,单位毫秒
org.quartz.jobStore.maxMisfiresToHandleAtATime:
每次可以执行的最大misfired的触发器数.如果设置很大的话,数据库在一瞬间就会锁住,性能将会受到很大的影响

核心代码

计算出可以fire的时间getMisfireTime,然后从数据库trigger表里面获取出数据,挨个进行触发,然后更新数据库trigger表

protected RecoverMisfiredJobsResult recoverMisfiredJobs(
        Connection conn, boolean recovering)
        throws JobPersistenceException, SQLException {

        // If recovering, we want to handle all of the misfired
        // triggers right away.
        int maxMisfiresToHandleAtATime = 
            (recovering) ? -1 : getMaxMisfiresToHandleAtATime();//获取单次执行misfire的最大数
        
        List misfiredTriggers = new LinkedList();
        long earliestNewTime = Long.MAX_VALUE;
        // We must still look for the MISFIRED state in case triggers were left 
        // in this state when upgrading to this version that does not support it. 
        boolean hasMoreMisfiredTriggers =
            getDelegate().hasMisfiredTriggersInState(
                conn, STATE_WAITING, getMisfireTime(), 
                maxMisfiresToHandleAtATime, misfiredTriggers);
                //查看trigger表,将需要触发的misfired的trigger放入misfiredTriggers中.如果超过maxMisfiresToHandleAtATime,返回值为true,否则false

        if (hasMoreMisfiredTriggers) {
            getLog().info(
                "Handling the first " + misfiredTriggers.size() +
                " triggers that missed their scheduled fire-time.  " +
                "More misfired triggers remain to be processed.");
        } else if (misfiredTriggers.size() > 0) { 
            getLog().info(
                "Handling " + misfiredTriggers.size() + 
                " trigger(s) that missed their scheduled fire-time.");
        } else {
            getLog().debug(
                "Found 0 triggers that missed their scheduled fire-time.");
            return RecoverMisfiredJobsResult.NO_OP; 
        }

        for (TriggerKey triggerKey: misfiredTriggers) {
            
            OperableTrigger trig = 
                retrieveTrigger(conn, triggerKey);

            if (trig == null) {
                continue;
            }

            doUpdateOfMisfiredTrigger(conn, trig, false, STATE_WAITING, recovering);//更新trigger的next_time

            if(trig.getNextFireTime() != null && trig.getNextFireTime().getTime() < earliestNewTime)
                earliestNewTime = trig.getNextFireTime().getTime();
        }

        return new RecoverMisfiredJobsResult(
                hasMoreMisfiredTriggers, misfiredTriggers.size(), earliestNewTime);
    }

你可能感兴趣的:(quartz misfire机制)