Lesson 5: SimpleTrigger_秋名山的放牛娃的博客-程序员秘密

技术标签: Quartz定时器  

http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-05.html

SimpleTrigger should meet your scheduling needs if you need to have a job execute exactly once at a specific moment in time, or at a specific moment in time followed by repeats at a specific interval. For example, if you want the trigger to fire at exactly 11:23:54 AM on January 13, 2015, or if you want it to fire at that time, and then fire five more times, every ten seconds.

With this description, you may not find it surprising to find that the properties of a SimpleTrigger include: a start-time, and end-time, a repeat count, and a repeat interval. All of these properties are exactly what you’d expect them to be, with only a couple special notes related to the end-time property.

The repeat count can be zero, a positive integer, or the constant value SimpleTrigger.REPEAT_INDEFINITELY. The repeat interval property must be zero, or a positive long value, and represents a number of milliseconds. Note that a repeat interval of zero will cause ‘repeat count’ firings of the trigger to happen concurrently (or as close to concurrently as the scheduler can manage).

If you’re not already familiar with Quartz’s DateBuilder class, you may find it helpful for computing your trigger fire-times, depending on the startTime (or endTime) that you’re trying to create.

The endTime property (if it is specified) overrides the repeat count property. This can be useful if you wish to create a trigger such as one that fires every 10 seconds until a given moment in time - rather than having to compute the number of times it would repeat between the start-time and the end-time, you can simply specify the end-time and then use a repeat count of REPEAT_INDEFINITELY (you could even specify a repeat count of some huge number that is sure to be more than the number of times the trigger will actually fire before the end-time arrives).

如果你需要在一个指定时间段内执行一次作业任务或是在指定的时间间隔内多次执行作业任务,SimpleTrigger应该能满足你的调度需求。例如,你希望触发器在2015年1月13日上午11:23:54准时触发,或是希望在那个时间点触发,然后再重复触发5次,每隔10秒一次。

有了这样的描述,你就不会对SimpleTrigger包含的参数感到奇怪:开始执行时间,结束执行时间,重复次数和重复执行间隔时间。所有的参数都是你期望的那样,只是关于结束执行时间参数有两条特别的提示。

 重复次数可以为0,正整数或是SimpleTrigger.REPEAT_INDEFINITELY常量值。重复执行间隔必须为0或长整数(long类型),它表示毫秒数的值。注意如果重复执行间隔时间为0会导致数量为“重复次数”的触发器并发执行(或是在调度器控制下接近并发执行)。

    如果你还不熟悉Quartz的DateBuilder类,你尝试创建日期对象时会发现它非常方便地根据startTime或 endTime参数计算触发器的触发时间。

EndTime参数(如果被指定)会覆盖重复次数参数的效果。当你希望创建一个触发器,每隔10秒被触发一次直到给定的截止时间,而不是必须完成在给定的开始和结束时间段内的触发次数,使用endTime参数会非常方便,你可以仅仅指定一个end-time参数,并且将重复次数设置为REPEAT_INDEFINITELY(你甚至可以将重复次数指定为非常大的值,确保比结束执行时间到达前实际要执行的次数大就行)。


SimpleTrigger instances are built using TriggerBuilder (for the trigger’s main properties) and SimpleScheduleBuilder (for the SimpleTrigger-specific properties). To use these builders in a DSL-style, use static imports:

SimpleTrigger实例通过TriggerBuilder(for the trigger’s main properties)和SimpleScheduleBuilder(for the SimpleTrigger-specific properties)创建,通过如下代码导入他们的静态方法:

import static org.quartz.TriggerBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
import static org.quartz.DateBuilder.*:

Here are various examples of defining triggers with simple schedules, read through them all, as they each show at least one new/different point:

下面是一些defining triggers with simple schedules的例子,可以从中比较他们的异同:

Build a trigger for a specific moment in time, with no repeats:

创建一个不会重复的在指定时间执行的trigger


SimpleTrigger trigger = (SimpleTrigger) newTrigger()
    .withIdentity("trigger1", "group1")
    .startAt(myStartTime) // some Date
    .forJob("job1", "group1") // identify job with name, group strings
    .build();
Build a trigger for a specific moment in time, then repeating every ten seconds ten times:

创建一个每10分钟执行一次、执行10次、在指定时间执行的trigger

trigger = newTrigger()
    .withIdentity("trigger3", "group1")
    .startAt(myTimeToStartFiring)  // if a start time is not given (if this line were omitted), "now" is implied
    .withSchedule(simpleSchedule()
        .withIntervalInSeconds(10)
        .withRepeatCount(10)) // note that 10 repeats will give a total of 11 firings
    .forJob(myJob) // identify job with handle to its JobDetail itself                   
    .build();


Build a trigger that will fire once, five minutes in the future:

创建一个5分钟后、只会执行一次的trigger

trigger = (SimpleTrigger) newTrigger()
    .withIdentity("trigger5", "group1")
    .startAt(futureDate(5, IntervalUnit.MINUTE)) // use DateBuilder to create a date in the future
    .forJob(myJobKey) // identify job with its JobKey
    .build();

Build a trigger that will fire now, then repeat every five minutes, until the hour 22:00:

创建一个立马执行的trigger, 每五分钟重复一次, 一直到 22:00:


trigger = newTrigger()
    .withIdentity("trigger7", "group1")
    .withSchedule(simpleSchedule()
        .withIntervalInMinutes(5)
        .repeatForever())
    .endAt(dateOf(22, 0, 0))
    .build();
Build a trigger that will fire at the top of the next hour, then repeat every 2 hours, forever:

创建一个trigger会在下一个小时的0分0秒执行,然后是一直是每两个小时执行一次

trigger = newTrigger()
    .withIdentity("trigger8") // because group is not specified, "trigger8" will be in the default group
    .startAt(evenHourDate(null)) // get the next even-hour (minutes and seconds zero ("00:00"))
    .withSchedule(simpleSchedule()
        .withIntervalInHours(2)
        .repeatForever())
    // note that in this example, 'forJob(..)' is not called
    //  - which is valid if the trigger is passed to the scheduler along with the job  
    .build();

    scheduler.scheduleJob(trigger, job);

Spend some time looking at all of the available methods in the language defined by TriggerBuilder and SimpleScheduleBuilder so that you can be familiar with options available to you that may not have been demonstrated in the examples above.

熟悉TriggerBuilderSimpleScheduleBuilder定义的一些方法,还可以发现一些其他上面的例子中没有列举出来的选项。

Note that TriggerBuilder (and Quartz's other builders) will generally choose a reasonable value for properties that you do not explicitly set. For examples: if you don't call one of the *withIdentity(..)* methods, then TriggerBuilder will generate a random name for your trigger; if you don't call *startAt(..)* then the current time (immediately) is assumed.
另外, TriggerBuilder (以及quartz其他的builders) ,在你没有明确的为一些属性指定值的时候,builder会将其设置为一些有意义的值。比如没有设置 withIdentity() 方法的值时候, TriggerBuilder 会为你的trigger生成一个随机的名字;如果没有调用 startAt() 那默认就是当前的时间。


SimpleTrigger Misfire Instructions

SimpleTrigger has several instructions that can be used to inform Quartz what it should do when a misfire occurs. (Misfire situations were introduced in “Lesson 4: More About Triggers”). These instructions are defined as constants on SimpleTrigger itself (including JavaDoc describing their behavior). The instructions include:

Misfire Instruction Constants of SimpleTrigger

SimpleTrigger 有一些instructions 可以通知quartz在misfire 出现的时候告诉quartz应该做些什么。这些instructionsSimpleTrigger 有对应的常量定义(可以参阅JavaDoc文档)。这些instructions 包括:

SimpleTrigger中的Misfire Instruction 常量

MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY
MISFIRE_INSTRUCTION_FIRE_NOW
MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT
MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT
MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT
MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT

You should recall from the earlier lessons that all triggers have the Trigger.MISFIRE_INSTRUCTION_SMART_POLICY instruction available for use, and this instruction is also the default for all trigger types.

If the ‘smart policy’ instruction is used, SimpleTrigger dynamically chooses between its various MISFIRE instructions, based on the configuration and state of the given SimpleTrigger instance. The JavaDoc for the SimpleTrigger.updateAfterMisfire() method explains the exact details of this dynamic behavior.

When building SimpleTriggers, you specify the misfire instruction as part of the simple schedule (via SimpleSchedulerBuilder):

在先前文章中使用的triggers都默认使用了Trigger.MISFIRE_INSTRUCTION_SMART_POLICY instruction, 这是默认的instruction对于所有的trigger类型来说。

如果smart policy instruciton被使用了,SimpleTrigger会基于configuration和SimpleTrigger instancestate动态的从各种MISFIRE instructions 中选取合适的。可以查看SimpleTrigger.updateAfterMisfire()的JavaDoc文档,详细描述了动态行为的细节。

通过SimpleSchedulerBuilder的simpleSchedule指定misfire instruction:


trigger = newTrigger()
    .withIdentity("trigger7", "group1")
    .withSchedule(simpleSchedule()
        .withIntervalInMinutes(5)
        .repeatForever()
        .withMisfireHandlingInstructionNextWithExistingCount())
    .build();



版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_26341909/article/details/78920515

智能推荐

libCGAL_Core-vc100-mt-4.4-I-900.liblibCGAL-vc100-mt-s-4.4-I-900.liblibboost_thread-vc100-mt-1_55.l_vv1025的博客-程序员秘密

libCGAL_Core-vc100-mt-4.4-I-900.liblibCGAL-vc100-mt-s-4.4-I-900.liblibboost_thread-vc100-mt-1_55.liblibboost_thread-vc100-mt-s-1_55.liblibboost_system-vc100-mt-1_55.liblibboost_thread-vc100-mt-sgd-1_55.lib

常见移动设备的 CSS3 Media Query 整理(iPhone/iPad/Galaxy)_offbye的博客-程序员秘密

今天帮人解决了iphone4下载页显示错误的问题,css3 media query坑啊,不容易匹配到。/** * iPhone 4/4s landscape & portrait */@media only screenand (min-device-width: 320px)and (max-device-width: 480px)and (-webkit-device-pixel-r

Python之集合set对象的创建_叫我王员外就行的博客-程序员秘密_python创建set

1、惭愧啊,集合的概念都没总结吗?2、基本概念英文名字:set中文名字:集合是一个元素无序、无重复元素、不可变对象的集合3、set有点像dict,只是没有value,而dict中的key集合,就是用的set4、重要特征a、set中的每一个元素必须是不可变对象b、set中不可以有重复的元素c、set中的元素插入、元素访问都是完全无序的5...

android 手指移动的动画,Android中View跟随手指滑动效果的实例代码_zetachow晓代~的博客-程序员秘密

Android中View跟随手指滑动效果的实例代码发布时间:2020-09-21 08:03:14来源:脚本之家阅读:127作者:meetings本文讲述了Android中View跟随手指滑动效果的实例代码。分享给大家供大家参考,具体如下:1、android View 主要6种滑动方法,分别是layout()offsetLeftAndRight()和offsetTopAndBottom()Layo...

嵌入式分享合集59_tt姐whaosoft的博客-程序员秘密

周末也发俩个,正好也在研究wasm一、 如何防止电源正负极接反这个还是要重视啊 我家床单都好多小洞洞了~~~硬件工程师的很多项目是在洞洞板上完成的,但有存在不小心将电源正负极接反的现象,导致很多电子元器件都烧毁,甚至整块板子都废掉,还得再焊接一块,不知道有什么好的办法可以解决?首先粗心不可避免,虽说只是区分正负极两根线,一红一黑,可能接线一次,我们不会出错;接10次线也不会出错,但是1000次?10000呢?

BOOST_PP_SEQ_ENUM宏_freemannnn的博客-程序员秘密

定义此宏的头文件boost\preprocessor\seq\enum.hpp此宏用来将一个序列转换成数组。 举个例子:先定义一个序列,#define BOOST_UNORDERED_PRIMES (17ul)(29ul)(37ul)(53ul)(此序列可以在boost\unordered\detail\util.hpp中找到)BOOST_PP_SEQ_ENUM(BOO

随便推点

HTML页面转pdf_IT-along的博客-程序员秘密_html 转pdf

HTML页面转pdf工作时,直接传个HTML页面给我,我人被整懵了,通过学习也是解决了这个问题,记录一下使用 flying-saucer-pdf 将HTML转成pdf

visio_csdn123t的博客-程序员秘密_visio

Visio 使你能够将复杂的文本和表转换为很难理解的可视化图表,以便快速传达信息。 有许多类型的Visio 图表,包括组织结构图、网络图、工作流和家庭或 office 计划。 Visio 入门可以分为三个基本步骤:使用模板、排列和连接形状以及使用文本修改形状。Visio百度云安装包分享矢量图:iconfont-阿里巴巴矢量图标库模板:visio(99图)[email protected]小-佐-收集_花瓣平面使用教程:快捷方式选择工具 ctrl+1 文字工具 ctrl+2 连接线 ctrl+3 形状工具 .

多级菜单_weixin_30603633的博客-程序员秘密

三级菜单可依次选择进入各子菜单所需新知识点:列表、字典 1 menu = { 2 '北京':{ 3 '海淀':{ 4 '五道口':{ 5 'soho':{}, 6 '网易':{}, 7 'google'...

LDAP介绍_Laputa_SKY的博客-程序员秘密

1. LDAP介绍1.1. LDAP是什么1.2. LDAP是电话簿1.3. LDAP是不是数据库2. LDAP的特点2.1. LDAP的优势2.1.1 跨平台2.1.2 费用及维护2.1.3 复制技术2.1.4 允许使用ACI2.2. LDAP存储什么数据2.3. 什么时候该用LDAP存储数据3. LDAP的基本模型3.1 信息模型:描述LDAP的信息表示方式3.2 命名模型:描述LDAP中的数...

unicode编码表_Taoey的博客-程序员秘密_unicode完整编码表

unicode编码表 转载于 近來情轉深的博客:http://jlqzs.blog.163.com/blog/static/2125298320070101826277/ 1 unicode编码表 Unicode目前普遍采用的是UCS-2,它用两个字节来编码一个字符, 比如汉字”经”的编码是0x7ECF,注意字符编码一般用十六进制来 表示,为了与十进制区分,十六进制以0x开头,0x7EC

利用Matlab filterDesigner 工具生成FIR滤波器函数,并调用实现低通滤波_毛毛虫的爹的博客-程序员秘密

https://blog.csdn.net/luobluesky/article/details/89143608function Hd = untitled%UNTITLED Returns a discrete-time filter object.% MATLAB Code% Generated by MATLAB(R) 8.6 and the Signal Processing Toolbox 7.1.% Generated on: 16-Nov-2021 15:19:48% B