Lesson Six Crontrigger
Crontrigger is more commonly used than simpletrigger when you need a calendar-based job scheduler, rather than specifying a time interval exactly as Simpletrigger.
With Simpletrigger, you can specify a trigger schedule such as "Noon every Friday," or "9:30 every weekend", or even "every 5 minutes from January to Monday, five 9:00 to 10:00".
However, as with Simpletrigger, Crontrigger also needs to specify starttime to let the scheduler take effect, specifying Endtime to let the scheduler terminate.
Cron-expression
Cron expressions are used to configure an Crontrigger instance. A cron expression is actually a string of 7 sub-expressions that describes the details of the timesheet. These sub-expressions are separated by spaces and represent:
1, seconds
2, Sub-
3, Hours
4, the number of days in the month
5, Month
6, Days of the week
7, year (optional)
String example of a complete cron expression "0 0 12?" * WED ", meaning" every Wednesday 12:00:00 ".
Each expression contains two permutations of "and", "or", for example, the number of days of the week in the previous example (shown as "WED") can be replaced by "Mon-fri", "Mon,wed,fri", or even "Mon-wed,sat".
The wildcard character ("*") can be used to represent any value of the field, so the month field "*" in the example above represents "every month", and "*" in the day of the week the number of days is clearly expressed as "any day of the week".
The "/" character can be used to represent the increment value. For example, if you write "0/15" in the minute field, which means "trigger every 15 minutes from the No. 0 minute of the hour," If you write "3/20" on the minute field, which means "every 20 minutes from the 3rd minute of the hour,"-in other words, this is specified on the Minutes field "3,23,43" is the same. Note The subtle difference: "/35" does not mean "every 35 minutes," but instead means "every 35 minutes from the No. 0 minute of the Hour", which is equivalent to specifying "0,35".
“?” The characters allow the number of days in the month and days of the week to appear in the field. It is generally used to specify "values that are not concerned". When you need to specify an indeterminate value in both fields it is very convenient that the character cannot be used in other fields. You can see the following example (or Crontrigger) for a more detailed explanation.
The "L" character allows the number of days in the month and days of the week to appear in the field. This character is the abbreviation for "last", but it has different meanings in both fields. For example, the "L" character appears in the number of days in the month field that represents "the last day of the month"-January 31, common year February 28. If the character is used alone in the days of the week, it simply means "7" or "SAT". However, in the days of the week, the word trailing characters after other values, indicating "the last one weeks of the month"-for example, "6L" or "Fril" means "last Friday of the month". You can also specify the number of offsets for the last day of the month, such as "L-3" for the last three days of the calendar month. When you use the "L" character, it's best not to use an array or range of values, otherwise you'll be surprised and difficult to understand the results.
The "W" character is used to specify the most recent business day for a given date (weekday refers to from Monday to Friday). For example, if you specify a value of "15w" for the number of days in the month, this means "nearest working day from 15th per month".
The "#" character is used to specify the nth working day of the month, for example, the value of the day of the week field is "6#3" or "fri#3" means "third Friday of the month".
Here are some examples and meanings of expressions-you can find more information in org.quartz.CronExpression.
Cron-expression case
The cron case only means that it is triggered every 5 minutes:
"00/5 * * *?"
The cron case, 2--, is triggered once every 5 minutes, after 10 seconds (such as morning 10:00:10,10:05:10, etc.):
"100/5 * * *?"
The Cron case 3--represents each Wednesday to Friday, in the morning 10:30,11:30,12:30 and 13:30 points trigger:
"030 10-13?" * Wed,fri "
The Cron case 4--represents every half-hour from 5th to 20th, 8 o'clock in the morning to 10 o'clock, and note that this trigger is triggered only at 8:00,8:30,9:00 and 9:30, and 10:00 does not trigger:
"00/30 8-9 5,20 *?"
Note that some scheduling requirements are too complex, such as "every 5 minutes from 9:00 to 10:00, 1:00 to 10:00 for every 20 minutes," and cannot be represented by a single trigger. The solution to this scenario is to create two simple triggers that register them in the scheduler to run the same job task.
Build Crontriggers
The Crontrigger instance object can be created using Triggerbuilder (for the main parameters of the trigger) and Cronschedulebuilder (for the specified parameters of the Crontrigger). In order to use these to create a class that satisfies the DSL format, use static import:
Create a trigger that fires every 2 minutes from 8 o'clock in the morning to 5 o'clock in the afternoon every day:
1Trigger =triggerbuilder.create ()2. Withidentity ("Trigger3","group1")3. Withcronschedule ("0 0/2 8-17 * *?")4. Forjob ("MyJob","group1")5. Build ();
Create a trigger that fires once a day 10:42 minutes:
1 // 2 Trigger = Triggerbuilder.create () 3 . Withidentity ( trigger3 ", " group1 " " 4 . Withschedule (Cronschedulebuilder.dailyathourandminute (10 , 42 5 6 . Build ();
Or
1Trigger =triggerbuilder.create ()2. Withidentity ("Trigger3","group1")3. Withcronschedule ("0 * *?")4. Forjob ("MyJob","group1")5. Build ();
Crontrigger Trigger Failure command
Crontrigger has several instructions to tell quartz what to do if the trigger fails. (In the fourth lesson more about triggers that have been described in the case of a trigger failure). These directives are designed as constants in the Crontrigger class (including Javadoc that describe their behavior). Directives are:
Crontrigger Trigger Failure Instruction constant
Misfireinstruction.ignoremisfirepolicy
misfireinstruction.crontrigger.donothing
Misfireinstruction.crontrigger.fireoncenow
All triggers can use the Trigger.misfire_instruction_smart_policy directive, and this is the default instruction for all triggers.
The "Smart strategy" Directive can be interpreted from the Crontrigger misfire_instruction_fire_now. The Updateaftermisfire method of Crontrigger in the document explains the more detailed information about the dynamic selection behavior.
When you create Crontrigger, you can trigger a failure instruction as part of the scheduler by Cronschedulerbuilder instructions.
1Trigger =triggerbuilder.create ()2. Withidentity ("Trigger3","group1")3. Withcronschedule ("0 0/2 8-17 * *?", x =x4 . Withmisfirehandlinginstructionfireandproceed ())5. Forjob ("MyJob","group1")6. Build ();
Translation Quartz.net Framework Tutorial (Chinese Version) 2.2.x Lesson VI Crontrigger