Spring timed Task Time format cronexpression set __spring timed task

Source: Internet
Author: User

Org.springframework.scheduling.quartz.CronTriggerBean allows you to more precisely control the running time of a task, and simply set its Cronexpression property.
A cronexpression expression has at least 6 (or possibly 7) time elements separated by a space. From left to right, these elements are defined as follows:
1. Seconds (0–59)
2. Min (0–59)
3. Hours (0–23)
4. The date in the month (1–31)
5. Month (1–12 or Jan–dec)
6. Date in the week (1–7 or Sun–sat)
7. Year (1970–2099)
0 0 10,14,16 * * ?  daily 10 o'clock in the morning, 2 o'clock in the afternoon and 4 o'clock in the afternoon
0 0,15,30,45  * 1-10 * ? 
30 0 0 1 1 ? 2012  every 15 minutes before the first 10 days of each month
0 0 8-5 ? * mon-fri  working hours  

per working day at 30 seconds after midnight on January 1, 2012

The available values for each time are as follows:
Seconds 0-59, –*/
Minute 0-59, –*/
Hour 0-23, –*/
Day 1-31, –*? /L W C
Month 1-12 or JAN-DEC, –*/
Weeks 1-7 or Sun-sat, –*? /L C #
Year (optional field) empty, 1970-2099, –*/

The available values are analyzed in detail as follows:

"*"--characters can be used for all fields, and "*" in the "in" field means "every minute".

“?” --characters can be used in the days and weeks fields. It is used to specify the ' ambiguous value '. This is used when you need to specify one of the values in these two fields, not the other. In the following example, you can see what it means.

"-"--the character is used to specify a range of values, for example, in the "Hour" field, set to "10-12″ 10 to 12 points."

","--the character specifies a number of values. For example, in the "Weeks" field set to "Mon,wed,fri" means "The days Monday, Wednesday, and Friday."

"/"--characters are used to specify the increment of a value. For example, in the "Seconds" field, set to "0/15″" No. 0, 15, 30, and 45 seconds. and "5/15″" means 5th, 20, 35, and 50″. The '/' plus ' * ' character is equivalent to specifying starting from 0 seconds. Each field has a series of values that can start or end. For the seconds and minutes fields, the range is 0 to 59, 0 to 23 for the hour field, 0 to 31 for the day field, and 1 to 12 for the month field. The "/" field is only a value that helps you to start "nth" within the range of allowable values.

"L"--characters can be used in two fields, days and weeks. It is the abbreviation for "last", but there are different meanings in these two fields. For example, "L" in the "Day" field means "last day in one months"-for January is number 31st for February is 28th (not a leap year). In the "Weeks" field, it simply means "7″or" SAT, but if used in the "Weeks" field after a number, it means "the last one weeks of the month X"-for example, "6L" means "the last Friday of the month". When using the ' L ' option, it is important to specify a list or range, otherwise you will be confused by the results.

"W"--Available for the day field. Use to specify the most recent weekday (Monday to Friday) for the given date of the calendar. For example, you set the "Day" field to "15W", meaning: "From the month of 15th, the most recent working day." So if number 15th is Saturday, the trigger will be called at number 14th, Friday. If number 15th is Sunday, the trigger will be triggered at number 16th, which is Monday. If the number 15th is Tuesday, then the day will be triggered. However, if you set the "Day" field to "1W" and the first number is Saturday, the trigger will be triggered in the next Monday, the month 3rd, because it will not cross the range boundary of the month's value. The ' W ' character can only be used when the value of the ' Day ' field is a single day instead of a series of values.

"L" and "W" can be combined for the "Day" field to be represented as ' LW ', meaning ' last weekday of the month '.

"#"--characters can be used in the weeks field. The character represents "Weeks X" of the month, such as "6#3″ represents the third Friday of the month (6 means Friday and" #3 ″ the third of the month). Another example: "2#1″= said the first Monday of the month and" 4#5″= the fifth Wednesday of the month. Note that if you specify "#5 ″ there is no fifth" week X "in the month, the month is not triggered.

"C"--characters can be used for the days and weeks fields, which are abbreviations for calendar. It is represented as a value computed, if any, based on the relevant calendar. If there is no calendar associated with it, it is equivalent to including all calendars. The day field value of "5C" indicates "after the first or 5th numbers in the Calendar", and the "Week" field value "1C" means "the first day in the calendar or after Sunday."

The legal characters for the month and week fields are not case sensitive.


Some examples:

About cronexpression expression: field allowed value special character seconds 0-59,-*/min 0-59,-*/hour 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,-*/Expression meaning "0 0 12 * *" Every day 12 o'clock Noon trigger 0 15 10? * * "0 15 10 * * *" every day 10:15 trigger "0 15 10 * *? * "0 15 10 * * per day 10:15 trigger" 2005 "2005 a Day 10:15 trigger" 0 * 14 * *? "1 0 0/5 * * * *" in every 14 minutes from 2 o'clock in the afternoon to 2:59 every day, "5 0 14,18 * "0 0-5 14 * * In every 5 minutes from 2 o'clock in the afternoon to 2:55 and 6 o'clock in the afternoon to 6:55 every day," 1 10,44 0 in every 14 minutes of the Daily 2 o'clock in the afternoon to 2:05 period? 3 WED "Wednesday 2:10 and 2:44 triggers per year of March" 0 15 10? * Mon-fri "Monday to Friday 10:15 trigger" 0 15 10 15 *? "15th 10:15 every month" 0 L *? "10:15 trigger for the last day of the month 0 15 10? * 6L "The last Friday 10:15 trigger of the month" 0 15 10? * 6L 2002-2005 "2002 to 2005 month of the last Friday 10:15 trigger" 0 15 10? * 6#3 "The third Friday 10:15 per month is triggered every morning at 6 0 6 * * * * * Every two hours 0 */2 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * One to three on the morning 11, 0 11 4 * 1-3 January 1, morning 4 0 4, 1 1 *


Quartz dynamically set cronexpression in spring


What is a dynamic timing task: It is generated by the customer, the server only knows what task to perform, but the timing of the task is uncertain (is made by the customer).

It's not always possible to modify the configuration file. Add a trigger for each scheduled task, even if you allow the customer to modify the profile, but always need to restart the Web service Ah, the dynamic timing of the quartz in spring is studied, and <bean id= "Crontrigger "class=" Org.springframework.scheduling.quartz.CronTriggerBean ">

<property name= "Jobdetail" ref= "Schedulerjobdetail"/>

<property name= "Cronexpression" >

<VALUE>0/10 * * * *?</value>

</property>

Cronexpression is the key, if you can dynamically set the value of cronexpression, it is said that if we can directly call the Crontriggerbean to set the Cronexpression method, you can successfully solve the problem.

Familiar with 1 of friends can skip the look, the following 2, 3 is the dynamic timing task of the specific implementation.

1. Quartz Simple configuration in spring

Spring configuration file:

<bean id= "Schedulerjobdetail" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">

<property name= "TargetObject" ref= "Scheduleinfoaction"/>

<property name= "Targetmethod" value= "Simplejobtest"/>

<property name= "Concurrent" value= "false"/>

</bean>

<bean id= "Crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >

<property name= "Jobdetail" ref= "Schedulerjobdetail"/>

<property name= "cronexpression" >

<VALUE>0/10 * * * *?</value>

</property>

</bean>

<bean id= "Schedulerfactory" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >

<property name= "Triggers" >

<list>

<ref local= "Crontrigger"/>

</list>

</property>

</bean>

In the configuration above, set the

①targetmethod: Specifies that the Simplejobtest () method in the scheduleinfoaction should be timed to execute

②concurrent: For the same jobdetail, when multiple trigger are specified, it is likely that the second job will begin before the first job completes. Specifies that concurrent is set to false, multiple jobs will not run concurrently, and the second job will not start before the first job completes.

cronexpression: 0/10 * * * * * For every 10 seconds, refer to the schedule.

④triggers: Multiple triggers can be placed in the list by adding additional ref elements.

The Simplejobtest () method in Scheduleinfoaction

Note: This method has no parameters and if Scheduleinfoaction has two methods Simplejobtest () and simplejobtest (String argument), spring will only execute the parameterless simplejobtest ().

public void Simplejobtest () {

Log.warn ("Uh oh, Job is scheduled!" + "' Success ...");

}

2. Quartz Dynamic Setting of Crontrigger method in spring

Spring configuration file:

<bean id= "scheduleinfoaction" class= "Com.lively.happyoa.jobs.webapp.action.ScheduleInfoAction" >

<property name= "Scheduler" ref= "Schedulerfactory"/>

<property name= "scheduleinfomanager" ref= "Scheduleinfomanager"/>

</bean>

<bean id= "Schedulerjobdetail" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">

<property name= "TargetObject" ref= "Scheduleinfoaction"/>

<property name= "Targetmethod" value= "reschedulejob"/>

<property name= "Concurrent" value= "false"/>

</bean>

<bean id= "crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >

<property name= "Jobdetail" ref= "Schedulerjobdetail"/>

<property name= "cronexpression" >

<VALUE>0/10 * * * *?</value>

</property>

</bean>

<bean id= "Schedulerfactory" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >

<property name= "Triggers" >

<list>

<ref local= "Crontrigger"/>

</list>

</property>

</bean>

reschedulejob () method and related methods in Scheduleinfoaction

reschedulejob(): Read the database, get the custom timer scheduling time

private void Reschedulejob() throws Schedulerexception, ParseException {

The runtime can get trigger by dynamic injection of scheduler

Crontriggerbean trigger = (Crontriggerbean) Scheduler.gettrigger (

"crontrigger", Scheduler.default_group);

String dbcronexpression = Getcronexpressionfromdb ();

String originconexpression = Trigger.getcronexpression ();

Determines whether the task time (originconexpression) is equal in the task time (dbcronexpression) obtained from DB and now in the quartz thread

If equal, it means that the user did not reset the task time in the database, which does not require reschedulejob

if (!originconexpression.equalsignorecase (dbcronexpression)) {

Trigger.setcronexpression (dbcronexpression);

Scheduler.reschedulejob ("crontrigger", scheduler.default_group, Trigger);

}

The following is a specific job content that you can set yourself

Executejobdetail ();

}

②getcronexpressionfromdb (): Get Dbcronexpression's specific code from the database, because of the use of Scheduleinfomanager, so to define the appropriate setter method

Private String Getcronexpressionfromdb () {

String sql= "from Scheduleinfo Scheduleinfo where 1=1";

sql=sql+ "and scheduleinfo.infoid = '" + "1" + "'";

List schedulelist = scheduleinfomanager.queryscheduleinlistbysql (sql);

Scheduleinfo Scheduleinfo = (scheduleinfo) schedulelist.get (0);

String dbcronexpression = Scheduleinfo.getcronexpression ();

return dbcronexpression;

}

③ configured the appropriate property (Scheduler/Scheduleinfomanager) in the spring configuration file scheduleinfoaction to set the setter method for

Private Scheduler Scheduler;

Setting value injection, passing the setter method to an instance of the callee scheduler

public void Setscheduler(Scheduler Scheduler) {

This.scheduler = Scheduler;

}

Private Scheduleinfomanager Scheduleinfomanager;

Setting value injection, passing the setter method to an instance of the callee Scheduleinfomanager

public void Setscheduleinfomanager(Scheduleinfomanager scheduleinfomanager) {

This.scheduleinfomanager = Scheduleinfomanager;

}

3. Quartz Dynamic Setting of Crontrigger method in spring two

In the 2 above we can see that although the reschedulejob can be done dynamically, we still need to set up a cronexpression if we try to get rid of the spring configuration

<property name= "cronexpression" >

<VALUE>0/10 * * * *?</value>

</property>

The container (such as Tomcat) will be started with an error.

In practice, we want Tomcat to start directly to read the database, get the appropriate dbcronexpression, and then execute a job, and do not want to configure the initial cronexpression , observe the following Crontriggerbean, considering that cronexpression needs to be initialized, if you set a class Initializingcrontrigger inherit Crontriggerbean, Then do some initialization work (set cronexpression) for reading DB in this class, and the problem will be solved.

Spring configuration file:

<bean id= "scheduleinfoaction" class= "Com.lively.happyoa.jobs.webapp.action.ScheduleInfoAction" >

<property name= "Scheduler" ref= "Schedulerfactory"/>

<property name= "scheduleinfomanager" ref= "Scheduleinfomanager"/>

</bean>

<bean id= "Schedulerjobdetail" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">

<property name= "TargetObject" ref= "Scheduleinfoaction"/>

<property name= "Targetmethod" value= "reschedulejob"/>

<property name= "Concurrent" value= "false"/>

</bean>

<bean id= "crontrigger" class= "Com.lively.happyoa.jobs.webapp.action.ScheduleInfoAction". Initializingcrontrigger">

<property name= "Jobdetail" ref= "Schedulerjobdetail"/>

<!--<property name= "cronexpression" >

<VALUE>0/10 * * * *?</value>

</property>-->

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.