Spring timer, timed execution (quartz)

Source: Internet
Author: User

Unlike the timer that inherits the TimerTask, this timer is more powerful and can specify the nth second of every minute, the nth of each day, and the weekly. Every year. To run this timer regularly. So here's how to use the quartz timer.

spring3.0 after the support of quartz, if the spring version is too low, the amount ...

First of all, it must be to download the jar package, naturally Quartz.***.jar! This is a must.

Building a class, this class contains the method to be executed by the timer, of course, the method name can be casually written, then in the configuration file defined in the line

 Public class Testtimer {    // method name can be written casually, a (), B, (), S (), B (), SB () ....     Public void run () {        System.out.println ("Go .....");}    }

Here is a configuration file, spring configuration file, yes

<!-- ............................... This configuration file is specifically configured for scheduled Tasks ........ .... ..... -    <BeanID= "Job2"class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">        <!--This is the class of the timer. -        < Propertyname= "TargetObject">            <Beanclass= "Com.tbs.timer.TestTimer" />        </ Property>        <!--This is the method to be executed, which is the previously defined run -        < Propertyname= "Targetmethod"value= "Run" />    <!--<property name= "concurrent" value= "false"/> -    <!--Job not concurrent scheduling, also means that single-threaded, not write is true -    </Bean>    <!--This is used to configure the execution time of the scheduled task -    <BeanID= "Crontrigger"class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean">        < Propertyname= "Jobdetail"ref= "Job2" />        < Propertyname= "Cronexpression"value= "*/1 * * * *?" />    </Bean>    <!--The factory that configures the execution timer, the bean that is configured in the list, is loaded into the factory, and then runs periodically -    <Beanclass= "Org.springframework.scheduling.quartz.SchedulerFactoryBean">        < Propertyname= "Triggers">            <List>                <refBean= "Crontrigger" />            </List>        </ Property>    </Bean>

This is the end, directly start the project on the line, you can directly copy the above code, you can run.

But there is one problem:

<name= "Cronexpression"  value= "*/1 * * * *?" />

What is that?

This is a timed cycle, given the instructions, the following excerpt from the network

Cron Expression Time field

The time field of a cron expression allows you to use some special characters, in addition to setting values, to provide functions such as lists, scopes, wildcards, and so on.
0 - 59 , - * / 0 - 59 , - * / 小时 0 - 23 , - * / 1 - 31 , - * ? / L W C 1 - 12 or JAN-DEC , - * / 周几 1 - 7 or SUN-SAT , - * ? / L C # 年 (可选字段) empty, 1970 - 2099 , - * /
Asterisk (*): Available in all fields, representing every moment of the corresponding time field, for example, * in the minute fields, denotes "per minute", question mark (?): The character is used only in the date and week fields, and it is usually specified as "meaningless value", which is equivalent to a dot character; minus sign (-): a range If you use "10-12" in the hour field, it means from 10 to 12 points, that is, 10,11,12; comma (,): To express a list value, such as "Mon,wed,fri" in the week field, that is Monday, Wednesday, and Friday; slash (/): X/y to express a sequence of equal steps , X is the starting value, and Y is the increment step value. If you use 0/15 in the minute field, it is expressed as 0,15,30 and 45 seconds, and 5/15 in the minute field represents 5,20,35,50, you can also use */y, which is equivalent to 0/y; L: The character is used only in the date and week fields, meaning "last". But it has a different meaning in two fields. L In the Date field, the last day of the month, such as number 31st in January, 28th for non-leap year February, or Saturday, equivalent to 7 if L is used in the week. However, if L appears in the Week field, and there is a value x in front of it, it means "last x days of the Month", for example, 6L represents the last Friday of the month; W: The character can only appear in the Date field, is a decoration on the leading date, and represents the most recent working day from that date. For example, 15W indicates the nearest working day from 15th of the month, if the month 15th is Saturday, match 14th Friday; if 15th is Sunday, match 16th Monday; If number 15th is Tuesday, the result is 15th Tuesday. But it must be noted that the associated match date is not able to cross the month, if you specify 1W, if 1th is Saturday, the result matches 3rd Monday, not the last day of last month. The W string can only specify a single date and cannot specify a date range; LW combination: LW can be combined in a date field, which means the last working day of the month; pound sign (#): This character can only be used in the day of the week, indicating a weekday of the month. If 6#3 represents the third Friday of the month (6 for Friday, #3表示当前的第三个), and 4#5 represents the fifth Wednesday of the month, assuming that the month does not have fifth Wednesday, ignoring does not trigger; C: The character is used only in the date and week fields, meaning "Calendar". It means the date that the schedule is associated with, and if the date is not associated, it is equivalent to all dates in the calendar. For example 5C in a Date field is equivalent to the first day of the calendar after 5th. 1C is equivalent to the first day of Sunday after the week field. Cron expressions are insensitive to the case of special characters, and are not sensitive to the case of the abbreviated English-language representation of the week. 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), wildcard. Because the "date in the month" and "date in the week" are mutually exclusive, one of the two elements must be set. 0 0 10,14,16 * *?   Daily 10 o'clock in the morning, 2 o'clock in the afternoon, 4 points 0 0/30 9-17 * *? Nine to five working hours every half hour 0 0 12? * WED 0 0 12 * per Wednesday noon 12 O'Clock "0 15 10 per day at 12 o'clock noon?" * * "10:15 per day" 0 15 10 * * "10:15" 0 15 10 * *? * "Trigger every day 10:15" 0 15 10 * *? 2005 "2005 Years a day 10:15 trigger" 0 * 14 * *? "Every 1 minutes from 2 o'clock in the afternoon to 2:59" 0 0/5 14 * *? "5 0 14,18 * per 0/5 minutes during daily 2 o'clock in the afternoon to 2:55 "0 0-5 14 * Every 5 minutes from 2 o'clock in the afternoon to 2:55 daily and 6 o'clock in the afternoon to 6:55" 1 10,44 0 per 14 minutes during daily 2 o'clock in the afternoon to 2:05? 3 WED "0 15 10 in Wednesday 2:10 and 2:44 per year in March"? * Mon-fri "from Monday to Friday 10:15" 0 15 10 15 *? "15th 10:15" 0 0 L *? "The last day of the month is triggered by 10:15" 15, 10? * 6L "last 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 "third Friday 10:15 per month" 0 5 2 1 8? * "August 1 2:5 per year" trigger some sub-expressions can contain ranges or lists such as: Sub-expressions (days (weeks)) can be "Mon-fri", "Mon,wed,fri", "Mon-wed,sat" "*" characters represent all possible values therefore, "*" In the subexpression (month) represents the meaning of each month, "*" in the subexpression (Day (week)) that represents the day of the Week "/" character is used to specify the increment of the numeric value for example: "0/15" in sub-expressions (minutes) means starting from No. 0 minute, every 15 minutes in subexpression (minutes)"3/20" means that from the 3rd minute onwards, every 20 minutes (it is the same as "3,23,43") meaning "? "Character is used only for days (months) and days (weeks) two sub-expressions, indicating that no worth 2 sub-expressions are specified after one of the values has been specified, in order to avoid a conflict, the value of another subexpression needs to be set to"? The "L" character is used only for days (months) and days (weeks) of two sub-expressions, which is the abbreviation of the word "last" but it has a different meaning in two sub-expressions. In the day (month) subexpression, "L" means the last day of one months in the day (week) Self-expression, "L" represents the last day of one weeks, that is, if the SAT has specific content before "L", it has other meanings, for example: "6L" means the 6th day of the month, "Fril" The last Friday of the month. Note: When using the "L" parameter, do not specify a list or range, as this can cause problems

Spring timer, timed execution (quartz)

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.