Understanding and thinking about timing execution of sping quartz

Reprinted, please indicate the original source, thank you!

I always thought I understood spring quartz, and suddenly I found myself wrong in the last few days. In April 18th, I implemented a spring quartz plan as follows:

100 */3 *?

I thought we would need to wait until 21 will perform, did not think second days is April 19th he performed, I restarted in April 20th, expected in 21, the 21 came to see today has not been carried out before, I found that I does not understand, I only know every 3 days in the morning zero minute execution, but the specific day I actually is not very clear.

The detailed usage field of the Cron expression

Special characters allowed by allowed values

  • Seconds 0-59 – *
  • 0-59, – *
  • 0-23 hours – * /
  • Date 1-31 – * * / L W C
  • Month 1-12 or JAN-DEC, – *
  • Week of 1-7 or SUN-SAT – L / C # *?
  • Year (optional) blank, 1970-2099 – /
    example: 0/5 * * * * *?: execute once every 5 seconds.
  1. The “*” character is used to specify all values. For example, “*” means “every minute” in the field of minutes”.
  2. “?” characters are used only in date fields and week fields. It is used to specify “indefinite values””. It’s useful when you need to specify something through one of these two domains. Look at the example below, and you’ll see that the two elements in the month and the date in the week should be mutually exclusive to indicate that they don’t want to set that field by setting a question mark.
  3. The “-” character is used to specify a range. For example, “10-12” means “10”, “11” and “12” in a small time domain”.
  4. “” “characters are used to specify additional values. For example, “MON, WED, FRI” means “Monday, Wednesday, Friday” in the week”.
  5. “/” characters are used to specify increments. For example, “0/15” in the second domain means 0, 15, 30 and 45 seconds per minute. “5/15” represents 5, 20, 35 and 50 of the hour in the minute domain.

The sign “*” is equal to 0 in front of “/” (*/10) in front of “/” (e.g.: 0/10). Remember an essence: each numeric field of an expression is a set of maximum and minimum values, such as the set of seconds and min fields is 0-59, the date field is 1-31, and the month domain is 1-12. The character “/” can help you take the corresponding value in each character field. Such as: “7/6” in the month domain, only when July will trigger, does not mean that every June.

  1. L is the ellipsis of ‘last’, which can be used to represent day-of-month and day-of-week domains, but differs in two fields, such as the last day of the month in the day-of-month domain. If you represent ‘7’ or ‘SAT’ in the day-of-week domain, if you add numbers to the day-of-week domain, it represents the last few days of the month, for example ‘6L’ means the last Friday of the month.
  2. The character “W” only allows the date field to appear. This character is used to specify the nearest workday on the date. For example, if you write “15W” in the date domain, say: the 15 month of the month, the most recent workday. So, if No. 15 is Saturday, the mission will trigger at number 14. If 15 is good on Sunday, the task will be triggered on Monday, that is, No. 16. If the 1W is filled in the date domain, even if the number 1 is Saturday, then the task will only happen next Monday, that is, the 3 trigger, and the latest working day specified by the “W” character is not able to cross the month. The character “W” can only be used with a single numeric value, and cannot be a number field, such as: 1-15W is wrong.

“L” and “W” can be used together in the date domain. LW represents the working week of the last week of the month.

  1. The character “#” is only allowed to appear in the week in the domain. This character is used to specify a certain day of the month. For example, “6#3” represents the third week of the month Friday (6 means Friday, and 3 represents third weeks). “2#1” means Monday in the first week of the month. “4#5” means Wednesday of fifth weeks.
  2. The character “C” is allowed to appear in the date domain and the week domain. This character depends on a specified calendar”. That is, the value of this expression depends on the result of the relevant “calendar”, and if there is no calendar Association, it is equivalent to all the included calendars”. For example, the date field is “5C” indicating the first day of the associated calendar, or the last 5 days after the first day of the month. The weekly domain is the “1C” that represents the first day of the first day of the calendar, or the first 1 days of the week, or the day after Sunday (Monday).

Expression examples

  • “0012 * *?” triggered at 12 noon every day
  • “01510 * * *” triggers at 10:15 a. m. every morning
  • “01510 * *?” triggers at 10:15 every morning
  • “01510 * * * *” triggers at 10:15 a.m. every day
  • “01510 * *? 2005” triggered at 10:15 a.m. every morning in 2005
  • “0 * 14 * * *” triggers every 1 minutes during 2 p.m. to 2:59 p.m. every day
  • “0 0/5 14 * *?” triggers every 5 minutes during the afternoon from 2 p.m. to 2:55 p.m.
  • “0 0/5 14,18 * *?” triggers every 5 minutes from 2 p.m. to 2:55 p.m. and from 6 p.m. to 6:55 p.m.
  • “0 0-514 * *?” triggers every 1 minutes during the afternoon from 2 p.m. to 2:05 p.m.
  • “0 10,44 14? 3 WED” triggered on Wednesday, March, at 2:10 p.m. and 2:44 p.m.
  • “01510 * * MON-FRI” triggered from 10:15 a.m. Monday to Friday
  • “0151015 *?” triggered at 10:15 a.m. on the 15 day of the month
  • “01510 L *?” triggered at 10:15 a.m. on the last day of the month
  • “01510 * * 6L” triggers at 10:15 a.m. on the last Friday of the month
  • “01510 * * 6L 2002-2005” triggers from 10:15 a.m. on the last Friday of 2002 to 2005 each month
  • “01510 * * 6#3” triggers at 10:15 a.m. on the third Friday of each month

Through the above discovery, 100 */3 *?

If executed in April 1st, similar reasoning, No. 4, 7, 10, 13, 16, 19, 22, 25, 28,
, because the monthly position is * monthly, then the beginning of next month is May 1st.

If the May 1st execution, similar reasoning, No. 4, No. 7, No. 10, No. 13, No. 16, No. 19, No. 22, No. 25, No. 28, No. 31,
month because the position for * per month, so next month is June 1st.

The problem should be clear, the previous understanding of the error, should be solved.

Next, we’ll try to modify the system time by writing java code, each time to the critical point to see the execution effect,
follows java code execution:

New Timer (.Schedule) (new (TimerTask) @Override public void (run) {{{Runtime.getRuntime (try).Exec ("CMD /c time 23:59:50"); //Windows catch (IOException E) {}} (e.printStackTrace); System.out.println ("= = = = =" +simpleDateFormatt.format (New) (Date));}} 50, 60000);

What if I want to have this interval of 3 days to achieve, just as the execution in May 1st, No. 4, No. 7, No. 10, No. 13, No. 16, No. 19, No. 22, No. 25, No. 28, No. 31, because the moon’s position * said a month, so next month is June 1st. What should you do if you want the next time to be June 3rd instead of June 1st?

It takes several days to execute the problem.

  • Consider the following:
ScheduledExecutorService exec = Executors.newScheduledThreadPool (1); exec.scheduleWithFixedDelay (New), MyTimeTask (direct current from the first execution and so on the number of milliseconds, the interval of a few days, TimeUnit.DAYS);

The advantage is simple, maybe you don’t know it intuitively next time, such as 10 days, you want to see if the next will be executed tomorrow or the day after tomorrow? It might be necessary to find the last execution time simply.

  • You can do this
Public static void main (String[] args) {ScheduledExecutorService exec = Executors.newScheduledThreadPool (1); exec.scheduleWithFixedDelay (New), MyTimeTask (0, 10, TimeUnit.SECONDS);}
Public class MyTimeTask implements Runnable SimpleDateFormat simpleDateFormatt SimpleDateFormat {private = new ("yyyy-MM-dd HH:mm:ss"); private int interval = 5; private = Date runTime new (Date); public (MyTimeTask) {try {interval = Integer.parseInt (ParseProperties.newInstance (.GetProperty) ("interval"));} catch (Exception E) {e.printStackTrace (String runTimes);} = (ParseProperties.newInstance).GetProperty ("runTime"); if (StringUtils.isNotEmpty (runTimes)) {try {runTime = simpleDateFormatt.parse (runTi MES catch (Exception);} e {(e.printStackTrace)}}}); @Override public void (run) if (New Date {/ / judgment (.CompareTo) (runTime) > =0) {System.out.println ("executive.................. "+simpleDateFormatt.format (New) (Date)); runTime = getNextRunTime (); System.out.println (" the next execution time: "+simpleDateFormatt.format (runTime));}} {Calendar (public Date getNextRunTime) = Calendar.getInstance (CAL); cal.setTime (runTime); / / when the current time is greater than the possible configuration inside the configuration such as configuration the 2016-04-21 00:00:01 is the current time is 2017-04-21 18:20:00 / / then the next execution time should be: 2017-04-24 00:00:01 long n (New) = Date (.GetTime) - runTime.getTime (()) / (1000 * 60 * 60 * 24L + interval; cal.add (Calendar.DATE), (int) n); Return cal.getTime ();}

This is the ability to print the next execution time, especially if the next execution time is stored outside, so the flexibility can change the execution of the scheduler.

Individual public number

Understanding and thinking about timing execution of sping quartz
ingenuity zero public number.Jpg