Spring quartz @scheduled cron expression not working

Spring cron expression not working

Posted by chyuan on 2022-11-14
Estimated Reading Time 4 Minutes
Words 661 In Total

General & Background

Recently, I want to do a scheduled email-sending function, which is sent regularly on Monday at 10 am.

Specific implementation and testing

It is used when implementing and testing

1
@Scheduled(fixedRate = 1000 * 60)

It means to execute the task every 60 seconds, with the above expression, everything is OK, easily and happily knock out the code sent by the implementation, BingGo, after the test passes. Finally, it needs to be put into production, and the other code has not changed, so it will be changed to cron, as follows:

1
@Scheduled(cron = "0 0 10 * * 2")

Here’s the problem

After changing the expression to @Scheduled (cron = "0 0 10 * * 2"), sit and wait for the production to send it regularly, ding~~ When it was 1 o’clock after 10, the user wrote an email and asked me, hey, is Xiao Wang (pseudonym) there? We didn’t receive the email, can you see if there is a Shenma problem? I was shocked and immediately sat up because I hadn’t tested @Scheduled(cron="0 0 10 * * 2") locally.

Retest

1
@Scheduled(cron = "0 3 10 * * 2")

When I received the question, it was 10:01, so I set 10:03 locally, and waited for the task to trigger, d=====( ̄▽ ̄*)b, the time was up, but it did not enter my breakpoint. I was a little broken inside, and I found various cron online expression verification, as follows:
insert image description here
insert image description here

Today is August 1st, and now the time is past 10 o’clock, and today is also Monday, so there is no time for today, but it can be inferred from the forecast that it should be implemented this morning. The last number 2 represents the day of the week, Sunday is the 1st, so Monday is the 2nd, so I wrote 2, and the online check website also affirmed my writing.

Conclusion

At this point, I was a little crazy, not knowing why death and life would not enter my breakpoint. So I searched wildly for search engines until I saw this:
insert image description here
Suddenly, as if I saw a beam of light, my God, it turns out that the starting point of these two things is different, and I want to scold the street.

The day of the week of crontab in Linux is from Sunday, but spring scheduled tasks is from Monday, so I know why I can’t enter my breakpoint, because according to the expression @Scheduled (cron = "0 3 10 * * 2"), I need to enter the breakpoint tomorrow, Tuesday.

Reason

But that wasn’t enough, I wondered why spring scheduled would fix this anti-human design, so I thought of looking at the source code for this implementation, and I found this:
https://github.com/spring-projects/spring-framework/blob/6e4551131dddffecd7adc00d96e3ecddd4c4911c/spring-context/src/main/java/org/springframework/scheduling/support/QuartzCronField.java
insert image description here
DayOfWeek comes from java.time.DayOfWeek, and it is an enumerated type

Search online and find:
https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html
insert image description here
Here is the description:

In addition to the textual enum name, each day-of-week has an int value. The int value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday). It is recommended that applications use the enum rather than the int value to ensure code clarity.

This enum provides access to the localized textual form of the day-of-week. Some locales also assign different numeric values to the days, declaring Sunday to have the value 1, however this class provides no support for this.

The source is found here, 1 stands for Monday, and it also shows that in some places 1 is used to represent Sunday, but in the DayOfWeek class, this statement is not supported.

At this point, I replaced @Scheduled(cron="0 0 10 * * 2") with @Scheduled(cron="0 0 10 * * 1") and then it was done.


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !