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

Lesson 4: More About Triggers

阅读更多

Quartz Enterprise Job Scheduler Tutorial

Lesson 4: More About Triggers

Like jobs, triggers are quite easy to work with, but do contain a variety of customizable options that you need to be aware of and understand before you can make full use of Quartz. Also, as noted earlier, there are different types of triggers that you can select from to meet different scheduling needs.

You will learn about the two most common types of triggers in Lesson 5: Simple Triggers and Lesson 6: Cron Triggers .

Common Trigger Attributes

Aside from the fact that all trigger types have TriggerKey properties for tracking their identities, there are a number of other properties that are common to all trigger types. These common properties are set using the TriggerBuilder when you are building the trigger definition (examples of that will follow).

Here is a listing of properties common to all trigger types:

  • The "jobKey" property indicates the identity of the job that should be executed when the trigger fires.
  • The "startTime" property indicates when the trigger's schedule first comes into affect. The value is a java.util.Date object that defines a moment in time on a given calendar date. For some trigger types, the trigger will actually fire at the start time, for others it simply marks the time that the schedule should start being followed. This means you can store a trigger with a schedule such as "every 5th day of the month" during January, and if the startTime property is set to April 1st, it will be a few months before the first firing.
  • The "endTime" property indicates when the trigger's schedule should no longer be in effect. In other words, a trigger with a schedule of "every 5th day of the month" and with an end time of July 1st will fire for it's last time on June 5th.

Other properties, which take a bit more explanation are discussed in the following sub-sections.

Priority

Sometimes, when you have many Triggers (or few worker threads in your Quartz thread pool), Quartz may not have enough resources to immediately fire all of the Triggers that are scheduled to fire at the same time. In this case, you may want to control which of your Triggers get first crack at the available Quartz worker threads. For this purpose, you can set the priority property on a Trigger. If N Triggers are to fire at the same time, but there are only Z worker threads currently available, then the first Z Triggers with the highest priority will be executed first. If you do not set a priority on a Trigger, then it will use the default priority of 5. Any integer value is allowed for priority, positive or negative.

Note: Priorities are only compared when triggers have the same fire time. A trigger scheduled to fire at 10:59 will always fire before one scheduled to fire at 11:00.

Note: When a trigger's job is detected to require recovery, its recovery is scheduled with the same priority as the original trigger.

Misfire Instructions

Another important property of a Trigger is its "misfire instruction". A misfire occurs if a persistent trigger "misses" its firing time because of the scheduler being shutdown, or because there are no available threads in Quartz's thread pool for executing the job. The different trigger types have different misfire instructions available to them. By default they use a 'smart policy' instruction - which has dynamic behavior based on trigger type and configuration. When the scheduler starts, it searches for any persistent triggers that have misfired, and it then updates each of them based on their individually configured misfire instructions. When you start using Quartz in your own projects, you should make yourself familiar with the misfire instructions that are defined on the given trigger types, and explained in their JavaDoc. More specific information about misfire instructions will be given within the tutorial lessons specific to each trigger type.

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 the trigger's firing schedule. For instance, you could create a trigger that fires a job every weekday at 9:30 am, but then add a Calendar that excludes all of the business's holidays.

Calendar's can be any serializable objects that implement the Calendar interface, which looks like this:

The Calendar Interface
package org.quartz;

public interface Calendar {

  public boolean isTimeIncluded(long timeStamp);

  public long getNextIncludedTime(long timeStamp);

}

Notice that the parameters to these methods are of the long type. As you may guess, they are timestamps in millisecond format. This means that calendars can 'block out' sections of time as narrow as a millisecond. Most likely, you'll be interested in 'blocking-out' entire days. As a convenience, Quartz includes the class org.quartz.impl.HolidayCalendar, which does just that.

Calendars must be instantiated and registered with the scheduler via the addCalendar(..) method. If you use HolidayCalendar, after instantiating it, you should use its addExcludedDate(Date date) method in order to populate it with the days you wish to have excluded from scheduling. The same calendar instance can be used with multiple triggers such as this:

An Example of using calendars
HolidayCalendar cal = new HolidayCalendar();
cal.addExcludedDate( someDate );
cal.addExcludedDate( someOtherDate );

sched.addCalendar("myHolidays", cal, false);


Trigger t = newTrigger()
    .withIdentity("myTrigger")
    .forJob("myJob")
    .withSchedule(dailyAtHourAndMinute(9, 30)) // execute job daily at 9:30
    .modifiedByCalendar("myHolidays") // but not on holidays
    .build();

// .. schedule job with trigger

Trigger t2 = newTrigger()
    .withIdentity("myTrigger2")
    .forJob("myJob2")
    .withSchedule(dailyAtHourAndMinute(11, 30)) // execute job daily at 11:30
    .modifiedByCalendar("myHolidays") // but not on holidays
    .build();

// .. schedule job with trigger2

The details of the construction/building of triggers will be given in the next couple lessons. For now, just believe that the code above creates two triggers, each scheduled to fire daily. However, any of the firings that would have occurred during the period excluded by the calendar will be skipped.

See the org.quartz.impl.calendar package for a number of Calendar implementations that may suit your needs.

© 2001-2011 Terracotta, Inc., http://www.terracotta.org

分享到:
评论

相关推荐

    触发器Triggers、MultiTrggers、EventTrigger应用案例

    在Windows Presentation Foundation (WPF) 中,触发器(Triggers)是XAML中的一种关键特性,它们允许我们根据特定条件改变UI元素的属性或者执行某些行为。触发器分为多种类型,包括数据触发器(DataTriggers)、样式...

    PyPI 官网下载 | cdk-triggers-0.0.34.tar.gz

    资源来自pypi官网。 资源全名:cdk-triggers-0.0.34.tar.gz

    PyPI 官网下载 | cdk_triggers-0.0.25-py3-none-any.whl

    资源来自pypi官网。 资源全名:cdk_triggers-0.0.25-py3-none-any.whl

    Python库 | fastybird_triggers_module-0.5.5-py3-none-any.whl

    python库。 资源全名:fastybird_triggers_module-0.5.5-py3-none-any.whl

    Python库 | cdk_triggers-0.0.136-py3-none-any.whl

    python库。 资源全名:cdk_triggers-0.0.136-py3-none-any.whl

    Python库 | cdk-triggers-0.0.172.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:cdk-triggers-0.0.172.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Python库 | cdk_triggers-0.0.192-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:cdk_triggers-0.0.192-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Python库 | cdk_triggers-0.0.152-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:cdk_triggers-0.0.152-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    vagrant plugin install vagrant-triggers

    The Vagrantfile requires the plugin vagrant-triggers. To install the plugin run: $ vagrant plugin install vagrant-triggers 安装Docker的管理程序 Deis 在 vagrant,需要安装vagrant-triggers,由于网络...

    trigger-schedule:待命模块

    智能流程管理 安排触发: 安装: 通过NPM ... " triggers " : [ { " id " : " schedule_default " , " type " : " @runnerty-trigger-schedule " } ] } 计划样本: 添加 : { " triggers " : [ {

    triggers.html

    【标题】:“triggers.html”通常指的是MySQL数据库中关于触发器(Triggers)的文档页面。触发器是一种数据库对象,允许在特定的数据操作(如INSERT、UPDATE或DELETE)之前或之后执行预定义的SQL语句,以此实现对...

    wpf简易播放器MediaElement

    在这个简易播放器中,开发者直接在XAML文件中定义了触发器(Triggers),以响应用户的交互事件,比如点击播放按钮。这些触发器会改变MediaElement的状态,执行播放、暂停等操作。值得注意的是,这个播放器设计时并未...

    System.Windows.Interactivity.rar

    4. **自定义行为和触发器**:除了库中提供的行为和触发器,开发者还可以根据需求自定义,继承`Behavior<T>`或`TriggerAction<T>`类。 通过System.Windows.Interactivity库,WPF开发者能够构建更富有表现力和动态性...

    qrtz_blob_triggers.sql

    quartz-2.2.3版本集群部署方式,需要创建的11张表的sql 文件,很多文章没有分享,一定要注意版本,不同的版本sql脚本不同

Global site tag (gtag.js) - Google Analytics