Quartz.net Notes (iv) more on triggers

Source: Internet
Author: User

Like jobs, triggers is relatively easy-to-work with, but does 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 is different types of triggers, that's can select to meet different scheduling needs.

with Job ,trigger is very easy to use, but it has some options that need to be noticed and understood, whiletrigger have different types to choose from as needed.  

Common Trigger Attributes

Aside from the fact and all trigger types has triggerkey properties for tracking their identities, there is a number of Other properties, that is common to all trigger types. These common properties is set using the Triggerbuilder when is building the trigger definition (examples of that WI ll follow).

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

  • The Jobkey property indicates the identity of the job, that should is executed when the trigger fires.
  • The Starttimeutc property indicates when the trigger's schedule first comes into affect. The value is a DateTimeOffset object, defines a moment in time on a given calendar date. For some trigger types, the trigger would actually fire at the start time, for others it simply marks the time that the Sch Edule should start being followed. This means your can store a trigger with a schedule such as "Every 5th day of the month" during January, and if the Startti Meutc property was set to April 1st, it would be a few months before the first firing.
  • The Endtimeutc property indicates when the trigger ' s schedule should no longer is 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 would fire for I T ' s last time on June 5th.

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

Priority level

Sometimes, when you had many Triggers (or few worker threads in your quartz.net thread pool), quartz.net could not have ENO Ugh resources to immediately fire all of the Triggers, that is scheduled to fire at the same time. In this case, 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" on a Trigger. If N Triggers is to fire on the same time, but there is only Z worker threads currently available, then the first z Trig Gers with the highest priority would be executed first. If you don't set a priority to a Trigger, then it'll use the default priority of 5. Any of the integer value is allowed to priority, positive or negative.

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

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

Misfire Instructions

Command not triggered

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

TriggerAnother important attribute of this is its“Misfire Instruction (command not triggered)". If a persistent trigger "misses" the trigger time because the scheduler is turned off , no triggering occurs. Different types of triggers have different non-triggering directives. By default, they use a " Smart policy " directive -- different actions depending on the trigger type and configuration. When Scheduler begins, it looks for all persistent triggers that are not triggered , and then updates them according to the non-triggering instructions configured for each trigger. When you start using Quartz in your project , you should familiarize yourself with the non-triggering directives defined on each type of trigger. A detailed description of the non-triggering instruction information will be given in the Guide course for each specific type of trigger. You can configure the non-triggering directives for a given trigger instance by using the Misfireinstruction property.

Calendars

Quartz.net Calendar objects implementing ICalendar interface can be associated with triggers at the time of the trigger is St ORed in the scheduler. Calendars is useful for excluding blocks of time from the the trigger ' s firing schedule. For instance, could create a trigger that fires a job every weekday at 9:30am, but then add a Calendar that excludes All of the business ' s holidays.

Calendar's can is any serializable objects that implement the ICalendar interface and which looks like this:

The Quartz Calendar object is associated with trigger when trigger is stored in scheduler . Calendar is very useful in the world of trigger triggering schedules. For example: you want to create a trigger on every weekday morning 9:30, then add a calendar that excludes all holidays.

The calendar can be any serialized object that implements the calendar interface. Looks like this:

1 namespaceQuartz2 {3      Public InterfaceICalendar4     {5         stringDescription {Get;Set; }6 7ICalendar Calendarbase {Set;Get; }8 9         BOOListimeincluded (DateTimeOffset timeutc);Ten  One DateTime GETNEXTINCLUDEDTIMEUTC (DateTimeOffset timeutc); A     } -}

Even though calendars can ' block out ' sections of time as narrow as a millisecond, most likely, you'll be interested in ' B Locking-out ' entire days. As a convenience, Quartz.net includes the class Holidaycalendar, which does just that.

Calendars must is instantiated and registered with the scheduler via the Addcalendar (..) method. If you use the Holidaycalendar, after instantiating it, you should the use of its addexcludeddate (DateTime date) method in order to P Opulate it with the "wish to" has excluded from scheduling. The same calendar instance can be used with multiple triggers such as this:

Calendar Example

1Holidaycalendar cal =NewHolidaycalendar ();2 Cal. Addexcludeddate (somedate);3 4Sched. Addcalendar ("myholidays", Cal,false);5 6Itrigger T =triggerbuilder.create ()7. Withidentity ("Mytrigger")8. Forjob ("MyJob")9. Withschedule (Cronschedulebuilder.dailyathourandminute (9, -))//execute job daily at 9:30amTen. Modifiedbycalendar ("myholidays")//But not on holidays One         . Build (); A  -     //.. schedule job with Trigger -  theItrigger t2 =triggerbuilder.create () -. Withidentity ("MyTrigger2") -. Forjob ("MyJob2") -. Withschedule (Cronschedulebuilder.dailyathourandminute ( One, -))//Execute job daily at +. Modifiedbycalendar ("myholidays")//But not on holidays -         . Build (); +  A     //.. schedule job with Trigger2

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

See the Quartz.Impl.Calendar namespace for a number of ICalendar implementations, may suit your needs.

Appendix:

Triggerutils-triggers made easy ( triggerutils -- make Triggers Easy)

The Triggerutils class contains a handy way to create triggers and dates. Using this class makes it easy to trigger triggers in every minute, hour, day, week, month, etc. Using this class can also produce distances that trigger the nearest good, minute, or hour, which is useful for setting the trigger start time.

Triggerlisteners

Finally, as job ,triggers can register listeners, and objects that implement the Triggerlistener interface can receive notifications that triggers are triggered.

Quartz.net Notes (iv) more on triggers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.