Quartz Java resuming a job excecutes it many times--转
原文地址: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--转的更多相关文章
- Quartz+JAVA+Servlet实现任务调度系统(简洁)
前言 该系统使用场景: 在12306上买了一张火车票,30分钟内需要支付(需要添加一个倒计时),30分钟还没有支付就请求取消订单的接口(自动根据url请求),如果支付了收到了支付的回调通知后,就删除计 ...
- 定时器篇---java.util.TimerTask和quartz
最近项目中出现了定时执行任务的东西,研究了一下,觉得挺不错的,以后还用得到,就总结了下. 这里只介绍两种java.util.Timer 和 quartz java.util.Timer java自带的 ...
- Java&Quartz实现任务调度
目录 Java&Quartz实现任务调度 1.Quartz的作用 2.预备 3.Quartz核心 3.1.Job接口 3.2.JobDetail类 3.3 JobExecutionContex ...
- 【59】Quartz+Spring框架详解
什么是Quartz Quartz是一个作业调度系统(a job scheduling system),Quartz不但可以集成到其他的软件系统中,而且也可以独立运行的:在本文中"job sc ...
- Quartz定时调度在Web中的应用
1.在数据库中建一个job表和job日志表 job表
- Quartz.Net—初识
什么是Quartz.Net 计划任务,定时框架.大到可以做灾难转移 负载均衡.小到可以做定时生成数据,数据更新等等. 官网 http://www.quartz-scheduler.org/ Q ...
- quartz详解1:初步了解quartz
http://blog.itpub.NET/11627468/viewspace-1763389/ 一.引入 你曾经需要应用执行一个任务吗?这个任务每天或每周星期二晚上11:30,或许仅仅每个月的最后 ...
- 关于Quartz .NET(V3.0.7)的简要说明
目录 0. 任务调度 1. Quartz .NET 1.1 基本概念 1.2 主要接口和对象 2. 使用示例 2.0 准备工作 2.1 每间隔一定时间间隔执行一次任务 2.3 某天的固定时间点执行任务 ...
- HowToDoInJava Spring 教程·翻译完成
原文:HowToDoInJava 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. ApacheCN 学习资源 目录 Spring 5 Spr ...
随机推荐
- JDK和环境配置
1. JASE : J2SE 这个就是我们现在在学的东西,他是一切Java的核心基础 JAME :J2ME : 他是Java的一个微型版,主要用来做移动开发 JAEE :J2EE Java企业版本,主 ...
- html5 localStorage实现表单本地存储
随笔开头,我不得不吐槽,为什么我的随笔每次发布之后,都会在分分钟之内移出首页.好气啊!! 进入正题了,项目中有许多表单输入框要填写,还有一些单选复选框之类的.用户可能在填写了大量的信息之后,不小心刷新 ...
- Java学习笔记(六)
期末课程选题:QQ登录界面.好友列表界面及聊天框界面. 功能实现:简单的功能可实现,如:点击登录进入好友列表界面:点击好友可进入聊天框:可实现简单聊天功能:聊天可输入及输出,可选择私聊或群聊,可获得当 ...
- 【转】VC中的字符串处理
http://hi.baidu.com/nmn714/item/ab8d2a96d0f2d6f228164727 貌似不少人刚开始做windows程序时都会纠结在字符串处理上,所以我把关于字符串处理的 ...
- jquery 获取 scrollHeight
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:匿名用户链接:http://www.zhihu.com/question/20985674/answer/16807177来源 ...
- Android热修复技术选型(不在市场发布新版本的情况下,直接更新app)
2015年以来,Android开发领域里对热修复技术的讨论和分享越来越多,同时也出现了一些不同的解决方案,如QQ空间补丁方案.阿里AndFix以及微信Tinker,它们在原理各有不同,适用场景各异,到 ...
- Java构建工具Ant小记(一)
Ant简介 Ant是基于java的构建工具.理论上来说它类似与make工具,但是却克服了make的一些固有的缺陷. 传统的Make是基于操作系统shell的构建工具,虽然也可以基于工作的os对make ...
- [C#进阶系列]专题一:深入解析深拷贝和浅拷贝
一.前言 这个星期参加了一个面试,面试中问到深浅拷贝的区别,然后我就简单了讲述了它们的之间的区别,然后面试官又继续问,如何实现一个深拷贝呢?当时只回答回答了一种方式,就是使用反射,然后面试官提示还可以 ...
- 关于使用ABP框架搭建的项目升级时需要注意的问题汇总
ABP理论学习总目录 一步一步使用ABP框架搭建正式项目系列教程 ABP之Module-Zero学习目录 本篇目录 说明 升级方法 问题_01:Log4Net导致编译不成功 2015/12/18更新 ...
- 图解集合5:不正确地使用HashMap引发死循环及元素丢失
问题引出 前一篇文章讲解了HashMap的实现原理,讲到了HashMap不是线程安全的.那么HashMap在多线程环境下又会有什么问题呢? 几个月前,公司项目的一个模块在线上运行的时候出现了死循环,死 ...