Note the use of Spring’s built-in scheduled tasks.
Spring uses scheduled tasks
Use scheduled tasks based on XML configuration files
First, configure Spring to enable scheduled tasks
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <task:annotation-driven /> <! -- Timer switch --> <bean id="myTask" class="com.spring.task.MyTask"></bean> <task:scheduled-tasks> <! Task: Scheduled ref= task:scheduled ref="myTask" method="show" cron="*/5 * * * *?" />
<task:scheduled ref="myTask" method="print" cron="*/10 * * * *?"/> </task:scheduled-tasks> <! -- Automatic scan package name --> <context:component-scan base-package="com.spring.task" />
</beans>
Copy the code
Define your own task execution logic
package com.spring.task; /** * define task */ public class MyTask {public voidshow() {
System.out.println("show method 1");
}
public void print() {
System.out.println("print method 1"); }}Copy the code
Use scheduled tasks based on annotations
package com.spring.task; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * Annotated timer */ @component public class MyTask2 {/** * timed calculation. */ @scheduled (cron = Scheduled) is performed every day at 01:00 am"0 0 1 * * *")
public void show() {
System.out.println("show method 2"); */ Scheduled(fixedRate = 1000*2) public void */ Scheduled(fixedRate = 1000*2) public void */ Scheduled(fixedRate = 1000*2print() {
System.out.println("print method 2"); }}Copy the code
This way, when the project starts, the scheduled tasks will be executed on time according to the rules.
Spring Boot uses scheduled tasks
Spring Boot is more convenient to use.
- The introduction of
springboot starter
package
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
Copy the code
- Start the class addition in the program entry
@EnableScheduling
To enable the scheduled task function
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Copy the code
- Define the scheduled task logic
@Component
public class MyTask3 {
private int count=0;
@Scheduled(cron="*/6 * * * *?")
private void process() {
System.out.println("this is scheduler task runing "+(count++)); }}Copy the code
Description of task execution rules
Take a look at the source code for the @Scheduled annotation
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
String cron() default "";
String zone() default "";
long fixedDelay() default -1;
String fixedDelayString() default "";
long fixedRate() default -1;
String fixedRateString() default "";
long initialDelay() default -1;
String initialDelayString() default "";
}
Copy the code
As you can see, there are eight parameters that can be passed in annotations:
- Cron: Specifies the CRON expression
- Zone: The default server time zone is used by default. Can be set to
java.util.TimeZone
The zoneId - FixedDelay: The interval between the completion of the previous task and the start of the next task, in milliseconds
- FixedDelayString: same as above, time value is
String
type - FixedRate: The interval from the start of the previous task to the start of the next task, in milliseconds
- FixedRateString: same as above, time value is
String
type - InitialDelay: Delay for the first execution of a task, in milliseconds
- InitialDelayString: same as above, the time value is
String
type
How to use cron expressions
A Cron expression is a string separated by five or six Spaces and divided into six or seven fields, each field representing a meaning. Cron has the following two syntax formats:
- Seconds Minutes Hours DayofMonth Month DayofWeek Year
- Seconds Minutes Hours DayofMonth Month DayofWeek
The characters that can appear in each field are as follows:
- Seconds: an integer ranging from 0 to 59, including “, – * /”
- Minutes: the value can contain four characters: “, – * /” and is an integer ranging from 0 to 59
- Hours: the value is an integer ranging from 0 to 23 and contains the following characters: “, – * /”
- DayofMonth: can appear “, – * /? The value is an integer ranging from 0 to 31
- Month: “, – * /”, an integer ranging from 1 to 12 or Jan-dec
- DayofWeek: can appear “, – * /? L C #” is a four-character integer ranging from 1 to 7 or sun-sat. 1 means Sunday, 2 means Monday, and so on
- “Year” : contains “, – * /” and is valid from 1970 to 2099
Each field uses numbers, but the following special characters can also appear, which mean:
*
: matches any value in the field. If * is used in the Minutes field, events are emitted every minute.?
: Applies only to the DayofMonth and DayofWeek domains. It also matches any value of the field, but it doesn’t. Because DayofMonth and DayofWeek affect each other. For example, if you want to trigger scheduling on the 20th of each month, you can only use the following command: 13, 13, 15, 20 *? , the last digit can only be used? If * is used to indicate that the trigger is triggered no matter what day of the week, which is not the case.-
: indicates a range. For example, if 5-20 is used in the Minutes field, it is triggered every minute from 5 to 20 Minutes./
: indicates that the alarm starts at the start time and is triggered every fixed time. For example, if 5/20 is used in the Minutes field, the alarm is triggered every 5 Minutes, 25,45, and so on..
: lists enumeration values. For example, using 5,20 in the Minutes field means firing at 5 and 20 Minutes per minute.L
: indicates the last value. The value can be generated only in the DayofWeek and DayofMonth fields. If 5L is used in the DayofWeek field, the value is triggered on the last Thursday.W
: indicates a valid working day (Monday to Friday). It can only be in the DayofMonth field. The system triggers an event on the latest valid working day to the specified day. For example, if 5W is used in DayofMonth, if the 5th falls on a Saturday, it will be triggered on the nearest working day: Friday, the 4th. If the 5th falls on a Sunday, the 6th (Monday) is triggered; If the 5th falls on a day between Monday and Friday, it is triggered on the 5th. Another point is that the recent search for W does not cross months.LW
: The two characters can be used together to indicate the last working day of a month.#
: specifies the day of the month. It can only be used in the DayofMonth field. For example, 4#2 is the second Wednesday of a month.
See the CRon expression example