search
HomeJavajavaTutorialRelated content of cron expression based on the simplest scheduled task implementation and configuration based on Spring

Originally this article was going to be published together with the first two articles in this series. However, I encountered an accident yesterday when I was searching for information and summarizing it, so I postponed it a bit.

The content of this article mainly refers to this blog post: (Baidu’s top search link for cron expressions). Try to write something a little different from others. Although, the content is mostly similar.

Let’s start with an example:

“0 0 10,14,16 * *?” What does it mean?

First of all, because the Cron expression is a string, the string is separated by 5 or 6 spaces and divided into 6 or 7 fields. Each field represents a meaning. Cron has the following two syntax formats :

# Seconds MINUTES HOURS Dayofmonth Month Dayofweek Year or
Seconds MINUTES HOURS DAYOFMONTH MOY Dayofweek

# : " The first two positions are both 0. The third position is the value of the hour, which is 10, 14, and 16. Here "," means listing and enumeration, which means that it will be executed every time it encounters 10, 14, and 16 hours, and The ones that follow are *, which means matching any value in this field. If * is used in the Minutes field, it means that the event will be triggered every minute.

Then the last "?" in "0 0 10,14,16 * * ?" refers to ?: can only be used in the two fields of DayofMonth and DayofWeek. It also matches any value of the domain, but it doesn't. Because DayofMonth and DayofWeek will affect each other. For example, if you want to trigger scheduling on the 20th of each month, no matter what day of the week the 20th falls, you can only use the following writing method: 13 13 15 20 * ?, where the last digit can only be used? , but * cannot be used. If * is used, it means that it will be triggered regardless of the day of the week, which is not actually the case.

So "0 0 10,14,16 * *?" means that it will be triggered at 10 o'clock, 14 o'clock, and 16 o'clock every day.

Then, the next question:

“0 0/30 9-17 * *?”

In this expression, there are two more than the previous one symbols, one is "/" and the other is "-". Their meanings are:

"-": Indicates the range. For example, using 5-20 in the Minutes field means that it triggers every minute from 5 minutes to 20 minutes.

"/": Indicates the starting time. Start triggering, and then trigger it every fixed time. For example, using 5/20 in the Minutes field means that it triggers once every 5 minutes, and triggers once every 25, 45, etc.

So here "0/30" ” means that it will be executed every 30 minutes starting from 0 minutes. And "9-17" marks the range, that is, it will be executed from 9 o'clock to 17 o'clock. Taken together, "0 0/30 9-17 * * ?" This expression means that it is executed every 30 minutes from 9:00 to 17:00 every day.

Continue, next question:

The expression here has an extra "WED", which is the abbreviation of Wednesday

So here " 0 0 12 ? * WED" means every Wednesday at 12 noon, the test results will be at once. (For the convenience of testing, change the time to every 10 seconds)

The remaining cron expressions in this article can be tested and verified in the scheduled tasks in the previous two articles. . The remaining expressions will not be analyzed one by one. The following is, quoting the original text, hoping to answer some questions:

1. Cron expression content analysis

Cron expression is a string , the string is separated by 5 or 6 spaces and divided into 6 or 7 fields. Each field represents a meaning. Cron has the following two syntax formats:

Seconds Minutes Hours DayofMonth Month DayofWeek Year or Seconds Minutes Hours DayofMonth Month DayofWeek

The characters that can appear in each field are as follows:

Seconds: Four characters ", - * /" can appear, and the valid range is 0 Integer of -59
Minutes: Four characters ", - * /" can appear, the valid range is 0-59 Integer
Hours: Four characters ", - * /" can appear, the valid range is 0 -23 integer
DayofMonth: eight characters ", - * / ? L W C" can appear, the valid range is an integer of 0-31
Month: four characters ", - * /" can appear, the valid range It is an integer from 1 to 12 or JAN-DEc
DayofWeek: The four characters ", - * / ? L C #" can appear, and the valid range is an integer from 1 to 7 or the two ranges of SUN-SAT. 1 means Sunday, 2 means Monday, and so on.
Year: Four characters ", - * /" can appear, and the valid range is 1970-2099.

Each field uses numbers, but there are still The following special characters can appear, and their meanings are:

(1)*: Indicates matching any value in this field. If * is used in the Minutes field, it means that the event will be triggered every minute.

(2)?: Can only be used in the DayofMonth and DayofWeek domains. It also matches any value of the domain, but it doesn't. Because DayofMonth and DayofWeek will affect each other. For example, if you want to trigger scheduling on the 20th of each month, no matter what day of the week the 20th falls, you can only use the following writing method: 13 13 15 20 * ?, where the last digit can only be used? , but * cannot be used. If * is used, it means that it will be triggered regardless of the day of the week, which is not actually the case.

(3)-: Indicates the range. For example, using 5-20 in the Minutes field means that it triggers every minute from 5 minutes to 20 minutes.

(4)/: Indicates the start time. Trigger, and then trigger every fixed time. For example, using 5/20 in the Minutes field means that it triggers once every 5 minutes, and triggers once every 25, 45, etc.

(5),: means listing enum value value. For example: using 5,20 in the Minutes field means that it is triggered every minute at 5 and 20 minutes.

(6)L: means last and can only appear in the DayofWeek and DayofMonth fields. If 5L is used in the DayofWeek field, it means it will be triggered on the last Thursday.

(7)W: Indicates valid working days (Monday to Friday), which can only appear in the DayofMonth field. The system will trigger the event on the nearest valid working day to the specified date. For example: using 5W on DayofMonth, if the 5th is a Saturday, it will be triggered on the nearest working day: Friday, which is the 4th. If the 5th is a Sunday, it will be triggered on the 6th (Monday); if the 5th is on one of Monday to Friday, it will be triggered on the 5th. Another point is that the latest search for W will not span months

(8)LW: These two characters can be used together to indicate the last working day of a certain month, that is, the last Friday.

(9)#: Used to determine the day of the week of each month and can only appear in the DayofMonth field. For example, in 4#2, it means the second Wednesday of a certain month.

A few examples:
0 0 2 1 * ? * means scheduling tasks at 2 a.m. on the 1st of each month
0 15 10 ? * MON-FRI means Monday to Friday Execute the job every day at 10:15 am
0 15 10 ? 6L 2002-2006 means execute the job at 10:15 am on the last Friday of each month from 2002 to 2006


A cron expression There are at least 6 (possibly 7) space-separated time elements.
In order, they are
seconds (0~59)
minutes (0~59)
hours (0~23)
days (months) (0~31, but you need to consider The number of days in your month)
month (0~11)
day (week) (1~7 1=SUN or SUN, MON, TUE, WED, THU, FRI, SAT)
year (1970- 2099)

Each element can be a value (such as 6), a continuous interval (9-12), an interval (8-18/4) (/ means every 4 hours), a List (1,3,5), wildcard. Since the two elements "date of month" and "date of week" are mutually exclusive, one of them must be set?


Some subexpressions can include Some ranges or lists

For example: the subexpression (day (week)) can be "MON-FRI", "MON, WED, FRI", "MON-WED,SAT"

The "*" character represents all possible values ​​

Therefore, "*" in the sub-expression (month) represents the meaning of each month, and "*" in the sub-expression (day (week)) represents the day of the week Each day


The "/" character is used to specify the increment of the value
For example: "0/15" in the subexpression (minutes) means that starting from the 0th minute, every 15 minutes
The "3/20" in the subexpression (minutes) means that starting from the 3rd minute, every 20 minutes (it has the same meaning as "3, 23, 43")


The "?" character is only used in the two sub-expressions of day (month) and day (week), indicating that no value is specified
When one of the two sub-expressions is specified with a value, in order to avoid conflicts, it is necessary to Set the value of another subexpression to "?"

The "L" character is only used in the day (month) and day (week) subexpressions, which is the abbreviation of the word "last"
But its meaning in the two subexpressions is different.
In the day (month) sub-expression, "L" represents the last day of the month
In the day (week) self-expression, "L" represents the last day of the week, which is SAT

If there is specific content before "L", it has other meanings

For example: "6L" means the sixth to last day of this month, "FRIL" means the last day of this month One Friday
Note: When using the "L" parameter, do not specify a list or range as this will cause problems

Special characters allowed for field allowed values ​​
Seconds 0-59, - */
Minute 0-59, - * /
Hour 0-23, - * /
Date 1-31, - * ? / L W C
Month 1-12 or JAN-DEC, - * /
Weekday 1-7 or SUN-SAT, - * ? / L C
#Year (optional) Leave blank, 1970-2099, - * /

2. Cron expression example:

"0 0 12 * * ?" Triggered every day at 12 noon
"0 15 10 ? * *" Triggered every day at 10:15 am
"0 15 10 * * ?" Triggered every day at 10:15 am
"0 15 10 * * ? *" Triggered every day at 10:15 AM
"0 15 10 * * ? 2005" Triggered every day at 10:15 AM in 2005
"0 * 14 * * ?" Trigger every 1 minute from 2pm to 2:59pm every day
"0 0/5 14 * * ?" Trigger every 5 minutes from 2pm to 2:55pm every day
"0 0/5 14,18 * * ?" Triggers every 5 minutes between 2pm and 2:55pm and every 5 minutes between 6pm and 6:55pm
"0 0-5 14 * * ?" Triggers every afternoon Triggered every 1 minute from 2:00 to 2:05 pm
"0 10,44 14 ? 3 WED" Triggered every year on Wednesdays in March at 2:10 pm and 2:44 pm
"0 15 10 ? * MON-FRI" Triggers at 10:15 am from Monday to Friday
"0 15 10 15 * ?" Triggers at 10:15 am on the 15th of each month
"0 15 10 L * ?" On the last day of each month Triggered at 10:15 AM
"0 15 10 ? * 6L" Triggered at 10:15 AM on the last Friday of each month
"0 15 10 ? * 6L 2002-2005" Every month from 2002 to 2005 Triggered at 10:15 AM on the last Friday
"0 15 10 ? * 6#3" Triggered at 10:15 AM on the third Friday of each month

The above is the detailed content of Related content of cron expression based on the simplest scheduled task implementation and configuration based on Spring. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Is java still a good language based on new features?Is java still a good language based on new features?May 12, 2025 am 12:12 AM

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

What Makes Java Great? Key Features and BenefitsWhat Makes Java Great? Key Features and BenefitsMay 12, 2025 am 12:11 AM

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Top 5 Java Features: Examples and ExplanationsTop 5 Java Features: Examples and ExplanationsMay 12, 2025 am 12:09 AM

The five major features of Java are polymorphism, Lambda expressions, StreamsAPI, generics and exception handling. 1. Polymorphism allows objects of different classes to be used as objects of common base classes. 2. Lambda expressions make the code more concise, especially suitable for handling collections and streams. 3.StreamsAPI efficiently processes large data sets and supports declarative operations. 4. Generics provide type safety and reusability, and type errors are caught during compilation. 5. Exception handling helps handle errors elegantly and write reliable software.

How do Java's Top Features Impact Performance and Scalability?How do Java's Top Features Impact Performance and Scalability?May 12, 2025 am 12:08 AM

Java'stopfeaturessignificantlyenhanceitsperformanceandscalability.1)Object-orientedprincipleslikepolymorphismenableflexibleandscalablecode.2)Garbagecollectionautomatesmemorymanagementbutcancauselatencyissues.3)TheJITcompilerboostsexecutionspeedafteri

JVM Internals: Diving Deep into the Java Virtual MachineJVM Internals: Diving Deep into the Java Virtual MachineMay 12, 2025 am 12:07 AM

The core components of the JVM include ClassLoader, RuntimeDataArea and ExecutionEngine. 1) ClassLoader is responsible for loading, linking and initializing classes and interfaces. 2) RuntimeDataArea contains MethodArea, Heap, Stack, PCRegister and NativeMethodStacks. 3) ExecutionEngine is composed of Interpreter, JITCompiler and GarbageCollector, responsible for the execution and optimization of bytecode.

What are the features that make Java safe and secure?What are the features that make Java safe and secure?May 11, 2025 am 12:07 AM

Java'ssafetyandsecurityarebolsteredby:1)strongtyping,whichpreventstype-relatederrors;2)automaticmemorymanagementviagarbagecollection,reducingmemory-relatedvulnerabilities;3)sandboxing,isolatingcodefromthesystem;and4)robustexceptionhandling,ensuringgr

Must-Know Java Features: Enhance Your Coding SkillsMust-Know Java Features: Enhance Your Coding SkillsMay 11, 2025 am 12:07 AM

Javaoffersseveralkeyfeaturesthatenhancecodingskills:1)Object-orientedprogrammingallowsmodelingreal-worldentities,exemplifiedbypolymorphism.2)Exceptionhandlingprovidesrobusterrormanagement.3)Lambdaexpressionssimplifyoperations,improvingcodereadability

JVM the most complete guideJVM the most complete guideMay 11, 2025 am 12:06 AM

TheJVMisacrucialcomponentthatrunsJavacodebytranslatingitintomachine-specificinstructions,impactingperformance,security,andportability.1)TheClassLoaderloads,links,andinitializesclasses.2)TheExecutionEngineexecutesbytecodeintomachineinstructions.3)Memo

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version