Quartz.net notes (vi) Crontrigger

Source: Internet
Author: User
Tags ranges

Crontriggers is often more useful than simpletrigger, if you need a job-firing schedule then recurs based on Calendar-lik E notions, rather than on the exactly specified intervals of Simpletrigger.

With Crontrigger, you can specify firing-schedules such as "Every Friday at noon", or "every weekday and 9:30am am", or even "Every 5 minutes between 9:00am am and 10:00am am on every Monday, Wednesday and Friday".

Even so, like Simpletrigger, Crontrigger have a startTime which specifies when the schedule are in force, and an (optional) EndTime that specifies when the schedule should is discontinued.

crontriggers is usually more useful than simpletrigger if you need to trigger tasks on a schedule like a calendar instead of firing at specific intervals like Simpletrigger .

With Crontrigger, you can specify " every Friday noon ", or " 9:30 per weekday" or "from every Monday, Wednesday, Friday morning 9:00 to morning 10: 00 every five minutes "such a schedule to trigger.

Even, like Simpletrigger,Crontrigger has a starttime to specify when the schedule starts, and an (optional)endtime to specify when the schedule no longer continues.

Cron Expressions CronAn expression

cron-expressions is used to configure instances of Crontrigger. Cron-expressions is strings that is actually made up of seven sub-expressions, that describe individual details of the s Chedule. These sub-expression is separated with White-space, and represent:

    • 1. Seconds
    • 2. Minutes
    • 3. Hours
    • 4. Day-of-month
    • 5. Month
    • 6. Day-of-week
    • 7. Year (optional field)

Cron Expressions are used to configure Crontrigger instances. A cron expression is a string that consists of 7 sub-expressions. Each sub-expression describes a separate schedule detail. These sub-expressions are separated by spaces, respectively:

1. Seconds sec

2. Minutes minutes

3. Hours hours

4. Days of the day-of-month month

5 Month

6. Day-of-week Days of the week

7. Year (optional field) (optional field)

A example of a complete cron-expression is the string "0 0 12?" * WED "-which means" every Wednesday at 6:00pm ".

The example string for a cron expression is "0 0 12?" * WED ", this means " every Wednesday noon :xx".

Individual sub-expressions can contain ranges and/or lists. For example, the day of week field in the previous (which reads "WED") example could is replaces with "Mon-fri", "MON, WED , FRI ", or even" Mon-wed,sat ".

A single child expression can contain a range or a list. For example: The days of the week in the previous example this field (here is "WED") can be replaced with "Mon-fri", "MON, WED, FRI" or even "Mon-wed,sat".

Wild-cards (the "character") can is used to say "every" possible value of this field. Thereforethe "character in the" month "field of the previous example simply means" every Month ". A ' * ' in the Day-of-week field would obviously mean "every day of the Week".

The wildcard character (' * ') can be used to represent "each " possible value in a domain. Therefore, the * in the month field represents each month, while the * in the day-of-week field represents "every day of the week ".

All of the fields has a set of valid values that can specified. These values should is fairly obvious-such as the numbers 0 to for seconds and minutes, and the values 0 to Urs. Day-of-month can is any of the value 0-31, but the need to is careful about what many days is in a given month! Months can specified as values between 0 and one, or by using the strings JAN, FEB, MAR, APR, May, June, JUL,, SEP, O CT, NOV and DEC. Days-of-week can specified as vaules between 1 and 7 (1 = Sunday) or by using the strings SUN, MON, TU E, WED, THU, FRI and SAT.

The '/' character can used to specify increments to values. For example, if you put ' 0/15 ' on the Minutes field, it means ' every Minutes, starting at Minute zero '. If you used ' 3/20 ' on the Minutes field, it would mean ' every Minutes during the hour, starting at minute three '-or I n other words it was the same as specifying ' 3,23,43 ' in the Minutes field.

The '? ' character is allowed for the Day-of-month and Day-of-week fields. It is used to specify "no specific value". This is useful if you are need to specify something in one of the. See the examples below (and Crontrigger API documentation) for clarification.

The ' L ' character is allowed for the Day-of-month and Day-of-week fields. This character was short-hand for ' last ', but it had different meaning in each of the. For example, the value ' L ' in the Day-of-month field means ' The last day of the month '-day to January, day of the Fe Bruary on non-leap years. If used in the Day-of-week field by itself, it simply means "7" or "SAT". If used in the Day-of-week field after another value, it means ' the last xxx day of the month '-for example "6L" or " Fril "both mean" the last Friday of the month ". When using the ' option ', it's important not to specify lists, or ranges of values, as you'll get confusing results.

The ' W ' is used-Specify the weekday (Monday-friday) nearest the given day. As an example, if you were to specify "15W" as the value of the Day-of-month field, the meaning is: "The nearest weekday To the 15th of the month.

The ' # ' is used to specify "the nth" XXX weekday of the month. For example, the value of "6#3" or "fri#3" in the Day-of-week field means "the third Friday of the month".

values in all domains have a specific legal scope, the legal range of these values is quite obvious, such as: seconds and the legal value of the domain is 0 to 0 to 23,day-of-month is worth legal where the range is 0 to 31, but be aware that the number of days in different months is different. The legal value of the month is 0 to 11. or use the string jan,feb MAR, APR, May, June, JUL,, SEP, OCT, nov  and dec to express. days-of-week can be 1 to 7来 (1= Sunday) or with string sun, MON, TUE, WED, THU, fri  and sat to represent

The '/' character is used to denote the increment of the value, for example , if the minute field is placed in ' 0/15 ', it means "every 15 minutes, starting from 0 ", if you use ' 3/20 ' in the domain of the copy , it means "every 20 minutes, starting from the first 3 minutes "or the other same form is ' 3,23,43 '.

‘?‘ Characters can be used in day-of-month and day-of-week domains, which are used to denote "No value specified ". This is useful when you need to specify a value for one or two fields without having to set up other domains.

' L ' characters can be day-of-month and Used in Day-of-week, this character is shorthand for day-of-month domain represent the last day of the month, that is, January 28th. If it is used in day-of-week, it means day-of-week domain, the "The last week of the month "Fril" all represent the last Friday of this month. When using the

The ' W ' character is used to specify the closest week (specified in the Day-of-week field) to the nearest day of the day . For example: If you specify "15W" for the Day-of-month field , it means "the nearest week of the month 15th" .

' # ' represents the first few weeks of the month. For example: "6#3" or "fri#3" in the Day-of-week field means "third Friday of the month ".

Example Cron Expressions

Here is a few more examples of expressions and their meanings-you can find even more in the API documentation for Cront Rigger

Crontrigger Example 1-an expression to create a trigger that simply fires every 5 minutes

"0 0/5 * * * ?"

Crontrigger Example 2-an expression to create a trigger this fires every 5 minutes at ten seconds after the minute (i.e. 10:00:10 am, 10:05:10 am, etc.).

"10 0/5 * * * ?"

Crontrigger Example 3-an expression to create a trigger it fires at, the, the 12:30pm, and 13:30, on every Wednesday and Friday.

"0 30 10-13 ? * WED,FRI"

Crontrigger Example 4-an expression to create a trigger that fires every half hour between the hours of 8 am and am O n the 5th and 20th of every month. Note that the trigger won't fire at 10:00am, just at 8:00am, 8:30am, 9:00am and 9:30

"0 0/30 8-9 5,20 * ?"

Note that some scheduling requirements is too complicated to express with a single trigger-such as "every 5 minutes bet Ween 9:00am and 10:00am, and every minutes between-5:00pm and 10:00am. The solution in this scenario are to simply create a triggers, and register both of them to run the same job.

Building crontriggers

Crontrigger instances is built using triggerbuilder (for the trigger ' s main properties) and Withcronschedule extension method (for the Crontrigger-specific properties).

You can also use Cronschedulebuilder's static methods to create schedules.

Build a trigger that would fire every other minute, between 8am and 5pm, every day:

trigger = TriggerBuilder.Create() .WithIdentity("trigger3", "group1") .WithCronSchedule("0 0/2 8-17 * * ?") .ForJob("myJob", "group1") .Build();

Build a trigger that'll fire daily at 10:42 am:

//we use Cronschedulebuilder ' s static helper methods Heretrigger = span class= "n" >triggerbuilder. Create () . Withidentity ( "Trigger3"  "group1" ) . Withschedule (cronschedulebuilder. Dailyathourandminute (1042forjob (myjobkey) . Build ()               

Or-

trigger = TriggerBuilder.Create() .WithIdentity("trigger3", "group1") .WithCronSchedule("0 42 10 * * ?") .ForJob("myJob", "group1") .Build();

Build a trigger that'll fire on Wednesdays @ 10:42 am, in a TimeZone and other than the system's default:

Trigger=Triggerbuilder.Create().Withidentity("Trigger3", "group1" ) . Withschedule (cronschedulebuilder . Weeklyondayandhourandminute (dayofweek. Wednesday1042) intimezone (timezoneinfo. Findsystemtimezonebyid ( "Central America Standard Time" )) forjob (myjobkey) . Build ()               

Or-

Trigger=Triggerbuilder.create () . Withidentity ( "Trigger3"  "group1" ) . Withcronschedule (" 0 42 10? * WED " x => x . Intimezone (timezoneinfo. Findsystemtimezonebyid ( "Central America Standard Time" )) forjob (myjobkey) . Build ()               
Crontrigger Misfire Instructions

The following instructions can be used to inform Quartz what it should does when a misfire occurs for Crontrigger. (misfire situations were introduced in the more about Triggers sections of this tutorial). These instructions is defined in as constants (and API documentation have description for their behavior). The instructions include:

    • Misfireinstruction.ignoremisfirepolicy
    • MisfireInstruction.CronTrigger.DoNothing
    • MisfireInstruction.CronTrigger.FireOnceNow

All triggers has the misfireinstrution.smartpolicy instruction available for use, and this instruction is also the Defaul T for all trigger types. The ' smart policy ' instruction is interpreted by Crontrigger as MisfireInstruction.CronTrigger.FireOnceNow. The API documentation for the Crontrigger.updateaftermisfire () method explains the exact details of this behavior.

When building crontriggers, you specify the misfire instruction as part of the Cron schedule (via Withcronschedule exte Nsion method):

trigger = triggerbuilder. Create () . Withidentity ( "Trigger3"  "group1" ) . Withcronschedule ( "0 0/2 8-17 * *?" x => x . Withmisfirehandlinginstructionfireandproceed ()) . Forjob ( "MyJob" build ()             

Quartz.net notes (vi) Crontrigger

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.