Quartz Scheduler(2.2.1)

摘要:
SimpleTriggershouldmeetyourschedulingneedsifyouneedtohaveajobexecuteexactlyonceataspecificmomentintime,orataspecificmomentintimefollowedbyrepeatsataspecificinterval.Forexample,ifyouwantthetriggertofir

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 thestartTime(orendTime) that you're trying to create.

TheendTimeproperty (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).

Examples of Defining Triggers with Different Schedules

The following are various examples of defining triggers with simple schedules. Review them all, as they each show at least one new/different point.

Also, 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 shown here.

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

SimpleTrigger trigger =(SimpleTrigger) JobBuilder.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:

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

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

trigger =(SimpleTrigger) JobBuilder.newTrigger()  
    .withIdentity("trigger5", "group1") 
    .startAt(DateBuilder.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 =JobBuilder.newTrigger() 
    .withIdentity("trigger7", "group1") 
    .withSchedule(SimpleScheduleBuilder.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 =JobBuilder.newTrigger() 
    .withIdentity("trigger8") //because group is not specified, "trigger8" will be in the default group 
    .startAt(DateBuilder.evenHourDate(null)) //get the next even-hour (minutes and seconds zero ("00:00")) 
.withSchedule(SimpleScheduleBuilder.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);

SimpleTrigger Misfire Instructions

SimpleTrigger has several instructions that can be used to inform Quartz what it should do when a misfire occurs. (For information about misfires, see the About Triggers topic.) These instructions are defined as constants on SimpleTrigger itself (including Javadoc that describes their behavior). These constants include:

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

All triggers have the MISFIRE_INSTRUCTION_SMART_POLICY instruction available for use, and this instruction is the default for all trigger types.

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

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

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

免责声明:文章转载自《Quartz Scheduler(2.2.1)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Debian中snmp的配置(使用监控宝)完整加载兔子vtk模型下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

项目中使用Quartz集群分享--转载

原文:http://hot66hot.iteye.com/blog/1726143 在公司分享了Quartz,发布出来,希望大家讨论补充. CRM使用Quartz集群分享 一:CRM对定时任务的依赖与问题 二:什么是quartz,如何使用,集群,优化 三:CRM中quartz与Spring结合使用 1:CRM对定时任务的依赖与问题 1)依赖 (1)每天晚上...

.Net Core自动化部署系列(三):使用GitLab CI/CD 自动部署Api到Docker

之前写过使用Jenkins实现自动化部署,最近正好没事研究了下GitLab的自动化部署,顺便记录一下。 使用GitLab部署我们需要准备两件事,第一个起码你得有个GitLab,自己搭建或者使用官方的都可以哈,我这里使用的官方的,想自己搭建的同学可以参考下这篇,使用Docker搭建GitLab: https://www.imooc.com/article/2...

52.关于Android Studio项目的Gradle构建

转载:某学姐http://mouxuejie.com/blog/2016-02-13/android-gradle-config/Gradle构建脚本使用DSL(Domain Specific Language)来描述构建逻辑,使用的语言是Groovy。想了解Android Studio工程的Gradle构建系统,可以先从Project的settings....

基于node的前端项目编译时(react vue 打包)内存溢出问题

前段时间公司有个基于 vue 的项目在运行 npm run build 的时候会报内存溢出,今天在某个技术流交群也有位小伙伴基于 angular 的项目也出现了这个问题,所以查了一些相关的资料总结了一下,下面会详细说明前端三大框架编译时遇到这个问题具体怎么解决。首先看我模拟出的报错内容 具体截图如下 里面有句关键的话, CALL_AND_RETRY_LA...

uboot学习之二----主Makefile学习之四----两种编译方法:原地编译和单独输出文件夹编译

第57-123行: 57 # 58 # U-boot build supports producing a object files to the separate external 59 # directory. Two use cases are supported: 60 # 61 # 1) Add O= to the mak...

vue 单页应用 seo 优化之 预渲染(prerender-spa-plugin)

前言:当前 SPA 架构流行的趋势如日中天,前后端分离的业务模式已经成为互联网开发的主流方式,但是 单页面 应用始终存在一个痛点,那就是 SEO, 对于那些需要推广,希望能在百度搜索时排名靠前的网站而言,使用单页面应用的是无法被 百度的 蜘蛛 爬到的,为此,众多流行的 MVVM 框架都推出了 很多解决方案,有官方的也有三方的,VUE也不例外,本文章就来分享...