Spring Cron-Expressions (timers)

Source: Internet
Author: User
Tags addall bulk insert numeric value

When writing the timer, record it:

A cron expression is a string of characters separated by 5 or 6 spaces, starting 6 or 7 fields, each representing a meaning, and cron having the following two syntaxes
format:
Seconds Minutes Hours dayofmonth Month DayofWeek year or
Seconds Minutes Hours dayofmonth Month DayofWeek
The following characters can appear for each field:
Code
Seconds: Can appear,-*/four characters, valid range is 0-59 integer
Minutes: Can appear,-*/four characters, valid range is 0-59 integer
Hours: Can appear,-*/four characters, valid range is 0-23 integer
DayOfMonth: Can appear,-*/? L W c Eight characters, integer with a valid range of 0-31
Month: Can appear,-*/four characters, valid range is 1-12 integer or Jan-dec
DayofWeek: Can appear,-*/? L C #四个字符, an integer with a valid range of 1-7 or a range of Sun-sat two. 1 means Sunday, 2 means Monday, and so on
year: can appear,-*/four characters, valid range is 1970-2,099 years
 each field uses numbers, but the following special characters can appear, meaning:
(1) *: Indicates any value that matches the field, and if you use * in the Minutes domain, the event will be triggered every minute.
   
(2)?: can only be used in DayOfMonth and DayOfWeek two domains. It also matches any value of the field, but it does not. Because DayOfMonth and DayOfWeek will affect each other. For example, to trigger a schedule on the 20th of each month, regardless of whether the 20th is the day of the week, you can only use the following: 13 13 15 20 *?, the last one can only be used? , and cannot use *, If you use * means that the day of the week will trigger, it is not.
   
(3)-: Indicates a range, for example, using 5-20 in the Minutes domain, indicating that it is triggered every minute from 5 minutes to 20 minutes
 
(4)/: Indicates that the start time starts triggering and then fires once every fixed time, such as using 5/20 in the Minutes domain, which means that 5 minutes is triggered once, and 25,45 is triggered separately.
   
(5),: Indicates that the enumeration value value is listed.    For example, using 5,20 in a minutes domain means firing at 5 and 20 minutes per minute.
   
(6) L: Indicates last, only in the DayOfWeek and dayofmonth domains, if 5L is used in the DayOfWeek domain, it means that the last Thursday triggers.
   
(7) W: Indicates a valid weekday (Monday to Friday), which can only appear in the DayOfMonth domain, and the system will trigger the event on the most recent active weekday from the specified date. For example: Use 5W in DayOfMonth, if 5th is Saturday, it will be triggered on the most recent weekday: Friday, 4th. If 5th is Sunday, it will be triggered on 6th; If 5th is a day from Monday to Friday, it will be triggered on 5th. Another point, W's recent search won't cross the month

(8) LW: These two characters can be used together, indicating the last working day of the month, that is, the last Friday.
(9) #: Used to determine the number of weeks per month, can only appear in the DayOfMonth domain.   For example in 4#2, which represents the second Wednesday of a month.  
For a few examples:
0 0 2 1 * * * indicates that the task is scheduled 2 o'clock in the morning on the 1st of each month
0. * Mon-fri indicates that the job is performed 10:15 every day from Monday to Friday
0.6L 2002-2006 represents the last of every month of 200-2006 years Friday 10:15 execute a job
91linux
a cron expression has at least 6 (or possibly 7) time elements that have a space separation. In order of
seconds (0~59)
minutes (0~59)hours (0~23)days (months) (0~31, but you need to consider the number of days of your month)month (0~11)Days (weeks) (1~7 1=sun or Sun,mon,tue,wed,thu,fri,sat)7. Year (1970-2099)each of these elements can be a value (such as 6), a continuous interval (9-12), a time interval (8-18/4) (/= every 4 hours), a list (1,3,5), and a wildcard character. because the two elements of the date in month and day of the week are mutually exclusive, one of these settings must be set.0 0 10,14,16 * * Every day 10 o'clock in the morning, 2 o'clock in the afternoon, 4.
0 0/30 9-17 * *? Every half hour for nine to five working hours
0 0? * WED said every Wednesday noon 12 o'clock
"0 0 12 * *?" trigger 12 o'clock noon every day.
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Daily 10:15 Trigger
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 trigger
"0 * 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:59 daily
"0 0/5 14 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily
"0 0/5 14,18 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily and from 6 o'clock in the afternoon to 6:55
"0 0-5 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:05 daily
"0 10,44 14?" 3 WED "2:10 and 2:44 triggers in Wednesday of every March
"0 15 10?" * Mon-fri "Monday to Friday 10:15 trigger
"0 15 10 15 *?" 15th 10:15 per month
"0 L *?" 10:15 on the last day of the month
"0 15 10?" * 6L "Last month of Friday 10:15 trigger
"0 15 10?" * 6L 2002-2005 "2002 to 2005 the last of the monthly Friday 10:15 trigger
"0 15 10?" * 6#3 "Monthly third Friday 10:15 trigger
Some sub-expressions can contain ranges or listsFor example: subexpression (Day (week)) can be "Mon-fri", "Mon,wed,fri", "Mon-wed,sat"the "*" character represents all possible valuestherefore, "*" in the sub-expression (month) represents the meaning of each month, "*" in the subexpression (Day (week)) represents every day of the week the "/" character is used to specify the increment of the numeric valueFor example: "0/15" in sub-expressions (minutes) means starting from the No. 0 minute, every 15 minutes"3/20" in the sub-expression (minutes) means that every 20 minutes (it is the same as "3,23,43") starting from the 3rd minute
"? "Character is used only for days (months) and days (weeks) of two sub-expressions, indicating that no value is specifiedwhen one of the 2 sub-expressions is assigned a value, in order to avoid a conflict, you need to set the value of another subexpression to "? " the "L" character is used only for days (months) and days (weeks) of two sub-expressions, which is the abbreviation for the word "last"but it has a different meaning in two sub-expressions. in the day (month) subexpression, "L" represents the last day of the one-monthin the day (week) Self-expression, "L" represents the last day of one weeks, the SATIf there is something specific before "L", it has other meanings.For example: "6L" means the 6th day of the month, and "Fril" represents the Friday of the month.Note: When using the "L" parameter, do not specify a list or range, as this can cause problems special characters allowed for field allowed values
seconds 0-59,-*/
Sub-0-59,-*/
hours 0-23,-*/
Date 1-31,-*?/L W C
month 1-12 or JAN-DEC,-*/
Week 1-7 or Sun-sat,-*?/L C #
year (optional) leave blank, 1970-2099,-*/

Note: Day and week are Ninchen one
?: Represents dispensable
*: Represents every year
Seconds of day and month
0 0 0 10 12? 2009//Representative: December 10, 2009 0:0 0 Seconds Execution (day of the week: '? ') Representative ignored)
0 0 0 10 12? *//representative: December 10 0:0 0 seconds per year execution
0 0 0 10 *? Representative: 10th 0:0 0 seconds per month execution
0 0 1 1 *? Rep: 1th per month, 1 pips, 0 minutes, 0 seconds execution
0 0 1 1 3,6,9? Representative: March June September, 1th, 1 points 0 minutes 0 seconds execution
0 0 1 1 2-5?

For example:

Added in Spmvc-servlet.xml:

< Task:scheduled-tasks > <!-- Daily 1:00 A.M. timed reminder message  - <  ref= "Messagetaskserviceimpl"  method= "Executemessagetask" Cron = "0 * * *?" /> </ Task:scheduled-tasks >

2, Imessagetaskservice add interface: void Executemessagetask (); Message task

3, Messagetaskserviceimpl in the implementation:

   /*** Execute Message task*/@Override Public voidExecutemessagetask () {Log.info ("= = start to perform the task, extract the message = ="); /**1) Sales forecast **/Log.info ("= = Calculate sales forecast ...."); List<Message> saletrargetmsglist =Getsaletarget (); /**2) Contact Birthday reminder **/Log.info ("= = Calculate contact Birthday reminder ...."); List<Message> birthdaylist =Selectallcontactshasbirthday (); /**3) Contract receipt (XXX means contract name) **/Log.info ("= = to calculate the contract collection reminder ...."); List<Message> receivingmsglist =getreceiving (); /**5) Customer Recovery alert **/Log.info ("= = Calculate Customer Recycling alerts ...."); List<Message> customlist =Selectwarningcustomer (); /**6) Recycling of expired high seas customers **///log.info ("= = recycling of expired high seas customers ....");//int recount = Updaterecoverycustomer ();                /**7) Processing historical Data add contact name first Letter **///log.info ("= = recycling of expired high seas customers ....");//int recount = Updatecontactsfirstletter (); //BULK INSERT MessagesLog.info ("= = = BULK Insert Message ....")); intCountsize = Saletrargetmsglist.size () +birthdaylist.size () +receivingmsglist.size () +customlist.size (); List<Message> msglist =NewArraylist<message>(countsize);        Msglist.addall (saletrargetmsglist);        Msglist.addall (birthdaylist);        Msglist.addall (receivingmsglist);        Msglist.addall (customlist); intresult =Messagemapper.batchinsert (msglist); Log.info ("= = = Total" +countsize+ ", insert" +result+ "bar ...."); Log.info ("To the end of Mission = ="); }

Spring Cron-Expressions (timers)

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.