Spring Scheduled Tasks (reproduced)

Source: Internet
Author: User
Tags dateformat

Using Spring's timing task in SPRINGMVC is very simple, as follows:

(i) Adding a task namespace to the XML

[HTML]View PlainCopy 
    1. xmlns:task="Http://www.springframework.org/schema/task"
    2. Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd

(ii) Enable annotation-driven timing tasks

[HTML]View PlainCopy 
    1. <task:annotation-driven scheduler="Myscheduler"/>

(iii) thread pool to configure timed tasks

It is recommended to configure the thread pool, which will cause problems if you do not configure multiple tasks. A single-threaded issue is explained in more detail later.

[HTML]View PlainCopy  
    1. <task:scheduler id="Myscheduler" pool-size="5"/>

(d) To write our scheduled tasks

@Scheduled annotations are timed tasks, the timing of write execution in cron expressions

[Java]View PlainCopy  
  1. Package Com.mvc.task.impl;
  2. Import Java.text.DateFormat;
  3. Import Java.text.SimpleDateFormat;
  4. Import Java.util.concurrent.TimeUnit;
  5. Import Org.joda.time.DateTime;
  6. Import org.springframework.scheduling.annotation.Scheduled;
  7. Import org.springframework.stereotype.Component;
  8. Import Com.mvc.task.IATask;
  9. @Component
  10. Public class Atask implements iatask{
  11. @Scheduled (cron="0/10 * * * *?") //Once every 10 seconds
  12. @Override
  13. public void Atask () {
  14. try {
  15. TimeUnit.SECONDS.sleep (20);
  16. } catch (Interruptedexception e) {
  17. E.printstacktrace ();
  18. }
  19. DateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
  20. System.out.println (Sdf.format (DateTime.Now () toDate ()) +"*********a task performs an entry test every 10 seconds");
  21. }
  22. }
[Java]View PlainCopy  
  1. Package Com.mvc.task.impl;
  2. Import Java.text.DateFormat;
  3. Import Java.text.SimpleDateFormat;
  4. Import Org.joda.time.DateTime;
  5. Import org.springframework.scheduling.annotation.Scheduled;
  6. Import org.springframework.stereotype.Component;
  7. Import Com.mvc.task.IBTask;
  8. @Component
  9. Public class Btask implements ibtask{
  10. @Scheduled (cron="0/5 * * * *?") //Once every 5 seconds
  11. @Override
  12. public void Btask () {
  13. DateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
  14. System.out.println (Sdf.format (DateTime.Now () toDate ()) +"*********b task performs an entry test every 5 Seconds");
  15. }
  16. }

Spring's scheduled tasks are single-threaded by default , with multiple tasks having a problem (the B task is delayed by 20S execution because the a task executes up to 20S), as shown in:



When we configure the thread pool and look at the results (multi-threaded, the B task will not be delayed because a task is executed to 20S):


The cron expression is detailed:

A cron expression has at least 6 (or possibly 7) time elements that have a space separation.

In order of
1 seconds (0~59)
2 minutes (0~59)
3 hours (0~23)
4 Days (0~31)
May (0~11)
6 weeks (1~7 1=sun or Sun,mon,tue,wed,thu,fri,sat)
7. Year (1970-2099)
Each of these elements can be a value (such as 6), a continuous interval (9-12), a time interval (8-18/4) (/= every 4 hours), a list (1,3,5), and a wildcard character. Because the "date in the month" and "date in the week" are mutually exclusive, one of the two elements must be set.
0 0 10,14,16 * *? 10 o'clock in the morning, 2 o'clock in the afternoon, 4 O ' Day
0 0/30 9-17 * *? Every half hour for nine to five working hours
0 0 12? * WED means every Wednesday noon 12 o'clock
"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
Some sub-expressions can contain ranges or lists
For example: subexpression (Day (week)) can be "Mon-fri", "Mon,wed,fri", "Mon-wed,sat"
The "*" character represents all possible values
The "/" character is used to specify the increment of the numeric value
For example: "0/15" in sub-expressions (minutes) means starting from the No. 0 minute, every 15 minutes
"3/20" in the sub-expression (minutes) means that every 20 minutes (it is the same as "3,23,43") starting from the 3rd minute
“? "Character is used only for days (months) and days (weeks) of two sub-expressions, indicating that no value is specified
When one of the 2 sub-expressions is assigned a value, in order to avoid a conflict, you need to set the value of another subexpression to "? ”
The "L" character is used only for days (months) and days (weeks) of two sub-expressions, which is the abbreviation for the word "last"
If there is specific content before "L", it has other meanings. For example: "6L" means the 6th day of the month.
Note: When using the "L" parameter, do not specify a list or range, as this can cause problems
The W character represents weekdays (MON-FRI) and can only be used in the day domain. It is used to specify the most recent weekday from the specified day. Most of the business process is based on the work week, so the W character may be very important.
For example, 15W in the daily field means "the most recent weekday of the month 15th." "If number 15th is Saturday, then trigger will trigger at 14th (Friday) because Thursday is closer to 15th than Monday.
C: means "Calendar". It means the date that the schedule is associated with, and if the date is not associated, it is equivalent to all dates in the calendar. For example 5C in a Date field is equivalent to the first day of the calendar after 5th. 1C is equivalent to the first day of Sunday after the week field.
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,-*/

Spring Scheduled Tasks (reproduced)

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.