原文地址: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@Overridepublic Date getNextFireTime() {Date nextFireTime = super.getNextFireTime();if (nextFireTime.getTime() < System.currentTimeMillis()) {// next fire time after nownextFireTime = super.getFireTimeAfter(null);super.setNextFireTime(nextFireTime);}return nextFireTime;}
}

转载于:https://www.cnblogs.com/davidwang456/p/5482858.html

Quartz Java resuming a job excecutes it many times--转相关推荐

  1. java 调度quartz,java quartz任务调度

    1.quartz的结构 quartz包括了3个部分:任务.触发器和调度器. (1)任务 Job:是一个接口.要定义自己的任务,必须继承该接口.该接口只用一个方法void execute(JobExec ...

  2. quartz java spring_从零开始学 Java - Spring 使用 Quartz 任务调度定时器

    生活的味道 睁开眼看一看窗外的阳光,伸一个懒腰,拿起放在床一旁的水白开水,甜甜的味道,晃着尾巴东张西望的猫猫,在窗台上舞蹈.你向生活微笑,生活也向你微笑. 请你不要询问我的未来,这有些可笑.你问我你是 ...

  3. quartz java 实现_Quartz使用-入门使用(java定时任务实现)

    [在项目中,定时实现某个任务是经常遇到的事情.在企业级的项目中,Quartz这一框架能够很好的帮我们完成定时任务.Quartz是一个开源的作业调度框架,它完全由Java写成,并 注:这里使用的是Qua ...

  4. Quartz - Java 任务调度

    1.背景 在企业应用中,经常有一些定时任务需要执行: 1)生成月报,季报和年报,这时候可以使用数据库的作业运行存储过程来实现: 2)定期查询哪些待审核单据即将过期,并给待审核人发送提醒邮件,可以使用p ...

  5. quartz java web_java web定时任务---quartz

    写在前面: 前面有简单的记录下Timer定时的用法,但是在此次项目中,选择的是quartz来完成定时操作任务的.两者都可以完成定时操作,但是spring可以整合quartz,并且配置起来也比较简便,还 ...

  6. Quartz Java编程

    http://www.quartz-scheduler.org/documentation/quartz-2.3.0/  http://www.quartz-scheduler.org/documen ...

  7. quartz java 线程 不释放_java Quartz 内存泄漏

    我用定时器启动应用的时候发现内存泄漏,具体报错如下: 十月 30, 2015 2:30:12 下午 org.apache.catalina.startup.HostConfig undeploy 信息 ...

  8. Quartz应用----发送邮件工作调度Java(转)

    Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中.下面介绍在J2SE中应用的邮件发送工作调度程序.            Quartz要运行起来,最简单需 ...

  9. job用法 Java_Web App使用Quartz实现java schedule job

    1 下载Quartz java包copy到WEB-INF/lib下 2 建立 scheduler初始化servlet 在web.xml里加入 Initializer com.nova.colimas. ...

最新文章

  1. 【C】printf warning: unknown conversion type character ‘l‘ in format [-Wformat=]
  2. java input 数组_Java基础之:数组
  3. 试玩 go-socks5
  4. mac打开class文件
  5. 一个LINUX高手写给初学者的话
  6. CRMEB开发文档及目录结构
  7. Game(HDU-6669)
  8. 苹果隐私追踪新规发布后 Android广告支出涨10%
  9. Bailian2935 有未知数的表达式【递归】
  10. JAVA处理模型的步骤,java-处理模型中条件字段的最佳方法
  11. python自动化测试-python自动化之(自动化测试报告)
  12. 【java笔记】方法引用介绍和使用
  13. C#中==操作符存在的缺陷
  14. ubuntu-桌面菜单栏、任务栏、标题栏都不见了-解决办法
  15. Java JDK 配置环境变量
  16. 恩典时代集团与深圳植慧科技产业签署合作备忘录
  17. 整形平台新氧的商标纠纷案背后:商标是创业公司命根子
  18. 基于JAVA的鲜花店商城平台【数据库设计、源码、开题报告】
  19. echarts scatter3D 图标陷进地图
  20. 纵向联邦线性模型在线推理过程中成员推断攻击的隐私保护研究

热门文章

  1. 13 登陆_13级!凌晨,“黑格比”登陆!对莆田的最新影响……
  2. http响应最大时长 nginx_nginx反向代理时如何保持长连接
  3. 开放大学计算机应用基础形考答案,国家开放大学计算机应用基础形考作业二答案~.doc...
  4. activity中fragment 返回键不退出_优雅地处理加载中(loading),重试(retry)和无数据(empty)等...
  5. java删除目录以及目录下文件,java删除文件、删除目录及目录下的文件
  6. 查看oracle系统信息,查看 ORACLE 系统级信息
  7. app.vue只执行一次吗_面包可以只发酵一次吗?
  8. ubuntu系统无法ssh登录--安装openssh
  9. python word2vector (三)
  10. 电脑电池修复_笔记本电脑不充电是怎么回事?