Spring's timing tasks

Spring configuration file

xmlns add the following:
xmlns:task=”http://www.springframework.org/schema/task”
and then add the following to xsi:schemaLocation:
http://www.springframework.org/schema/task http:/ /www.springframework.org/schema/task/spring-task.xsd

Timing task driver:

<!-- spring的定时器配置  -->
<task:annotation-driven/>

Scan of my spring:

<context:component-scan base-package="com.yj" >
    <context:exclude-filter type="annotation"     expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

There are two methods below: (1, annotation. 2, xml configuration)

1. Annotation

task class

package com.yj.conn;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TextJob {

    @Scheduled(cron="0/5 * * * * ? ")
    public void text() {
        System.out.println("halou");
    }
}

2, xml placement

Spring configuration file added

  <bean id="textJob" class="com.yj.conn.TextJob"></bean>
    <task:scheduled-tasks>
         <!-- 这里是测试时间,每隔20秒执行一次 -->
        <task:scheduled ref="textJob" method="text" cron=" 0/5 * * * * ? "/>
    </task:scheduled-tasks>

task class

package com.yj.conn;

import org.springframework.stereotype.Component;

@Component
public class TextJob {

    public void text() {
        System.out.println("halou");
    }
}

Timer's quartz CronExpression expression

A cron expression has at least 6 (and possibly 7) time elements separated by spaces.
The order is
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 in your month)
5. Month (0~11)
6. Day (week) (1~7 1=SUN or SUN, MON, TUE, WED, THU, FRI, SAT)
7. Year (1970-2099)
Each element can be a value (eg 6), a continuous interval (9-12), an interval (8-18/4) (/ means every 4 hours), a list (1,3,5), wildcards.
Since the two elements of "day of the month" and "day of the week" are mutually exclusive, it is necessary to set one of them

Example:
0 0 10, 14, 16 * * ? Every day at 10am, 2pm, 4pm
0 0/30 9-17 * * ??? Every half hour during 9 to 5 working hours
0 0 12 ? * WED means every Wednesday at 12 noon
Some sub-expressions can contain some ranges or lists
For example: sub-expressions (day (week)) can be "MON-FRI", "MON, WED, FRI", "MON-WED, SAT"
The "*" character represents all possible values.
Therefore , " " in the subexpression (month) represents the meaning of each month, and " " in the subexpression (day (week)) represents each day of the week
"/" Characters used to specify increments of values
​​For example : "0/15" in the subexpression (minutes) means every 15 minutes starting from the 0th minute;
"3/20" in the subexpression (minutes) means Every 20 minutes (it has the same meaning as "3, 23, 43") starting from the 3rd minute

The "?" character is only used for the day (month) and day (week) sub-expressions, indicating that no value is specified.
When one of the two sub-expressions is specified with a value, in order to avoid conflicts, the other sub-expression needs to be replaced. The value of the formula is set to "?"

The "L" character is only used in day (month) and day (week) subexpressions, it is an abbreviation for the word "last"
but its meaning is different in the two subexpressions.
In the day (month) subexpression, "L" represents the last day of the month, and
in the day (week) subexpression, "L" represents the last day of the week, that is,
if there is a SAT before "L" The specific content, it has other meanings
For example : "6L" means the 6th last day of the month, "FRIL" means the last Friday of the month
Note: When using the "L" parameter, do not specify a list or range , as this will cause problems

The full format of CronTrigger configuration is: [Seconds] [Minutes] [Hours] [Days] [Months] [Weeks] [Years]
Serial Number Description Required Values ​​Allowed Wildcards Allowed
1 second is 0-59 , - * /
2 Minutes are 0-59, -*/
3 hours are 0-23, -*/
4 days are 1-31, -*?/LW
May is 1-12 or JAN-DEC, -*/
6 weeks is 1- 7 or SUN-SAT , - * ? / LW
7 years no empty or 1970-2099 , - * /

Wildcard description:

* means all values. For example: setting "*" on the field of minutes means that it will be triggered every minute.
? means no value is specified. The use case is that you don't need to care about the value of the current setting of this field. For example: to trigger an operation on the 10th of each month, but do not care about the day of the week, so the field that needs the position of the week is set to "?" Specifically set to 0 0 0 10 * ?
- to indicate the interval. For example, setting "10-12" on the hour means that it will be triggered at 10, 11, and 12 o'clock.
, means specifying multiple values, such as setting "MON,WED,FRI" on the week field for Monday, Wednesday and Friday triggering
/for incremental triggering. For example, setting "5/15" on the second means that it starts from 5 seconds and triggers every 15 seconds (5, 20, 35, 50). Set '1/3' on the month field to start on the 1st of each month and trigger every three days.
L means the last. On the day field setting, it means the last day of the current month (according to the current month, if it is February, it will also depend on whether it is a leap year [leap]), and on the week field it means Saturday, which is equivalent to "7" or "SAT". If you add a number before "L", it means the last of the data. For example, setting the format "6L" on the week field means "the last Friday of the month"
W means the nearest working day (Monday to Friday) from the specified date. For example, setting "15W" on the day field means Triggered on the working day closest to the 15th of each month. If the 15th happens to be Saturday, it will trigger on the nearest Friday (14th), if the 15th is a weekday, it will trigger on the nearest next Monday (16th). If the 15th happens to be on a working day (Monday to Weekly) 5), it will be triggered on that day. If the specified format is "1W", it means that it will be triggered on the nearest working day after the 1st of each month. If the 1st falls on a Saturday, it will trigger on the following Monday, the 3rd. (Note, only specific numbers can be set before "W", and the interval "-" is not allowed).
# Serial number (indicates the day of the month), for example, setting "6#3" on the week field means the third Saturday of each month. Note that if "#5" is specified, there is no week in the fifth week Sixth, the configuration will not be triggered (it's perfect for Mother's Day and Father's Day);

Tip:
'L' and 'W' can be used in combination. If "LW" is set on the day field, it means that it will be triggered on the last working day of the month;
the setting of the week field is case-insensitive if English letters are used, that is, MON is the same as mon;

Common examples:

0 0 12 * * ? Every day at 12:00
0 15 10 ? * * Every day at 10:15
0 15 10 * * ? Every day at 10:15
0 15 10 * * ? * Every day at 10:15
0 15 10 * * ? 2005 2005 every day at 10:15 triggers
0 * 14 * * ? every day from 2:00 to 2:59 pm every minute to trigger
0 0/5 14 * * ? every day from 2:00 to 2:59 pm ( Start on the hour, trigger every 5 minutes)
0 0/5 14,18 * * ? Every afternoon from 2:00 to 2:59, 18:00 to 18:59 (start on the hour, trigger every 5 minutes)
0 0-5 14 * * ?
0 10,44 14 ? 3 WED every Wednesday at 2:10 and 2:44 pm every Wednesday from 2:00 pm to 2:05 pm
? * MON-FRI Triggered at 10:15 am every day from Monday to Friday
0 15 10 15 * ? Triggered at 10:15 am on the 15th of each month
0 15 10 L * ? Triggered at 10:15 am on the last day of each month
0 15 10 ? * 6L Triggered at 10:15 on Friday of the last week of every month
0 15 10 ? * 6L 2002-2005 Triggered at 10:15 on Friday of the last week of every month from 2002 to 2005
0 15 10 ? * 6# 3 Triggered on Friday of the third week of each month
0 0 12 1/5 * ? Triggered every 5 days from the first noon of each month
0 11 11 11 11 ? Every year on November 11th at 11:11 trigger (Singles Day)

A few points to note:

1. Spring's @Scheduled annotation needs to be written on the implementation,

2. The task method of the timer cannot have a return value (if there is a return value, spring will tell you that there is an error when initializing, you need to set a certain value of proxytargetclass to true, go to Baidu google for details)

3. The implementation class must have component annotation @Component

4. The following article link is the corn expression, you can refer to it

http://blog.csdn.NET/u010448530/article/details/52209119


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325835852&siteId=291194637