原文地址: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 :

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 CronTrigger works by remembering the nextFireTime. After creating the trigger the nextFireTime is initialized. Every time the job is triggered nextFireTime is updated. Since the job is not triggered when paused nextFireTime remains "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 nextFireTime and 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;
}
}

Quartz Java resuming a job excecutes it many times--转的更多相关文章

  1. Quartz+JAVA+Servlet实现任务调度系统(简洁)

    前言 该系统使用场景: 在12306上买了一张火车票,30分钟内需要支付(需要添加一个倒计时),30分钟还没有支付就请求取消订单的接口(自动根据url请求),如果支付了收到了支付的回调通知后,就删除计 ...

  2. 定时器篇---java.util.TimerTask和quartz

    最近项目中出现了定时执行任务的东西,研究了一下,觉得挺不错的,以后还用得到,就总结了下. 这里只介绍两种java.util.Timer 和 quartz java.util.Timer java自带的 ...

  3. Java&Quartz实现任务调度

    目录 Java&Quartz实现任务调度 1.Quartz的作用 2.预备 3.Quartz核心 3.1.Job接口 3.2.JobDetail类 3.3 JobExecutionContex ...

  4. 【59】Quartz+Spring框架详解

    什么是Quartz Quartz是一个作业调度系统(a job scheduling system),Quartz不但可以集成到其他的软件系统中,而且也可以独立运行的:在本文中"job sc ...

  5. Quartz定时调度在Web中的应用

    1.在数据库中建一个job表和job日志表 job表

  6. Quartz.Net—初识

    什么是Quartz.Net 计划任务,定时框架.大到可以做灾难转移  负载均衡.小到可以做定时生成数据,数据更新等等. 官网 http://www.quartz-scheduler.org/    Q ...

  7. quartz详解1:初步了解quartz

    http://blog.itpub.NET/11627468/viewspace-1763389/ 一.引入 你曾经需要应用执行一个任务吗?这个任务每天或每周星期二晚上11:30,或许仅仅每个月的最后 ...

  8. 关于Quartz .NET(V3.0.7)的简要说明

    目录 0. 任务调度 1. Quartz .NET 1.1 基本概念 1.2 主要接口和对象 2. 使用示例 2.0 准备工作 2.1 每间隔一定时间间隔执行一次任务 2.3 某天的固定时间点执行任务 ...

  9. HowToDoInJava Spring 教程·翻译完成

    原文:HowToDoInJava 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. ApacheCN 学习资源 目录 Spring 5 Spr ...

随机推荐

  1. Eclipse安装与配置

    http://www.th7.cn/Program/java/201307/144477.shtml http://www.eclipse.org/ 我们下载J2EE版本:Eclipse IDE fo ...

  2. angularjs ocLazyLoad分步加载js文件,angularjs ocLazyLoad按需加载js

    用angular有一段时间了,平日里只顾着写代码,没有注意到性能优化的问题,而今有时间,于是捋了捋,讲学习过程记录于此: 问题描述:由于采用angular做了网页的单页面应用,需要一次性在主布局中将所 ...

  3. RCP:拖拽功能的实现 Drag and Drop

    SWT中的拖拽是使用的org.eclipse.swt.dnd. 有三个需要密切注意的类: 1.DragSource 2.DropTarget 3.Transfer DragSource封装了需要被拖拽 ...

  4. EF6(CodeFirst)+MySql开发遇到的坑

    最近一不小心偷个懒就已经过了好几个月了,真是惭愧惭愧,出来混终究是要还的,我还是把”脱坑指南“写完吧,-_-~~.点我打开上篇博客 0x001.架构名”dbo”の殇 坑之首也,当提架构名,在mssql ...

  5. WebAdaptor Object reference not set to an instance of an object.

    C:\inetpub\wwwroot\arcgis目录下webAdaptor.config文件内容被清空,从别的地方拷贝一份即可. <?xml version="1.0" e ...

  6. Alpha阶段冲刺项目总结(补充)

    Alpha阶段冲刺阶段总结(补充) 此篇博客为"作业七:Alpha版本冲刺阶段" 与 "作业八:Alpha阶段项目总结" 的总结版. 一.项目预期计划vs实际进 ...

  7. hadoop研究:mapreduce研究前的准备工作

    继续研究hadoop,有童鞋问我,为啥不接着写hive的文章了,原因主要是时间不够,我对hive的研究基本结束,现在主要是hdfs和mapreduce,能写文章的时间也不多,只有周末才有时间写文章,所 ...

  8. 风筝的C++随时记

    关于常量指针与指针常量 关于按位运算符(&.|.<<.>>)   关于常量指针和指针常量 两个概念经常混淆啊,这是在考中文四六级啊,所以我给这两个概念起个长一点的名字. ...

  9. webservice4

    原理: 参考 http://lvwenwen.iteye.com/blog/1478236 客户端通过get请求可以得到wsdl文件,也就知道服务器提供的方法和参数了,然后客户端会通过webservi ...

  10. iptables基础命令详解

    TCP/IP基本概念: TCP/IP将网络分为四层:应用层,传输层,网络层,链路层. 传输层:定义了两种通信协议,分别为TCP协议和UDP协议. TCP协议:TCP协议在传输 数据过程中会检查数据的完 ...