Quartz.NET笔记(四) 关于Triggers更多内容

Like jobs, triggers are relatively 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.NET. Also, as noted earlier, there are different types of triggers, that you can select to meet different scheduling needs. 

同Job一样,trigger非常容易使用,但它有一些可选项需要注意和理解,同时,trigger有不同的类型,要按照需求进行选择。 

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 StartTimeUtc property indicates when the trigger‘s schedule first comes into affect. The value is a DateTimeOffset 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 StartTimeUtc property is set to April 1st, it will be a few months before the first firing.
  • The EndTimeUtc 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.NET thread pool), Quartz.NET 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.NET 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.NET‘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.NET 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 API documentation. More specific information about misfire instructions will be given within the tutorial lessons specific to each trigger type.

Trigger的另一个重要属性就是它的“misfire instruction(未触发指令)”。如果因为scheduler被关闭而导致持久的触发器“错过”了触发时间,这时,未触发就发生了。不同类型的触发器有不同的未触发指令。缺省情况下,他们会使用一个“智能策略”指令——根据触发器类型和配置的不同产生不同动作。当scheduler开始时,它查找所有未触发的持久triggers,然后按照每个触发器所配置的未触发指令来更新它们。开始工程中使用Quartz的时,应熟悉定义在各个类型触发器上的未触发指令。关于未触发指令信息的详细说明将在每种特定的类型触发器的指南课程中给出。可以通过MisfireInstruction属性来为给定的触发器实例配置未触发指令。

Calendars

Quartz.NET Calendar objects implementing ICalendar interface can be associated with triggers at the time the trigger is 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 ICalendar interface, which looks like this:

Quartz Calendar对象在trigger被存储到scheduler时与trigger相关联。Calendar对于在trigger触发日程中的采用批量世间非常有用。例如:你想要创建一个在每个工作日上午9:30触发一个触发器,那么就添加一个排除所有节假日的日历。

Calendar可以是任何实现Calendar接口的序列化对象。看起来如下:

 1 namespace Quartz
 2 {
 3     public interface ICalendar
 4     {
 5         string Description { get; set; }
 6
 7         ICalendar CalendarBase { set; get; }
 8
 9         bool IsTimeIncluded(DateTimeOffset timeUtc);
10
11         DateTime GetNextIncludedTimeUtc(DateTimeOffset timeUtc);
12     }
13 } 

Even though 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.NET includes the class 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(DateTime 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:

Calendar Example

 1 HolidayCalendar cal = new HolidayCalendar();
 2     cal.AddExcludedDate(someDate);
 3
 4     sched.AddCalendar("myHolidays", cal, false);
 5
 6     ITrigger t = TriggerBuilder.Create()
 7         .WithIdentity("myTrigger")
 8         .ForJob("myJob")
 9         .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(9, 30)) // execute job daily at 9:30
10         .ModifiedByCalendar("myHolidays") // but not on holidays
11         .Build();
12
13     // .. schedule job with trigger
14
15     ITrigger t2 = TriggerBuilder.Create()
16         .WithIdentity("myTrigger2")
17         .ForJob("myJob2")
18         .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(11, 30)) // execute job daily at 11:30
19         .ModifiedByCalendar("myHolidays") // but not on holidays
20         .Build();
21
22     // .. 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 Quartz.Impl.Calendar namespace for a number of ICalendar implementations that may suit your needs.

附录:

TriggerUtils - Triggers Made EasyTriggerUtils——使Triggers变得容易)

TriggerUtils类包含了创建触发器以及日期的便捷方法。使用这个类可以轻松地使触发器在每分钟,小时,日,星期,月等触发。使用这个类也可以产生距离触发最近的妙、分或者小时,这对设定触发开始时间非常有用。

TriggerListeners

最后,如同job一样,triggers可以注册监听器,实现TriggerListener接口的对象将可以收到触发器被触发的通知。

时间: 2024-10-29 03:17:57

Quartz.NET笔记(四) 关于Triggers更多内容的相关文章

(原创)c#学习笔记05--变量的更多内容01--类型转换01--隐式转换

第五章 变量的更多内容 本章内容: ●  如何在类型之间进行隐式和显式转换 ●  如何创建和使用枚举类型 ●  如何创建和使用结构类型 ●  如何创建和使用数组 ●  如何处理字符串值 首先要讨论的主题是类型转换,即把值从一种类型转换为另一种类型. 接着阐述另外一些类型的变量: 枚举——变量类型,用户定义了一组可能的离散值,这些值可以用人们能理解的方式使用. 结构——合成的变量类型,由用户定义的一组其他变量类型组成. 数组——包含一种类型的多个变量,可以以索引方式访问各个数值. 这些类型比前面使

Sharepoint2013搜索学习笔记之设置外网内容源(四)

第一步,进入管理中心,点击管理应用程序,点击search service 应用程序进入到搜索管理配置页面,选择内容源 第二步,点击新建内容源,给内容源命名,在爬网内容类型中选网站,在爬网设置中选中仅对每个开始地址的第一个页面进行爬网,在这一步需要注意爬网设置的选择,如果选择自定义,服务器越距配置过大可能会造成对全网进行爬网,让你的服务器资源急剧耗尽. 第三步,爬网计划点启用增量爬网,创建增量爬网计划,注意,完全爬网会对所有内容源重新进行爬网,增量爬网只访问上次爬网之后被更新的内容.新建的内容源就

iOS9-by-Tutorials-学习笔记四:APP-瘦身

iOS9-by-Tutorials-学习笔记四:APP-瘦身 本文版权归作者所有,如需转载请联系孟祥月 CSDN博客:http://blog.csdn.net/mengxiangyue 独立博客:http://mengxiangyue.com 这篇文章在书中的标题是App Thinning,这里我给翻译成了App 瘦身. 本文然然进行了一些语法的修改,很开心她为我修改这些东西.她说我转折只会用但是,被她这么一说想想还真是只是会用但是,嘿嘿. iPhone经过这几年的发展,已经发生了很大的变化,例

《高效能程序员的修炼》读书笔记四

第一章第三节:如何培养写作习惯 -------------------------------- 我个人一直坚信要成为一名优秀的程序员,其实和写代码是没有多大关系的(刚好本书作者也是这样的想法).当然,作为一名程序员的话,具有一定水平的技术能力还是需要的.但,个人觉得更重要的是良好地沟通技巧. 本书作者的合伙人Joel Spolsky有过这样一段话: 杰出的程序员跟勉强过得去的程序员之间的差别,不在于他们掌握了多少种编程语言,也不在于他们谁更擅长Python或Java.真正的关键是,他们能不能把

Quartz学习笔记

什么是job schedule system? job schedule system是负责在预定义的时间执行或者通知某个应用组件的系统.举个例子来说,比如在每周一早上9:30发送email通知客户最新的业务情况. java.util.Timer和java.util.TimerTask Timer和TimerTask是可以完成job schedule的两个jdk提供的类,不过这不能称为一个system.Timer和TimerTask是很简单的,不直接支持持久化任务,线程池和类似日历(calend

代码管理工具 --- git的学习笔记四《重新整理git(1)》

1.创建版本库 mkdir  创建目录 cd  地址,到该地址下 pwd 显示当前目录 1.创建目录 $ mkdir startGit $ cd startGit $ pwd 显示当前目录 或者cd到桌面,然后再创建目录 2.初始化版本库 $ git init 初始化仓库 提示信息:Initialized empty Git repository in /Users/xingzai/Desktop/startGit/.git/ 建立一个空的git仓库在/Users/xingzai/Desktop

Linux学习笔记四:Linux的文件搜索命令

1.文件搜索命令  which 语法:which [命令名称] 范例:$which ls  列出ls命令所在目录 [[email protected] ~]$ which ls alias ls='ls --color=auto' /bin/ls 另外一个命令:whereis [名称名称],也可以列出命令所在目录. [[email protected] ~]$ whereis ls ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/ma

R实战读书笔记四

第三章 图形入门 本章概要 1 创建和保存图形 2 定义符号.线.颜色和坐标轴 3 文本标注 4 掌控图形维数 5 多幅图合在一起 本章所介绍内容概括如下. 一图胜千字,人们从视觉层更易获取和理解信息. 图形工作 R具有非常强大的绘图功能,看下面代码. > attach(mtcars) > plot(wt, mpg) > abline(lm(mpg~wt)) > title("Regression of MPG on Weight") > detach(m

老男孩培训视频听课笔记四(在51cto上听的)

实际操作:     1.创建一个目录 mkdir 语法:mkdir [-mp] [目录名称]            一般与配合cd tree pwd等命令来实现,整个操作     2.建议一个文件 利用touch命令来完成 语法:touch [path]/filename            批量创建文件:        for f in `seq 1000`;do touch $f.txt;done         创建文件的命令很多:vi echo> > cat等命令      3.文件