Quartz2.2.0 产生misfire条件参数misfireThreshold和misfire策略详细说明


首先,misfire产生的条件是:misfire的时间间隔大于配置里设置的misfireThreshold值,就认为是misfire了,错失触发了。


比如:13:07:24开始执行,重复执行5次,开始执行时,quartz已经计算好每次调度的时间刻,分别如下:

03:33:36,03:33:39,03:33:42,03:33:45,03:33:48,03:33:51

如果第一次执行时间为11s,到03:33:47结束,03:33:47减去03:33:39的时间间隔是8s,如果misfireThreshold设置的时间小于等于8s,则认为是misfire了,如果大于8s,则认为没有misfire。


假如Misfire策略是MISFIRE_INSTRUCTION_FIRE_NOW,对错失的调度不处理,


如果misfireThreshold = 9,即<prop key="org.quartz.jobStore.misfireThreshold">9000</prop>  ,处理输出如下:(并 没有misfire)

QuartzMissfireJob的任务调度[1585915091]开始:2014-02-17 03:33:36
QuartzMissfireJob的任务调度[1585915091]结束2014-02-17 03:33:47
QuartzMissfireJob的任务调度[1031004174]开始:2014-02-17 03:33:47
QuartzMissfireJob的任务调度[1031004174]结束2014-02-17 03:33:49
QuartzMissfireJob的任务调度[1762116878]开始:2014-02-17 03:33:49
QuartzMissfireJob的任务调度[1762116878]结束2014-02-17 03:33:51
QuartzMissfireJob的任务调度[297927453]开始:2014-02-17 03:33:51
QuartzMissfireJob的任务调度[297927453]结束2014-02-17 03:33:53
QuartzMissfireJob的任务调度[1141610173]开始:2014-02-17 03:33:53
QuartzMissfireJob的任务调度[1141610173]结束2014-02-17 03:33:55
QuartzMissfireJob的任务调度[1222040615]开始:2014-02-17 03:33:55
QuartzMissfireJob的任务调度[1222040615]结束2014-02-17 03:33:57

如果misfireThreshold = 8,即<prop key="org.quartz.jobStore.misfireThreshold">8000</prop>  ,处理输出如下:(misfire了)

QuartzMissfireJob的任务调度[905379918]开始:2014-02-17 03:53:33
QuartzMissfireJob的任务调度[905379918]结束2014-02-17 03:53:44
QuartzMissfireJob的任务调度[573294390]开始:2014-02-17 03:53:44
QuartzMissfireJob的任务调度[573294390]结束2014-02-17 03:53:46
QuartzMissfireJob的任务调度[1944119278]开始:2014-02-17 03:53:47
QuartzMissfireJob的任务调度[1944119278]结束2014-02-17 03:53:49
QuartzMissfireJob的任务调度[505858271]开始:2014-02-17 03:53:50
QuartzMissfireJob的任务调度[505858271]结束2014-02-17 03:53:52


在quartz里,org.quartz.jobStore.misfireThreshold: 60000,默认是60s

在spring配置文件中,如果没有配置<prop key="org.quartz.jobStore.misfireThreshold">8000</prop>  属性,那么misfireThreshold会为0



当产生misfire时,会有多种处理策略,Quartz2.2.0 + Spring4.0.0版本配置文件中misfire策略详细说明:

关键是

<property name="misfireInstruction"><value>0</value></property>
这个属性


SimpleTrigger说明:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans	http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 启动触发器的配置开始 -->
	<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
	<bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<!-- 调度清单 -->
			<list>
				<ref bean="simpleTrigger" />
			</list>
		</property>
		<!-- quartz配置 -->  
		<property name="quartzProperties">  
			<props>  
				<prop key="org.quartz.threadPool.threadCount">10</prop>  
				<prop key="org.quartz.jobStore.misfireThreshold">1</prop>  
			</props>  
		</property>  
		<!-- 初始化之后延迟3秒启动scheduler -->  
		<!-- 
		<property name="startupDelay">  
			<value>1</value>  
		</property>  
		-->
	</bean>
	
	<!-- 启动触发器的配置结束 -->

	<!-- 调度的配置开始 -->
	<!--
			quartz-1.8以前的配置 
		<bean id="myJobTrigger"
			class="org.springframework.scheduling.quartz.CronTriggerBean">
			<property name="jobDetail">
				<ref bean="myJobDetail" />
			</property>
			<property name="cronExpression">
				<value>0/1 * * * * ?</value>
			</property>
		</bean>
		-->
	<!-- quartz-2.x的配置 -->
	<!-- 定义触发时间 -->
	<!-- cronTrigger简单触发器配置 -->
	<!-- 默认是withMisfireHandlingInstructionFireAndProceed —— 以当前时间为触发频率立刻触发一次执行,然后按照Cron频率依次执行,如没有空闲进程可用,则kill掉前面那个调度,立即触发当前这个-->
	<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
		<property name="jobDetail">
			<ref bean="simpleJobDetail"/>
		</property>
		<!-- <property name="startDelay" value="1" /> -->
		<!-- 每3s执行一次 -->
		<property name="repeatInterval" value="3000" />
		<property name="repeatCount" value="5" />
		<!-- 
			MISFIRE_INSTRUCTION_FIRE_NOW    1  
			
			Instructs the Scheduler that upon a mis-fire situation, the SimpleTrigger wants to be fired now by Scheduler. 
			
			NOTE: 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 MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT . 
			立即执行,会丢失misfire job,等效于MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT
			
			
			MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT   3
			
			Instructs the Scheduler that upon a mis-fire situation, the SimpleTrigger wants to be re-scheduled to 'now' (even if the associated Calendar excludes 'now') with the repeat count set to what it would be, if it had not missed any firings. This does obey the Trigger end-time however, so if 'now' is after the end-time the Trigger will not fire again. 
			
			NOTE: Use of this instruction causes the trigger to 'forget' the start-time and repeat-count that it was originally setup with. Instead, the repeat count on the trigger will be changed to whatever the remaining repeat count is (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). 
			
			NOTE: This instruction could cause the Trigger to go to the 'COMPLETE' state after firing 'now', if all the repeat-fire-times where missed. 
			立即执行,继续重复执行的次数等于本次+剩余的次数,即剩余次数+1,misfire job不会再执行.
			比如,3s执行一次,重复执行5次,05:08:11开始执行,那么理应重复执行的时间点是05:08:11,05:08:14,05:08:17,05:08:20,05:08:23,05:08:26,
			但如果第一次执行的时间是11s,执行完的时间点是:05:08:22,,那么立即执行的时间点是05:08:22,
			而且从这点开始,还有05:08:23,05:08:26,这两次没执行,所以立即执行后,还会再执行2次。
			所以之后的执行时间点是:05:08:22,05:08:25,05:08:28

			MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT   5
			
			Instructs the Scheduler that upon a mis-fire situation, the SimpleTrigger wants to be re-scheduled to the next scheduled time after 'now' - taking into account any associated Calendar, and with the repeat count left unchanged. 
			
			NOTE/WARNING: This instruction could cause the Trigger to go directly to the 'COMPLETE' state if the end-time of the trigger has arrived. 
			不会立即执行,misfire job不会再执行,到下一个触发点再执行,继续重复执行的次数等于剩余次数-1.
			比如,3s执行一次,重复执行5次,02:28:38开始执行,那么理应重复执行的时间点是02:28:38,02:28:41,02:28:44,02:28:47,02:28:50,02:28:53
			但如果第一次执行时间用了11s,到02:28:49结束,那下次执行时间是02:28:50,02:28:53,只有(2-1)=1次,所以一次执行时间点是:02:28:50

			
			
			MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT  4
			
			Instructs the Scheduler that upon a mis-fire situation, the SimpleTrigger wants to be re-scheduled to the next scheduled time after 'now' - taking into account any associated Calendar, and with the repeat count set to what it would be, if it had not missed any firings. 
			
			NOTE/WARNING: This instruction could cause the Trigger to go directly to the 'COMPLETE' state if all fire-times where missed. 
			不会立即执行,misfire job不会再执行,到下一个触发点再执行,继续重复执行的次数等于剩余次数-1.
			会根据第一次执行的时间,然后从当前时间开始,到终止时间,计算还能执行的剩余次数,重复执行这个剩余次数。相当于重新计算剩余次数,进行调度。
			如:3s执行一次,重复执行5次,从 05:18:52开始,理论上依次执行的时间点是 05:18:55,05:18:58,05:19:01,05:19:04,05:19:07
			但如果第一次执行了11s,到05:19:03结束,则继续按照本来的调度,下次执行的开始时间是05:19:04,接着执行05:19:07
			且错过的05:18:55,05:18:58,05:19:01的触发不会再执行下去


			MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT  2
			
			Instructs the Scheduler that upon a mis-fire situation, the SimpleTrigger wants to be re-scheduled to 'now' (even if the associated Calendar excludes 'now') with the repeat count left as-is. This does obey the Trigger end-time however, so if 'now' is after the end-time the Trigger will not fire again. 
			
			NOTE: 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). 
			立即执行,不会丢失misfire job,从当前时刻开始重新计算每次执行时间点,重做misfire的job
			如:3s执行一次,重复执行5次,05:24:44开始,每次时间点理应是:05:24:47,05:24:50,05:24:53,05:24:56,05:24:59
			但由于第一次执行用了11s,到05:24:55结束,那么从05:24:55开始重新计算剩余次数5次的每次执行的时间点,假设剩余执行每次只需执行2s时间,不会Misfire
			那么剩余执行调度的时间点是:05:24:55,05:24:58,05:25:01,05:25:04,05:25:07
			
			
			
			MISFIRE_INSTRUCTION_SMART_POLICY    0   default 
			
			Instructs the Scheduler that upon a mis-fire situation, the updateAfterMisfire() method will be called on the Trigger to determine the mis-fire instruction, which logic will be trigger-implementation-dependent. 
			
			In order to see if this instruction fits your needs, you should look at the documentation for the getSmartMisfirePolicy() method on the particular Trigger implementation you are using.
			
			如果没有自定义的话,当misfire后,立即执行,不会丢失misfire job... 跟MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT差不多策略
		-->
		<property name="misfireInstruction"><value>0</value></property>
	</bean>
	<!-- 调度的配置结束 -->

	<!-- job的配置开始 -->
	<!-- 定义调用对象和调用对象的方法 -->
	<bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="missfireJob" />
		<property name="targetMethod" value="work" />
		<!-- <property name="concurrent" value="false" /> -->
		<!-- 同步执行 -->
		<property name="concurrent" value="false" />
	</bean>
	<!-- job的配置结束 -->

	<!-- 要调用的工作类 -->
	<bean id="missfireJob" class="com.liangbinny.quartz.example6.QuartzMissfireJob" />
</beans>




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值