Today is the 12th day of my study in LeByte Education. The main content of today’s study is Spring Task Scheduled Task Main Content Overview of Scheduled Task Development in a project should be a relatively common requirement. There are three main solutions for developing scheduled Task in Java: Use the JDK’s built-in Timer, Quartz, and Spring Task.

Timer is a built-in timed task tool in JDK. It is simple and easy to use, but it cannot meet the complex timing rules and is rarely used in actual project development. Quartz is powerful, but relatively cumbersome to use. Spring Task has the advantages of the first two (powerful and easy to use), is simple to use, requires no additional packages beyond spring-related packages, and supports annotations and configuration files.

Implementing scheduled tasks using Spring Task You can configure scheduled tasks using Spring Task:

1. The XML configuration

2. Annotate the configuration

In the POM.xml configuration file, modify the project environment to add Spring dependency coordinates

Add the configuration file spring. XML in the SRC /main/resources directory and set up the Spring scanner

<context:component-scan base-package=”com.xxxx” /> Define the scheduled task method Create a new class, add auto-injected annotations, and define the method that defines the task package com.xxxx.task;

import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date;

@Component public class TaskJob {

Public void job1(){system.out.println (” task 1:” + new SimpleDateFormat(” YYYY-MM-DD hh: MM :ss”).format(new Date())); }

Public void job2(){system.out.println (” task 2:” + new SimpleDateFormat(” YYYY-MM-DD hh: MM :ss”).format(new Date())); }} Adding a scheduled task namespace In the spring. XML configuration file, add the scheduled task namespace

XMLNS: task = “HTTP: / / www.springframework.org/schema/task…

www.springframework.org/schema/task www.springframework.org/schema/task… Modify the configuration file as follows:

Scheduled Task Configuration

task:scheduled-tasks

< Task: Scheduled ref= “taskJob” method= “job1” cron= “0/2 * * * *?” < Task: Scheduled ref= “taskJob” method= “job1” cron= “0/2 * * * *? />

< Task: Scheduled ref= “taskJob” method= “job2” cron= “0/5 * * * *?” < Task: Scheduled ref= “taskJob” method= “job2” cron= “0/5 * * * *? />

</task: Scheduled – Tasks > Scheduled tasks public static void main(String[] args) {

System.out.println(" Define task test...") ); / / get the Spring context ApplicationContext ac = new ClassPathXmlApplicationContext (" Spring. XML "); TaskJob TaskJob = (TaskJob) ac.getBean(" TaskJob ");Copy the code

} Implement Scheduled tasks using annotation configurations define the use of Scheduled task methods @scheduled annotations

package com.xxxx.task;

import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat; import java.util.Date;

@Component public class TaskJob02 {

@Scheduled(cron=”0/2 * * * * ?” ) public void job1(){system.out.println (” 表 1:” + new SimpleDateFormat(” YYYY-MM-DD hh: MM :ss”).format(new Date())); }

@Scheduled(cron=”0/5 * * * * ?”) public void job2(){ System.out.println(“任务 2:” + new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”).format(new Date())); } }

Adding a scheduled task namespace In the spring. XML configuration file, add a scheduled task namespace

XMLNS: task = “HTTP: / / www.springframework.org/schema/task…

www.springframework.org/schema/task www.springframework.org/schema/task… Configure the scheduled task driver

The <task:annotation-driven /> configuration file contains the following contents:

<context:component-scan base-package=”com.xxxx” />

<task:annotation- Driven /> Public static void main(String[] args) {system.out.println (” define task test… );

/ / get the Spring context ApplicationContext ac = new ClassPathXmlApplicationContext (" Spring. XML "); TaskJob02 TaskJob02 = (TaskJob02) ac.getBean(" TaskJob02 ");Copy the code

} Cron expressions Have at least six (and possibly seven) space-separated time elements. From left to right, these elements are defined as follows:

1. Seconds (0 to 59)

2. Minutes (0 — 59)

3. Hours (0 — 23)

4. Date of month (1-31)

5. Month (1-12 or Jan-Dec)

6. Day of the week (1 — 7 or SUN — SAT)

7. Year (1970 — 2099)

0, 0, 10,14,16 * *?

10 am, 2 PM and 4 PM daily 0,15,30,45 * 1-10 *?

Every 15 minutes for the first 10 days of the month 30 0 0 1 1? 2012

Available values of each time 30 seconds past midnight on January 1, 2012 are as follows:

0-59 seconds, – * /

It’s 0-59, – * /

Hour 0-23, – * /

Day 1-31, – *? / L W C

Month 1-12 or jan-dec, – * /

1-7 or sun-sat, – *? / L C #

Year (optional field) Empty, 1970-2099, – * /

The available values are analyzed as follows:

“” — the character can be used for all fields. In the” minute “field, it is set to” “to mean” every minute “. “?” The “day” and “day of the week” fields can be used to specify “undefined values”. This is used when you need to specify one of these fields and not the other. You can see what this means in the following examples. The “-” — character is used to specify the norm of a value. For example, set it to “10-12” in the “hour” field to “10 to 12”. “, “– the character specifies several values. For example, set “MON,WED,FRI” in the “Day of the week” field to “The days Monday, Wednesday, and Friday”. The slash character specifies the increment of a value. For example, setting “0/15” in the “seconds” field means “0, 15, 30, and 45 seconds “. And “5/15” means “5, 20, 35, and 50”. The “* “character before the ‘/’ specifies the start of 0 seconds. Each field has a series of values that can start or end. For the “second” and “minute” fields, the values range from 0 to 59. This is 0 to 23 for the “hour” field and 0 to 31 for the “day” field. For the “month” field it is 1 to 12. The “/” field just helps you to start with the “NTH” value within the range of values allowed. “L” – the character can be used in the “day” and “day of the week” fields. It is short for “last”, but has different meanings in the two fields. The “L” in the “day” field stands for “last day of the month,” which is the 31st for January and the 28th for February (not leap years). In the “day of the week” field, it simply means “7 “or” SAT “. But if used after a number in the “day of the week” field, it means “last week of the month ×”. For example, “6L” stands for “last Friday of the month”. When using the “L” option, it is important to specify a definite list or range, otherwise you will be confused by the results. “W” — can be used for the “day” field. Used to specify the most recent working day (Monday through Friday) for a given calendar date. For example, set the “day” field to “15W “, which means “the closest working day to the 15th of the month”. So if the 15th falls on a Saturday, the trigger will be called on Friday the 14th. If the 15th falls on a Sunday, the trigger will fire on Monday the 16th. If the 15th falls on a Tuesday, it will be triggered that day. If the “day” field is set to “1W” and “1 “is a Saturday, it will be triggered next Monday, the third of the month, and it will not cross the range of values for that month. The “W” character can only be used when the value of the “day” field is a single day rather than a series of values. “L” and “W” can be combined for the “day” field to indicate “LW”, meaning “last business day of the month”. “#” — the character can be used for the “day of the week” field. This character stands for “week x of the month”. For example, “6#3” is the third Friday of the month (6 means Friday and “#3” is the third Friday of the month). Another example: “2#1” is the first Monday of the month, while “4#5” is the fifth Wednesday of the month. Note that if you specify “#5” for a month without a fifth “week ×”, the month will not trigger. “C” — the character can be used for the “day” and “day of the week” fields, which is short for “Calendar”. It is represented as a calculated value based on the associated calendar (if any). If there is no associated calendar, it is equivalent to including all calendars. The “day” field value is “5C “, which means “the first day in the calendar or after the 5th”. If the value of Day of the week is 1C, it indicates the first day in the calendar or after Sunday. Characters that are valid for “month” and “day of the week” fields are not case sensitive. Some examples:

“0, 0, 12 * *? It’s triggered at twelve o ‘clock every day

“0 15 10? “* *” is triggered every morning at 10:15

“0, 15, 10 * *? Triggered every morning at 10:15

“0, 15, 10 * *? * “triggered every morning at 10:15

“0, 15, 10 * *? 2005 “triggered every morning at 10:15 in 2005

“0 * 14 * *?” It is triggered every minute from 2 p.m. to 2:59 p.m. every day

“0 0/5 14 * *?” It is triggered every five minutes every day from 2pm to 2:55pm

“0/5 14,18 * *?” It is triggered every five minutes every day between 2 p.m. and 2:55 p.m. and between 6 p.m. and 6:55 p.m

“0 0-5 14 * *?” Triggered every minute from 14:00 to 14:05 every day

“0 10 44 14? 3 WED “triggered every Wednesday in March at 14:10 and 14:44

“0 15 10? * Mon-fri “hits every Monday, Tuesday, Wednesday, Thursday and Friday at 10:15

“0 15 10 15 *?” Triggered at 10:15 on the 15th of every month

“0, 15, 10 L *? Triggered at 10:15 on the last day of the month

“0 15 10? * 6L “triggers every 5 minutes at 10:15 on the last Friday of each month

“0/5 14,18 * *?” It is triggered every five minutes every day between 2 p.m. and 2:55 p.m. and between 6 p.m. and 6:55 p.m

“0 0-5 14 * *?” Triggered every minute from 14:00 to 14:05 every day

“0 10 44 14? 3 WED “triggered every Wednesday in March at 14:10 and 14:44

“0 15 10? * Mon-fri “hits every Monday, Tuesday, Wednesday, Thursday and Friday at 10:15

“0 15 10 15 *?” Triggered at 10:15 on the 15th of every month

“0, 15, 10 L *? Triggered at 10:15 on the last day of the month

“0 15 10? * 6L “10:15 on the last Friday of every month