The configuration of spring timed tasks uses

Source: Internet
Author: User

Spring's scheduled task configuration is divided into three steps:
1. Define Tasks
2. Task execution Policy configuration
3. Start the Task

1. Define Tasks
<!--methods to be executed on a timed basis - <BeanID= "Testtaskjob"class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> < Propertyname= "TargetObject"> <!--specifies the class of methods to be executed on a timed basis, defining a timed task as a bean - <refBean= "Testtask" /> </ Property> < Propertyname= "Targetmethod"> <!--Specify a method for timed execution - <value>Execute</value> </ Property> < Propertyname= "Concurrent"> <!--Specifies that the target is encapsulated as a stateful task, a stateful task cannot be executed concurrently, and a stateless task can execute concurrently - <value>False</value> </ Property> </Bean> 
2. Task Execution Policy configuration 

(1) Specify the recurrence interval for the scheduled task

<!--Scheduling Time Settings - <BeanID= "Testtaskjobtrigger"class= "Org.springframework.scheduling.quartz.SimpleTriggerBean"> < Propertyname= "Jobdetail"> <refBean= "Testtaskjob" /> </ Property> <!--delay Start time, Unit Ms - < Propertyname= "Startdelay"value= "60000"></ Property> <!--Repeat interval time, unit Ms - < Propertyname= "Repeatinterval"value= "60000"> </ Property> </Bean> 

(2) Tasks performed on a periodic basis

 <!--Define trigger Time -     <BeanID= "Dotime"class= "Org.springframework.scheduling.quartz.CronTriggerBean">         < Propertyname= "Jobdetail">             <refBean= "Testtaskjob" />         </ Property>         <!--cron expression, which is executed 10 point 42 per day -         < Propertyname= "Cronexpression">             <value>0 42 10 * *?</value>         </ Property>     </Bean> 
3. Start the task
<!--Start Work - <BeanLazy-init= "false"class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean"> < Propertyname= "Triggers"> <List> <!--Multiple timer task triggers are added here, corresponding to the Bean ID for step 2nd - <refLocal= "Testtaskjobtrigger" /> </List> </ Property> </Bean> 

At this point, when the system starts, it can load the scheduled task and execute it according to the specified timing policy.

For one-time scheduled tasks, there are generally the following forms: (1) using the Spring init method, (2) deciding whether to load when used, and ensuring that it is executed only once.

Basic use of cron expressions:

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,-*/
Meaning of an expression
"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
Every morning at 6.
0 6 * * *
Every two hours
0 */2 * * *
Every two hours between 11 o'clock and 8 in the morning, eight in the morning.
0 23-7/2,8 * * *
Every month, number 4th and Monday to Sunday, three a.m., 11 .
0 4 * 1-3
January 1 morning, 4.
0 4 1 1 *

The configuration of spring timed tasks uses

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.