`
thinktothings
  • 浏览: 780164 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Lesson 5: SimpleTrigger

阅读更多

Quartz Enterprise Job Scheduler Tutorial

Lesson 5: SimpleTrigger

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 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:

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:

Build a trigger for a specific moment in time, with no repeats:
  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:
  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:
  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 = 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 = 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.

Info 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.

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
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):

  trigger = newTrigger()
    .withIdentity("trigger7", "group1")
    .withSchedule(simpleSchedule()
        .withIntervalInMinutes(5)
        .repeatForever()
        .withMisfireHandlingInstructionNextWithExistingCount())
    .build();
分享到:
评论

相关推荐

    Lesson 1: Using Quartz

    《Lesson 1: 使用Quartz》 在IT领域,任务调度是系统自动化的重要组成部分,而Quartz是一款功能强大且广泛使用的Java作业调度框架。本文将深入探讨如何利用Quartz进行任务管理和执行,以及其在实际项目中的应用。 ...

    Quartz之SimpleTrigger

    5. **工具支持**: 除了编程方式配置,Quartz还提供了基于XML或数据库的配置方式,以及管理工具如QuartzAdmin,方便用户可视化管理和监控任务。 6. **QuartzDemo**: 压缩包中的"QuartzDemo"可能是包含了一个...

    quartz 包,源文件与 使用说明

    Quertz中提供了两类触发器为:SimpleTrigger,CronTrigger。前者用于实现比较简单的定时功能,例如几点开始,几 点结束,隔多长时间执行,共执行多少次等,后者提供了使用表达式来描述定时功能,因此适用于比较复杂的...

    Quartz 固定时间间隔计划+Calendar和Cron

    标题中的 "固定时间间隔计划" 指的是使用 Quartz 的 SimpleTrigger 类来设置间隔时间执行任务的方式,而 "Calendar 和 Cron" 提到了另外两种时间触发器,它们提供了更复杂的调度策略。 1. **SimpleTrigger**: ...

    scheduler任务调度各种触发器和过时处理策略

    ### 5. 过时处理策略 过时处理策略对于系统稳定性和任务的正确执行至关重要。scheduler提供多种策略来应对任务未能在预定时间执行的情况(称为“过时”),例如: - **MISFIRE_INSTRUCTION_FIRE_NOW**:立即执行一...

    spring定时器配置说明

    本说明文档将详细解析两种常见的触发器:SimpleTrigger和CronTrigger的配置与使用。 首先,SimpleTrigger适用于那些需要简单定时任务的场景。例如,如果你想让某个任务在特定时间启动,并按设定的间隔重复执行,...

    Quartz2.2版本开发手册(JAVA).doc

    5. **Quartz 手册 java 版-(五) SimpleTrigger**:SimpleTrigger 是一种基础的触发器,用于按照固定间隔重复执行 Job。开发者需要了解如何设置重复次数、间隔时间和结束时间。 6. **Quartz 手册 java 版-(六)...

    Quartz Job Scheduling Framwork中文文档

    5. **SimpleTrigger**:SimpleTrigger是最简单的触发器,用于设定固定间隔的重复执行,如每隔多少秒、分钟、小时执行一次。 6. **持久化**:Quartz支持Job和Trigger的持久化存储,这使得在Scheduler重启后,之前的...

    Quartz作业调度器

    3. **SimpleTrigger**:SimpleTrigger则用于定义基于时间间隔的重复执行,如每隔10秒执行一次。可以设置重复次数和结束时间。 4. **JobDetail**:JobDetail定义了作业的详细信息,包括作业的类、组名和关联的数据。...

    Quartz-2.2.3

    - **org.quartz.SimpleTrigger**: SimpleTrigger类,定义简单的延时和重复执行规则。 3. **配置与使用** - **XML配置**: Quartz可以通过XML配置文件定义Job和Trigger,方便管理和维护。 - **代码配置**: 也可以...

    人物调度Quartz 学习

    10. **触发器类型**:SimpleTrigger按照固定间隔触发任务,而CronTrigger则可以根据Cron表达式,按照特定的时间模式(如每日、每周等)触发任务,考虑到了时区和夏令时的影响。 总的来说,Quartz 是一个功能丰富的...

    TestQuartz.zip

    - **Trigger**:触发器,定义何时运行Job,可以是SimpleTrigger(简单触发器)或CronTrigger(cron表达式触发器)等。 - **JobDetail**:包含Job的详细信息,如Job类、数据绑定等。 - **JobDataMap**:Job的参数...

    使用java定时器的几种方式

    SimpleTrigger可以在指定时间点执行一次或者按照指定的时间间隔重复执行。CronTrigger则更灵活,它允许以cron表达式的形式指定复杂的时间规则。Quartz的配置和使用相对复杂一些,但提供了更加灵活的调度功能。 第三...

    quartzAPI-2.2.1参看文档

    - Trigger 定义了 Job 的执行策略,如 SimpleTrigger 或 CronTrigger。 - TriggerBuilder 可以帮助构建 Trigger,例如 `TriggerBuilder.create().withIdentity()` 和 `withSchedule()`。 6. **CronTrigger**: -...

    Quartz 插件类型定时器的使用方法

    5. **CalendarIntervalTrigger**:除了SimpleTrigger和CronTrigger,还有CalendarIntervalTrigger,用于按日、周、月等时间间隔进行调度。 6. **JobDataMap**:JobDataMap允许你在Job执行时传递参数或数据,它是...

    task-examproject:使用Spring Scheduler的任务基础项目

    SimpleTrigger用于固定间隔的重复任务,而CronTrigger则可以基于cron表达式,实现更灵活的时间调度。 【项目结构解析】 在`task-examproject-master`中,我们通常会看到以下关键部分: 1. `src/main/java`: 存放...

    Quartz的一个简单实例

    SimpleTrigger simpleTrigger = new SimpleTrigger("trigger1", "job1", new Date(ts), null, -1, 1000L); // 将 ‘job1’ 加入调度者中 scheduler.scheduleJob(jobDetail, simpleTrigger); // 开始运行调度...

    Quartz调度资料

    2. **触发器类型**:介绍SimpleTrigger和CronTrigger的区别,SimpleTrigger用于重复执行,CronTrigger则可以根据cron表达式进行复杂的时间安排。 3. **作业执行上下文**:JobDataMap的使用,用于在作业执行时传递...

    quartz实现定时功能实例详解(servlet定时器配置方法)

    5. **总结**: 通过Quartz,我们可以轻松地在Java应用程序中实现定时任务,无论是简单的定时执行还是复杂的Cron表达式调度。在Servlet环境下,只需要进行适当的配置,就可以让Quartz在Web应用中无缝工作。Quartz的...

    quartz完整版demo

    5. **运行环境**:这个demo是在Eclipse环境中运行的,因此可能包含了Eclipse项目文件,如`.project`和`.classpath`,以及必要的依赖库。确保Eclipse已经安装了Quartz库,才能成功运行示例。 在学习这个demo时,你...

Global site tag (gtag.js) - Google Analytics