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
Java Spring怎么实现定时任务Java Spring怎么实现定时任务May 24, 2023 pm 01:28 PM

java实现定时任务Jdk自带的库中,有两种方式可以实现定时任务,一种是Timer,另一种是ScheduledThreadPoolExecutor。Timer+TimerTask创建一个Timer就创建了一个线程,可以用来调度TimerTask任务Timer有四个构造方法,可以指定Timer线程的名字以及是否设置为为守护线程。默认名字Timer-编号,默认不是守护线程。主要有三个比较重要的方法:cancel():终止任务调度,取消当前调度的所有任务,正在运行的任务不受影响purge():从任务队

Java axios与spring前后端分离传参规范是什么Java axios与spring前后端分离传参规范是什么May 03, 2023 pm 09:55 PM

一、@RequestParam注解对应的axios传参方法以下面的这段Springjava代码为例,接口使用POST协议,需要接受的参数分别是tsCode、indexCols、table。针对这个Spring的HTTP接口,axios该如何传参?有几种方法?我们来一一介绍。@PostMapping("/line")publicList

Spring Boot与Spring Cloud的区别与联系Spring Boot与Spring Cloud的区别与联系Jun 22, 2023 pm 06:25 PM

SpringBoot和SpringCloud都是SpringFramework的扩展,它们可以帮助开发人员更快地构建和部署微服务应用程序,但它们各自有不同的用途和功能。SpringBoot是一个快速构建Java应用的框架,使得开发人员可以更快地创建和部署基于Spring的应用程序。它提供了一个简单、易于理解的方式来构建独立的、可执行的Spring应用

Spring 最常用的 7 大类注解,史上最强整理!Spring 最常用的 7 大类注解,史上最强整理!Jul 26, 2023 pm 04:38 PM

随着技术的更新迭代,Java5.0开始支持注解。而作为java中的领军框架spring,自从更新了2.5版本之后也开始慢慢舍弃xml配置,更多使用注解来控制spring框架。

Java Spring框架创建项目与Bean的存储与读取实例分析Java Spring框架创建项目与Bean的存储与读取实例分析May 12, 2023 am 08:40 AM

1.Spring项目的创建1.1创建Maven项目第一步,创建Maven项目,Spring也是基于Maven的。1.2添加spring依赖第二步,在Maven项目中添加Spring的支持(spring-context,spring-beans)在pom.xml文件添加依赖项。org.springframeworkspring-context5.2.3.RELEASEorg.springframeworkspring-beans5.2.3.RELEASE刷新等待加载完成。1.3创建启动类第三步,创

从零开始学Spring Cloud从零开始学Spring CloudJun 22, 2023 am 08:11 AM

作为一名Java开发者,学习和使用Spring框架已经是一项必不可少的技能。而随着云计算和微服务的盛行,学习和使用SpringCloud成为了另一个必须要掌握的技能。SpringCloud是一个基于SpringBoot的用于快速构建分布式系统的开发工具集。它为开发者提供了一系列的组件,包括服务注册与发现、配置中心、负载均衡和断路器等,使得开发者在构建微

Java Spring Bean生命周期管理的示例分析Java Spring Bean生命周期管理的示例分析Apr 18, 2023 am 09:13 AM

SpringBean的生命周期管理一、SpringBean的生命周期通过以下方式来指定Bean的初始化和销毁方法,当Bean为单例时,Bean归Spring容器管理,Spring容器关闭,就会调用Bean的销毁方法当Bean为多例时,Bean不归Spring容器管理,Spring容器关闭,不会调用Bean的销毁方法二、通过@Bean的参数(initMethod,destroyMethod)指定Bean的初始化和销毁方法1、项目结构2、PersonpublicclassPerson{publicP

spring设计模式有哪些spring设计模式有哪些Dec 29, 2023 pm 03:42 PM

spring设计模式有:1、依赖注入和控制反转;2、工厂模式;3、模板模式;4、观察者模式;5、装饰者模式;6、单例模式;7、策略模式和适配器模式等。详细介绍:1、依赖注入和控制反转: 这两个设计模式是Spring框架的核心。通过依赖注入,Spring负责管理和注入组件之间的依赖关系,降低了组件之间的耦合度。控制反转则是指将对象的创建和依赖关系的管理交给Spring容器等等。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools