spring two ways to achieve timing tasks

One way: Notes

1, added task in the spring configuration file namespace

1
2
3
xmlns:task="http://www.springframework.org/schema/task" 
http:
http://www.springframework.org/schema/task/spring-task-3.0.xsd

2, configure the scan notes

1
2
<task:annotation-driven />
<context:component-scan base-package="com.glaway.offline.JobDemo" />

3, to perform tasks like writing

1
2
3
4
5
6
7
8
9
10
11
package com.glaway.offline.JobDemo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


public class { @Scheduled (the cron = "* * * * 0/10?" ) // 10 second intervals perform public void executeJob () { System.out.println ( "scheduled task Demo" ); } }





4, Attention:

  1. @Scheduled need to write notes on implementation
  2. Methods task Timer not have a return value
  3. Components must be achieved on the class notes @Component

Second way: Profile

1, the same task of adding the spring configuration file namespace

1
2
3
xmlns:task="http://www.springframework.org/schema/task" 
http:
http://www.springframework.org/schema/task/spring-task-3.0.xsd

2, write a configuration file

1
2
3
4
5
6
7
8
<! - driving the timing annotation -> 
: <Annotation Task-Driven />
<! - the timing task class, which is defined as a the bean ->
<the bean ID = "timeJobDemo" class = "com.glaway .offline.JobDemo.TimeJobDemo "> </ the bean>
<- tag by task definition timer ->!
<task: Scheduled-tasks>
<TAS large column  are two ways to achieve timing spring task k: scheduled ref = "timeJobDemo" Method, = "executeJob" cron = "0/10 * * * *?" />
</ Task: Scheduled-Tasks>

3, to perform tasks like writing

1
2
3
4
5
6
7
package com.glaway.offline.JobDemo;

public class { public void executeJob () { System.out.println ( "scheduled task Demo" ); } }




Extension: timer time setting

Such as: "0/10 ?"

CronTrigger fully configured format: [seconds] [minutes] [h] [day] [month] [weeks] [years]

No. Explanation Required Which allowed the value of Allow wildcard
1 second Yes 0-59 , - * /
2 Minute Yes 0-59 , - * /
3 Time Yes 0-23 , - * /
4 day Yes 1-31 , - * ? / L W
5 month Yes 1-12 or JAN-DEC , - * /
6 week Yes 1-7 or SUN-SAT , - * ? / L W
7 year no empty or 1970-2099 , - * /

Wildcard Description:

    • Represent all the values ​​such as: is provided on the sub-fields "*" indicates trigger every minute.
  • ? Said they did not specify the value. Scene used to not care about the value of the current setting of this field.

  • For example: To trigger on the 10th of each month of operation, but is not concerned about a few weeks, so it is necessary that the field is set for the specific peripheral position is set to 00010 * "?"?

    • Representing the interval. For example, set up "10-12" on the hour, 10, 11, represents the point will be triggered.
  • Indicating specify multiple values, such as setting "MON, WED, FRI" in the week field said on Monday, Wednesday and Friday trigger

  • / For incrementing trigger. As set "5/15" above represents the second for 5 seconds, every 15 seconds by a trigger (5,20,35,50). Set in the month field '1/3' Start No. 1 a month, once every three days triggered shown.

  • L represents the final meaning. On the field, set the day, represents the last day (based on the current month, if February is also based on whether it is a leap year [leap]) the month, expressed Saturday, the equivalent of "7" or "SAT" in the week field. If the "L" digitally before, indicates the last of the data. For example, setting this format "6L" in the week field, it means "the last Friday of the month."

  • W represents the example set "15W" on the daily field closest to the date specified weekday (Monday to Friday), represents the 15th of each month from recent days that trigger. If the 15th happens to be Saturday, you find the nearest Friday (14) is triggered, if the number 15 is the weekend, then look for the nearest next Monday (16) is triggered. If the number in just 15 working days (Monday to Sunday e), then triggered in the day. If you specify the format of "1W", it said 1 month later recent days triggered. If Saturday is the No. 1, No. 3 will be triggered Monday. (Note, "W" can only be set before the specific numbers are not allowed interval "-").

  • 'L' and 'W' may be used in combination. If set to "LW" in the Japanese field, then triggered the last business day of the month;

  • Well serial number (the first few weeks of every month a few), for example on the field Week "6 # 3". Note that if you specify "# 5", not just the fifth week of the third week of the month Saturday Sixth, the configuration is not triggered (with mother's Day and father's Day a perfect fit);

  • Set circumferential field, the use of letters is not case sensitive, i.e. the MON mon same;

Guess you like

Origin www.cnblogs.com/lijianming180/p/12037975.html