Spring timed task Scheduling "detailed with cron parameters"

Source: Internet
Author: User
Tags aop set time

There is a task within spring that is a set time automatic task scheduling with spring itself

Task is convenient to use, but he can do less than quartz.

You can use annotations and configuration in two ways, configured in the following ways

Introducing spring to the beginning of appcation.xml

<beans xmlns= "Http://www.springframework.org/schema/beans"
	xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
	xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "
	xmlns:context=" http:// Www.springframework.org/schema/context "
	xmlns:task=" Http://www.springframework.org/schema/task "
	xsi: schemalocation= "
		Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.0.xsd
		HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.0.xsd
		Http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-4.0.xsd
		Http://www.springframework.org/schema/task http://www.springframework.org/ Schema/task/spring-task-4.0.xsd "
	default-lazy-init=" true >
<!--register Bean-->
<bean id= "Voicefileclearjob" class= "Com.zjr.modules.boss.job.VoiceFileClearJob"/> <bean id=
" Versionlistenjob "class=" Com.zjr.modules.boss.job.VersionListenJob "/> <bean id=" statjob "class="
Com.zjr.modules.opstat.job.StatJob "/>
<!--Open Task Scheduler (timer)-->
<task:scheduled-tasks>
	<task:scheduled ref= "Voicefileclearjob" method= "execute" initial-delay= "5000" fixed-delay= "3600000"/>
	<task:scheduled ref= "Versionlistenjob" method= "execute" initial-delay= "5000" fixed-delay= "5000"/> <task:scheduled ref= "statjob" method= "Statlgj
	" cron= "0 59 23 * *?" />
	<task:scheduled ref= "Statjob" method= "Statbadnameandqq" cron= "23 28 20 * *?" />
</task:scheduled-tasks>

The first task indicates that the Execute method in the Voicefileclearjob class is called after the program starts 5s, and then the Execute is invoked every one hours

The third task represents a daily 23:59 call to the Statlgj method in the Statjob class

Ref is a work class

Method is the way to execute in a work class

Initial-delay is the delay before the task is first invoked, in milliseconds

Fixed-delay is the delay that was called again after the last call was completed

Fixed-rate is the latency (without waiting for the last call to complete) that was called again after the previous call started

A cron is an expression that indicates when a task is scheduled. Cron expression:

A cron expression has a space-delimited time element of at least 6 (or possibly 7).
sequentially, in the order of
1. Seconds (0~59)
2. Minutes (0~59)
3. Hours (0~23)
4. Days (months) (0~31, but you need to consider the number of days of your month)
5. Month (0~11)
6. Days (weeks) (1~7 1=sun or Sun,mon,tue,wed,thu,fri,sat)
7. Year (1970-2099)


Each element can be a value (such as 6), a continuous interval (9-12), a time interval (8-18/4) (per 4 hours), a list (1,3,5), wildcard characters.because the two elements, "date in the month" and "date in the Week", are mutually exclusive, one of the 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 * *? Nine to five working hours every half hour
0 0 12? * WED says every Wednesday 12 o'clock noon
"0 0 12 * *" Every day 12 o'clock noon.
"0 15 10?" * * "Every 10:15 trigger
"0 15 10 * *" Every day 10:15 trigger
"0 15 10 * *? * "Every 10:15 trigger
"0 15 10 * *? 2005 "2005 Every day 10:15 triggers
"0 * 14 * *" is triggered every 1 minutes from 2 o'clock in the afternoon to 2:59 every day
"0 0/5 14 * *" is triggered every 5 minutes from 2 o'clock in the afternoon to 2:55 every day
"0 0/5 14,18 * * *" triggered 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
"0 0-5 14 * *" is triggered every 1 minutes from 2 o'clock in the afternoon to 2:05 every day
"0 10,44 14?" 3 WED "Every March of Wednesday, 2:10 and 2:44 triggers
"0 15 10?" * Mon-fri "from Monday to Friday 10:15 trigger
"0 15 10 15 *" Every month 15th 10:15 trigger
"0 L *?" triggered 10:15 the last day of every month
"0 15 10?" * 6L "last Friday 10:15 trigger per month
"0 15 10?" * 6L 2002-2005 "2002 to 2005 monthly last Friday 10:15 trigger
"0 15 10?" * 6#3 "The third Friday 10:15 trigger per month

Some of the subexpression can contain ranges or lists

For example: a subexpression (days (weeks)) can be "Mon-fri", "Mon,wed,fri", "Mon-wed,sat"

The "*" character represents all possible values

Therefore, "*" represents the meaning of each month in the subexpression (month), and "*" in the subexpression (Day (week)) represents every day of the week
The '/' character is used to specify the increment of the value

For example, the expression "0/15" in a subexpression (minutes) starts from the No. 0 minute, every 15 minutes

The expression "3/20" in the subexpression (minutes) is the same as the meaning of every 20 minutes (it's "3,23,43") starting at the 3rd minute.

“。 "Character is used only for days (months) and days (weeks), two subexpression, indicating unspecified value

To avoid conflicts, you need to set the value of another subexpression to "when one of the 2 subexpression is assigned a value. ”

The "L" character is used only for days (months) and days (weeks) of two subexpression, which is the abbreviation for the word "last"


But it has different meanings in the two-subexpression.


In the day (month) subexpression, "L" means the last day of the one month


In the day (week) Self expression, "L" means the last day of the one week, which is the SAT


If there is a specific content before "L", it has other meanings.


For example: "6L" means the 6th day of the month, and "Fril" means the last Friday of the month.


Note: Do not specify a list or range when using the "L" parameter, because this can cause problems

value
Field allow special characters allowed by

Seconds

0-59

, - * /

Part

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 #

Years (optional)

Leave Blank, 1970-2099

, - * /



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.