Quartz.net Open Source Job scheduling framework series (ii): crontrigger-turn

Source: Internet
Author: User
Tags local time

Crontriggers is more flexible and useful than simpletrigger, for more complex task triggering rules, such as "12:00 every Sunday Night" for backup tasks, Simpletrigger is not competent, You can only select Crontriggers. With Crontrigger, you can not only implement the task of "12:00" every Sunday night, but also perform "every 5 minutes from 9:00 to 10:00 every Monday/Wednesday/Friday" Perform an automated task.

1 Cron Expressions

Cron-expressions is a crontrigger that is instantiated by the user configuration. Cron-expressions is a 6-or 7-character string expression with each character representing a specific meaning and a range of values. Each character is separated by a space, and its expression is (from left to right):

  

Generally speaking, the cron-expressions is 6-bit, the year generally omitted. The description of each character in the character is the meaning of the characters + range (with []) + available special characters ({}). For example seconds [0-59] {,-*/} means the first character is seconds, the value range is 0-59,  Also available special characters identifier,-*/For example "0 15 10?" * 6L "means the last Friday 10:15 of the month. The following describes the meaning of special characters:

Special characters

Meaning

*

All value (all values)

?

No specific value (no specific value)

-

Range 0-10

,

Added value 1,2,5

L

The different character positions represent different meanings, "L" represents the last day of the month in the number of days of the month, and if it appears alone in the day of the week, representing 7, is the end of the week Saturday (English idioms).

W

The most recent of a given date (Monday to Friday). Day of the Month "15W", representing the most recent weekday from the 15th day of the month

#

The number of days of the month, "6#3" means the month of three week 5 (6 = Friday and "#3" = third in the month).

2 Common expressions

Some common cron-expressions examples are given below:

An expression

Meaning

0 0 12 * *?

12pm Daily Boot

0 15 10? * *

10:15am Start every day

0 15 10 * *?

10:15am Start every day

0 15 10 * *? *

10:15am Start every day

0 15 10 * *? 2005

10:15AM starts every day in 2005

0 * 14 * *?

Starts every minute from 2pm to 2:59pm per day

0 0/5 14 * *?

Starts every 5 minutes between 2pm and 2:55pm every day

0 0/5 14,18 * *?

Starts every 5 minutes from 2pm to 2:55pm and 6pm to 6:55pm Daily

0 0-5 14 * *?

Starts every minute from 2pm to 2:05pm per day

0 10,44 14? 3 WED

Every March, Wednesday 2:10pm to 2:44pm to start.

0 15 10? * Mon-fri

The 10:15am starts every week to Friday.

0 15 10 15 *?

10:15am on the 15th day of every month.

0 L *?

The 10:15am of the last day of each month starts

0 L-2 *?

The second day of every month to the last day of the 10:15am to start

0 15 10? * 6L

The last Friday 10:15am of the month is launched.

0/1 * * * *?

Start every second

0 15 10? * 6L 2002-2005

Starting from 2002 to 2005 10:15am of the last Friday of the month

0 15 10? * 6#3

The third Friday 10:15am is launched every month.

0 0 12 1/5 *?

Start at 12pm every 5 days from the first day of each month

0 11 11 11 11?

The 11:11am of November 11 is launched every year.

3 Crontrigger Example

By the common expressions above we know the expression "0/1 * * * *?" Represents the execution per second, and its crontrigger is defined as follows:

1                 Itrigger trigger = Triggerbuilder.create () 2                . Withidentity ("CronTrigger1", "Simplegroup") 3                . Withcronschedule ("0/1 * * * *?", x = x4                    . Withmisfirehandlinginstructionfireandproceed ()) 5                . Forjob ("Hellojob", "Simplegroup") 6                . Build ();

You can also use the following method to define (note that the default time is not the system time-Beijing time, so if the print out the first time the job printing is not the correct time, but does not seem to affect the use of):

1//define Job 2 Ijobdetail job3 = jobbuilder.create<colorjob> () 3. Withidentity ("Job3", "Group1") 4. Build (); 5//Definition Crontrigger 6 icrontrigger crontrigger = (Icrontrigger) triggerbuilder.create () 7                              . Withidentity ("Crontrigger", "group1") 8                               . Withcronschedule ("0/20 * * * *?", x = x 9                                         .) Intimezone (Timezoneinfo.findsystemtimezonebyid ("China Standard Time"))//invalid?                              Build (); 11//Default start time: DateTimeOffset ScheduleTime3 = sched. Schedulejob (JOB3, Crontrigger); 13//Convert to local time (Beijing time) DateTimeOffset dt=  scheduletime3.tolocaltime ();//15// Beijing time is the default time + 8 hours DateTimeOffset Scheduletimechina = scheduletime3.addhours (8); 17//Return cron Expression18 string Cronexpression = crontrigger.cronexpressionstring;

If, under the defined time rules, I want to exclude some dates, how do I do that? Modifiedbycalendar can be implemented:

 1//Excluded date 2 Holidaycalendar cal = new Holidaycalendar (); 3 datetime dt exclusion = new DateTime (2015, 12, 1); 4 Cal. Addexcludeddate (dt excluded); 5 6 Sched. Addcalendar ("Myholidaycalendar", Cal, False, True); 7//Define job 8 Ijobdetail job3 = jobbuilder.create<colorjob> () 9. Withidentity ("Job3", "group1") 10.                                          Build (); 11//Definition cronTrigger12 Icrontrigger Crontrigger = (Icrontrigger) triggerbuilder.create () 13 . Withidentity ("Crontrigger", "group1") 14. Withcronschedule ("0/20 * * * *?", x = x15. Intimezone (Timezoneinfo.findsystemtimezonebyid ("China Standard Time"))//invalid? 16. Modifiedbycalendar ("Myholidaycalendar") 17. Build (); 18//Default start time datetimeofFset ScheduleTime3 = sched.             Schedulejob (JOB3, Crontrigger); 20//Convert to local time (Beijing time) 21//2015-12-02 00:00:00 +08:00 does not contain 2015-12-0122 DateTimeOffset dt = Scheduletime3.tolocaltime (); 23//Beijing time for default time + 8 hours for DateTimeOffset Sch Eduletimechina = scheduletime3.addhours (8); 25//Return cron Expression26 string cronexpression = Crontrig Ger. cronexpressionstring;

Quartz.net Open Source Job scheduling framework series (ii): crontrigger-turn

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.