精进Quartz源码—Quartz调度器的Misfire处理规则(四)[通俗易懂]

Quartz调度器的Misfire处理规则调度器的启动和恢复中使用的misfire机制,还需细化!

大家好,又见面了,我是全栈君。

=================================================

对人工智能感兴趣的伙伴,分享一个我朋友的人工智能教程。零基础!通俗易懂!风趣幽默!大家可以看看是否对自己有帮助,点击这里查看教程

=================================================

欢迎关注我的公众号: Java编程技术乐园。分享技术,一起精进Quartz!

做一个积极的人

编码、改bug、提升自己

我有一个乐园,面向编程,春暖花开!

Quartz调度器的Misfire处理规则

调度器的启动和恢复中使用的misfire机制,还需细化!


一、SimpleTrigger的misfire机制 默认的 Trigger.MISFIRE_INSTRUCTION_SMART_POLICY !!!

trig.updateAfterMisfire(cal);
getMisfireInstruction() —-> misfireInstruction == 0
——以当前时间为触发频率立即触发执行

SimpleScheduleBuilder ssb = SimpleScheduleBuilder.simpleSchedule();

ssb.withMisfireHandlingInstructionFireNow();//1
ssb.withMisfireHandlingInstructionIgnoreMisfires();//2
ssb.withMisfireHandlingInstructionNextWithExistingCount();//3
ssb.withMisfireHandlingInstructionNextWithRemainingCount();//4
ssb.withMisfireHandlingInstructionNowWithExistingCount();//5
ssb.withMisfireHandlingInstructionNowWithRemainingCount();//6
//1
withMisfireHandlingInstructionFireNow  ---> misfireInstruction == 1
——以当前时间为触发频率立即触发执行
——执行至FinalTIme的剩余周期次数
——以调度或恢复调度的时刻为基准的周期频率,FinalTime根据剩余次数和当前时间计算得到
——调整后的FinalTime会略大于根据starttime计算的到的FinalTime值

//2
withMisfireHandlingInstructionIgnoreMisfires ---> misfireInstruction == -1
—以错过的第一个频率时间立刻开始执行
——重做错过的所有频率周期
——当下一次触发频率发生时间大于当前时间以后,按照Interval的依次执行剩下的频率
——共执行RepeatCount+1
//3
withMisfireHandlingInstructionNextWithExistingCount ---> misfireInstruction == 5
——不触发立即执行
——等待下次触发频率周期时刻,执行至FinalTime的剩余周期次数
——以startTime为基准计算周期频率,并得到FinalTime
——即使中间出现pause,resume以后保持FinalTime时间不变
//4
withMisfireHandlingInstructionNextWithRemainingCount ---> misfireInstruction = 4
——不触发立即执行
——等待下次触发频率周期时刻,执行至FinalTime的剩余周期次数
——以startTime为基准计算周期频率,并得到FinalTime
——即使中间出现pause,resume以后保持FinalTime时间不变
//5
withMisfireHandlingInstructionNowWithExistingCount ---> misfireInstruction = 2
——以当前时间为触发频率立即触发执行
——执行至FinalTIme的剩余周期次数
——以调度或恢复调度的时刻为基准的周期频率,FinalTime根据剩余次数和当前时间计算得到
——调整后的FinalTime会略大于根据starttime计算的到的FinalTime值
//6
withMisfireHandlingInstructionNowWithRemainingCount --- >misfireInstruction = 3
——以当前时间为触发频率立即触发执行
——执行至FinalTIme的剩余周期次数
——以调度或恢复调度的时刻为基准的周期频率,FinalTime根据剩余次数和当前时间计算得到
——调整后的FinalTime会略大于根据starttime计算的到的FinalTime值


MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT 值为 3
——此指令导致trigger忘记原始设置的starttime和repeat-count
——触发器的repeat-count将被设置为剩余的次数
——这样会导致后面无法获得原始设定的starttime和repeat-count值

updateAfterMisfire 方法源码:

/** * <p> * Updates the <code>SimpleTrigger</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>If the Repeat Count is <code>0</code>, then the instruction will * be interpreted as <code>MISFIRE_INSTRUCTION_FIRE_NOW</code>.</li> * <li>If the Repeat Count is <code>REPEAT_INDEFINITELY</code>, then * the instruction will be interpreted as <code>MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT</code>. * <b>WARNING:</b> using MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT * with a trigger that has a non-null end-time may cause the trigger to * never fire again if the end-time arrived during the misfire time span. * </li> * <li>If the Repeat Count is <code>&gt; 0</code>, then the instruction * will be interpreted as <code>MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT</code>. * </li> * </ul> * </p> * */
/* 基于在创建SimpleTrigger时选择的MISFIRE_INSTRUCTION_XXX更新SimpleTrigger的状态。 如果失火指令设置为MISFIRE_INSTRUCTION_SMART_POLICY,则将使用以下方案: •如果重复计数为0,则指令将解释为MISFIRE_INSTRUCTION_FIRE_NOW。 •如果重复计数为REPEAT_INDEFINITELY,则指令将解释为MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT。 警告:如果触发器具有非空的结束时间,则使用MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT可能会导致触发器在失火时间范围内到达结束时,不会再次触发。 •如果重复计数大于0,则指令将解释为MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT。 */
@Override
public void updateAfterMisfire(Calendar cal) { 
   
    int instr = getMisfireInstruction();//获取misfire的值,默认为0

    if(instr == Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY)//instr == -1
        return;

    if (instr == Trigger.MISFIRE_INSTRUCTION_SMART_POLICY) { 
    //instr == 1
        if (getRepeatCount() == 0) { 
   
            instr = MISFIRE_INSTRUCTION_FIRE_NOW; //instr = 1
        } else if (getRepeatCount() == REPEAT_INDEFINITELY) { 
   //getRe..Count == -1
            //instr = 4
            instr = MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT;
        } else { 
   
            // if (getRepeatCount() > 0)
            instr = MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT;//instr == 2
        }
        //instr == 1
    } else if (instr == MISFIRE_INSTRUCTION_FIRE_NOW && getRepeatCount() != 0) { 
   
        //instr == 3
        instr = MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT;
    }

    if (instr == MISFIRE_INSTRUCTION_FIRE_NOW) { 
    //instr == 1
        setNextFireTime(new Date());
        //instr == 5
    } else if (instr == MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT) { 
   
        Date newFireTime = getFireTimeAfter(new Date());
        while (newFireTime != null && cal != null
               && !cal.isTimeIncluded(newFireTime.getTime())) { 
   
            newFireTime = getFireTimeAfter(newFireTime);

            if(newFireTime == null)
                break;

            //avoid infinite loop
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.setTime(newFireTime);
            if (c.get(java.util.Calendar.YEAR) > YEAR_TO_GIVEUP_SCHEDULING_AT) { 
   
                newFireTime = null;
            }
        }
        setNextFireTime(newFireTime);
        //instr == 4
    } else if (instr == MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT) { 
   
        Date newFireTime = getFireTimeAfter(new Date());
        while (newFireTime != null && cal != null
               && !cal.isTimeIncluded(newFireTime.getTime())) { 
   
            newFireTime = getFireTimeAfter(newFireTime);

            if(newFireTime == null)
                break;

            //avoid infinite loop
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.setTime(newFireTime);
            if (c.get(java.util.Calendar.YEAR) > YEAR_TO_GIVEUP_SCHEDULING_AT) { 
   
                newFireTime = null;
            }
        }
        if (newFireTime != null) { 
   
            int timesMissed = computeNumTimesFiredBetween(nextFireTime,
                                                          newFireTime);
            setTimesTriggered(getTimesTriggered() + timesMissed);
        }

        setNextFireTime(newFireTime);
    } else if (instr == MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT) { 
    //instr == 2
        Date newFireTime = new Date();
        if (repeatCount != 0 && repeatCount != REPEAT_INDEFINITELY) { 
   
            setRepeatCount(getRepeatCount() - getTimesTriggered());
            setTimesTriggered(0);
        }

        if (getEndTime() != null && getEndTime().before(newFireTime)) { 
   
            setNextFireTime(null); // We are past the end time
        } else { 
   
            setStartTime(newFireTime);
            setNextFireTime(newFireTime);
        } 
    } else if (instr == MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT) { 
    //instr == 3
        Date newFireTime = new Date();

        int timesMissed = computeNumTimesFiredBetween(nextFireTime,
                                                      newFireTime);

        if (repeatCount != 0 && repeatCount != REPEAT_INDEFINITELY) { 
    //repeatCount == -1
            int remainingCount = getRepeatCount()
                - (getTimesTriggered() + timesMissed);
            if (remainingCount <= 0) { 
    
                remainingCount = 0;
            }
            setRepeatCount(remainingCount);
            setTimesTriggered(0);
        }

        if (getEndTime() != null && getEndTime().before(newFireTime)) { 
   
            setNextFireTime(null); // We are past the end time
        } else { 
   
            setStartTime(newFireTime);
            setNextFireTime(newFireTime);
        } 
    }

}

二、CronTrigger的misfire机制—-默认的 Trigger.MISFIRE_INSTRUCTION_SMART_POLICY !!!

trig.updateAfterMisfire(cal);
getMisfireInstruction() —-> misfireInstruction == 0

CronScheduleBuilder csb = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
				
csb.withMisfireHandlingInstructionDoNothing();
csb.withMisfireHandlingInstructionFireAndProceed();
csb.withMisfireHandlingInstructionIgnoreMisfires();
withMisfireHandlingInstructionDoNothing ---> misfireInstruction = 2
——不触发立即执行
——等待下次Cron触发频率到达时刻开始按照Cron频率依次执行
withMisfireHandlingInstructionFireAndProceed ---> misfireInstruction = 1
——以当前时间为触发频率立刻触发一次执行
——然后按照Cron频率依次执行

withMisfireHandlingInstructionIgnoreMisfires ---> misfireInstruction = -1
——以错过的第一个频率时间立刻开始执行
——重做错过的所有频率周期后
——当下一次触发频率发生时间大于当前时间后,再按照正常的Cron频率依次执行

updateAfterMisfire 方法源码:

/** * <p> * Updates the <code>CronTrigger</code>'s state based on the * MISFIRE_INSTRUCTION_XXX that was selected when the <code>CronTrigger</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> */
/* 根据创建CronTrigger时选择的MISFIRE_INSTRUCTION_XXX更新CronTrigger的状态。 如果失火指令设置为MISFIRE_INSTRUCTION_SMART_POLICY,则将使用以下方案: •指令将解释为MISFIRE_INSTRUCTION_FIRE_ONCE_NOW */

@Override
public void updateAfterMisfire(org.quartz.Calendar cal) { 
   
    int instr = getMisfireInstruction();//获取misfire的值,默认为0

    if(instr == Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY)//instr == -1
        return;

    if (instr == MISFIRE_INSTRUCTION_SMART_POLICY) { 
   //instr == 0
        instr = MISFIRE_INSTRUCTION_FIRE_ONCE_NOW;//instr = 1
    }

    if (instr == MISFIRE_INSTRUCTION_DO_NOTHING) { 
   //instr == 2
        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) { 
   //instr == 1
        setNextFireTime(new Date());
    }
}

参考文章

Quartz的Misfire处理规则
Quartz-错过触发机制

Quartz专栏系列

1.精进Quartz——Quartz大致介绍(一)
2.精进Quartz——Quartz简单入门Demo(二)
3.精进Quartz——Spring和Quartz集成详解
4.精进Quartz——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
5.精进Quartz源码——JobStore保存JonDetail和Trigger源码分析(一)
6.精进Quartz源码——scheduler.start()启动源码分析(二)
7.精进Quartz源码——QuartzSchedulerThread.run() 源码分析(三)
8.精进Quartz源码——Quartz调度器的Misfire处理规则(四)http://blog.csdn.net/u010648555/article/details/53672738)


谢谢你的阅读,如果您觉得这篇博文对你有帮助,请点赞或者喜欢,让更多的人看到!祝你每天开心愉快!


不管做什么,只要坚持下去就会看到不一样!在路上,不卑不亢!

博客首页 : http://blog.csdn.net/u010648555

愿你我在人生的路上能都变成最好的自己,能够成为一个独挡一面的人
精进Quartz源码—Quartz调度器的Misfire处理规则(四)[通俗易懂]

© 每天都在变得更好的阿飞云

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/121200.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Zabbix3.0版报警设置

    Zabbix3.0版报警设置

    2022年4月3日
    45
  • pycharm不支持32位系统怎么办_pycharm不支持32位系统吗

    pycharm不支持32位系统怎么办_pycharm不支持32位系统吗在32位操作系统中安装pycharm过程中发现的一些问题首先是下载了最新版本的pycharm安装后打开,弹出未发现可执行的文件,然后想到了其他的办法1、下载最新版本的pycharm不能直接运行,因此可以下载之前版本的pycharm,这里选择了2017.3版本的window版本下载并安装到自定义的目录,需要注意的是在安装的过程中,有一项需要勾选,添加到桌面32位安装好之后,双…

    2022年8月25日
    3
  • opengrok搭建[通俗易懂]

    opengrok搭建[通俗易懂]前序OpenGrok是一个快速,便于使用的源码搜索引擎与对照引擎,它能够帮助我们快速的搜索、定位、对照代码树。一般常用于大型的项目中,比如Android系统源码。我也是刚来公司后才发现有这个东西的,在此自己也搭建一套,方便在家里也可以查看源码。因为是查看自己定制的系统源码所以要搭这个,如果只是查看AOSP的官方源码推荐一个网站:http://androidxref.com/环境OS:Debian

    2022年4月27日
    61
  • java多线程编程面试题_linux多线程面试题

    java多线程编程面试题_linux多线程面试题一、多线程的几种实现方式,什么是线程安全。四种:继承Thread类,实现Runnable接口,实现Callable接口,使用线程池。线程安全:当多个线程访问某个类时,这个类始终都能表现出正确的行为,那么就称这个类是线程安全的。(Java并发编程实战)最核心的概念是正确性。正确性:某个类的行为与其规范完全一致。二、volatile的原理,作用,能代替锁么。volatile的理解三、画一个…

    2022年8月27日
    2
  • 用C语言进行Windows编程入门

    用C语言进行Windows编程入门用C语言进行Windows编程入门本文对一般教程或网上有的(如C语言语法等基础)不深入介绍,对初学者易造成疑惑误解或难以找到的内容进行较详尽的介绍。学习C语言很久了,一直面对控制台应用程序(Win32ConsoleApplication),没有漂亮的界面,是不是不爽呀。用C开发图形界面程序,有多种技术方案,本文希望用简单的例子,深入浅出的介绍一种方案——用C和SDK进行图形界面编程。…

    2022年6月18日
    21
  • Codeforces Round #FF (Div. 2):C. DZY Loves Sequences[通俗易懂]

    Codeforces Round #FF (Div. 2):C. DZY Loves Sequences

    2022年1月25日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号