Quartz Scheduler(2.2.1) - Usage of 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).

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 line were omitted), "now" is implied
    .withSchedule(SimpleScheduleBuilder.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) 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();
时间: 2024-10-16 22:56:00

Quartz Scheduler(2.2.1) - Usage of SimpleTrigger的相关文章

Quartz Scheduler(2.2.1) - Usage of CronTriggers

Cron is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerful and proven. The CronTrigger class is based on the scheduling capabilities of cron. CronTrigger uses “cron expressions”, which are able to create fir

Quartz Scheduler(2.2.1) - Usage of Calendars

Quartz Calendar objects (not java.util.Calendar objects) can be associated with triggers at the time the trigger is defined and stored in the scheduler. Calendars are useful for excluding blocks of time from the trigger's firing schedule. For instanc

Table of Contents - Quartz Scheduler

Getting Started Hello World Integration with Spring Quartz Scheduler Developer Guide Usage of JobDataMap Usage of Calendars Usage of SimpleTrigger Usage of CronTriggers Working with TriggerListeners and JobListeners Working with SchedulerListeners Wo

Quartz定时任务调度机制解析(CronTirgger、SimpleTrigger )

一.Quartz的介绍 Quartz 是 OpenSymphony 开源组织在任务调度领域的一个开源项目,完全基于 Java 实现.该项目于 2009 年被 Terracotta 收购,目前是 Terracotta 旗下的一个项目.它是一个而安全有java辨析额的开源作业调度框架. 二.Quartz 具有以下特点 1.强大的调度功能,例如支持丰富多样的调度方法,可以满足各种常规及特殊需求: 2.灵活的应用方式,例如支持任务和调度的多种组合方式,支持调度数据的多种存储方式: 3.分布式和集群能力,

Quartz Scheduler(2.2.1) - hello world

简单示例 1. maven 依赖 <dependencies> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-

Quartz Scheduler(2.2.1) - Working with JobStores

About Job Stores JobStores are responsible for keeping track of all the work data you give to the scheduler: jobs, triggers, calendars, and so forth. Selecting the appropriate JobStore for your Quartz scheduler instance is an important step. The choi

Spring Boot集成Spring Scheduler和Quartz Scheduler

本文介绍了Spring Boot集成Spring Scheduler和Quartz Scheduler的基础知识,利用ShedLock解决Spring Scheduler多实例运行冲突,介绍了Quartz ScheduleBuilder.Calendar,介绍了动态创建Quartz Job的方法. GitHub源码 Spring Scheduler Spring Framework提供了简单.易用的Job调度框架Spring Scheduler. 示例 在Spring Boot中,只需两步即可启

Quartz使用(2) - Quartz核心接口Scheduler、Job

quartz的核心接口如下: 接口 含义 Scheduler scheduler的主要API接口 Job 任务实现接口,期望调度器能够执行 JobDetail 用于定义Job实例 Trigger 调度器基于特定时间来执行指定任务的组件 JobBuilder 用于定义.创建JobDetail实例 TriggerBuilder 用于定义.创建Trigger实例 1. Scheduler 一个调度器的生命周期为通过SchedulerFactory创建,直到执行其shutdown()方法.当Schedu

Spring 3 + Quartz 1.8.6 Scheduler Example--reference

In this tutorial, we will show you how to integrate Spring with Quartz scheduler framework. Spring comes with many handy classes to support Quartz, and decouple your class to Quartz APIs. Tools Used : Spring 3.1.2.RELEASE Quartz 1.8.6 Eclipse 4.2 Mav