Quartz Java resuming a job excecutes it many times--转
2016-05-11 17:58
495 查看
原文地址:http://stackoverflow.com/questions/1933676/quartz-java-resuming-a-job-excecutes-it-many-times
Question:
For my application i create jobs and schedule them with CronTriggers. Each job has only one trigger and both the job name and the trigger names are the same. No jobs share a trigger.
Now when i create a cron trigger like this "0/1 * * * * ?" which instructs the job to execute every second, it works just fine.
The problem rises when i first pause the job by calling :
and then resuming the job after let's say 50 seconds with :
What i see is that for these 50 seconds the job did not execute as requested. But the moment i resume the job i see 50 executions of the job at the same time!!!
I thought that this was due to the default setting for the misfire instruction but even after setting the trigger's misfire instruciton upon creation to this :
The same thing happens. Can anyone suggest a way to fix this?
answer:
The
The problem is, the trigger doesn't know it is being paused. To overcome this there is this misfire handling. After resuming the jobs the trigger's
Since you have problems I assume it is not. ;) To workaround this you can modify the threshold or use a simple wrapper around
Question:
For my application i create jobs and schedule them with CronTriggers. Each job has only one trigger and both the job name and the trigger names are the same. No jobs share a trigger.
Now when i create a cron trigger like this "0/1 * * * * ?" which instructs the job to execute every second, it works just fine.
The problem rises when i first pause the job by calling :
scheduler.pauseJob(jobName, jobGroup);
and then resuming the job after let's say 50 seconds with :
scheduler.resumeJob(jobName, jobGroup);
What i see is that for these 50 seconds the job did not execute as requested. But the moment i resume the job i see 50 executions of the job at the same time!!!
I thought that this was due to the default setting for the misfire instruction but even after setting the trigger's misfire instruciton upon creation to this :
trigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
The same thing happens. Can anyone suggest a way to fix this?
answer:
The
CronTriggerworks by remembering the
nextFireTime. After creating the trigger the
nextFireTimeis initialized. Every time the job is triggered
nextFireTimeis updated. Since the job is not triggered when paused
nextFireTimeremains "old". So after you resume the job the trigger will return every old trigger time.
The problem is, the trigger doesn't know it is being paused. To overcome this there is this misfire handling. After resuming the jobs the trigger's
updateAfterMisfire()method will be invoked which corrects the
nextFireTime. But not if the difference between
nextFireTimeand now is smaller than the misfireThreshold. Then the method is never called. This threshold's default value is 60,000. Thus if your pause period would be longer than 60s everything would be fine.
Since you have problems I assume it is not. ;) To workaround this you can modify the threshold or use a simple wrapper around
CronTrigger:
public class PauseAwareCronTrigger extends CronTrigger { // constructors you need go here @Override public Date getNextFireTime() { Date nextFireTime = super.getNextFireTime(); if (nextFireTime.getTime() < System.currentTimeMillis()) { // next fire time after now nextFireTime = super.getFireTimeAfter(null); super.setNextFireTime(nextFireTime); } return nextFireTime; } }
相关文章推荐
- SpringMVC4直接返回String乱码
- Myeclipse打不开,弹出对话框The Myeclipse executable launcher was unable to locate its companion shared library ”
- java中的匿名内部类总结
- myeclipse新建一个工作空间要做的事
- Java cookie的使用
- eclipse 3.7 没有jadClipse选项
- Spring Aop实例之xml配置
- spring 的优点?什么是DI机制? 什么是AOP?
- java中spring+springMVC+Mybatis主流框架maven的pom.xml配置参考
- 实例解析Java程序中正则表达式的贪婪模式匹配
- java 获取程序执行时间
- selenium java等待alert对话框弹出,并关闭对话框
- java枚举类型enum的使用
- struts2的result中传递多个参数
- java 匿名内部类
- decimalFormat("#","##0.00") java
- Spring基础知识(4)
- Java内存区域与内存溢出异常
- Hibeinate: Could not determine type for: java.util.List, at table: address_inf, for columns: [org.hi
- java内存溢出和内存泄露