026_Scheduled timer

1. The scheduled timer is spring 3 0 comes with a timer.

2. Use maven to build a spring boot scheduled project called spring boot

3. Add coordinates that support Scheduled

4. pom.xml 

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.bjbs</groupId>
	<artifactId>spring-boot-scheduled</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.13.RELEASE</version>
	</parent>

	<!-- modify jdk edition -->
	<properties>
		<java.version>1.8</java.version>
		<!-- appoint thymeleaf and thymeleaf-layout-dialect Higher versions can prevent html Nonstandard label and error report -->
		<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
		<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
	</properties>

	<dependencies>
		<!-- springBoot Starter for -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- springBoot thymeleaf Starter for -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

		<!-- Add pair Scheduled Supported coordinates -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
	</dependencies>
</project>

5. Create a new userservice java

package com.bjbs.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
	public void addUser() {
		System.out.println("Add User......");
	}
}

6. Create a new scheduleddemo java

package com.bjbs.scheduled;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.bjbs.service.UserService;

/**
 * Scheduled Scheduled task
 */
@Component
public class ScheduledDemo {
	@Autowired
	private UserService userService;
	
	/**
	 * Timed task method
	 * 
	 * @Scheduled: Set cron attribute of scheduled task: cron expression. Timed task trigger is a string expression of time
	 */
	@Scheduled(cron = "0/2 * * * * ?")
	public void scheduledMethod() {
		System.out.println("Timer triggered: " + new Date());
		
		userService.addUser();
	}
}

7. Create a new app java

package com.bjbs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * SpringBoot Startup class
 */
@SpringBootApplication
@EnableScheduling // On the startup class, add the annotation to start the scheduled task
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

8. Operation project

9. Explanation of cron expression

9.1. cron expression is a string, which is divided into 6 or 7 fields. Each field represents a meaning. There are two syntax formats:

9.1.1. Seconds Minutes Hours Day Month Week

9.1.2. Seconds Minutes Hours Day Month Week Year

9.2. Corns are separated from left to right by spaces: the date, month, week and year in the second minute hour month.

9.3. In addition to setting numerical values, the time field of cron expression can also use some special characters to provide functions such as list, range, wildcard, etc.

9.4. Meaning of each field

position

Time domain name

Allowable value

Allowed special characters

1

second

0-59

, - * /

2

minute

0-59

, - * /

3

hour

0-23

, - * /

4

day

1-31

, - * ? / L W C

5

month

1-12

, - * /

6

week

1-7

, - * ? / L C #

7

Year (optional)

1970-2099

, - * /

9.5. Asterisk (*): it can be used in all fields to represent each time in the corresponding time domain. For example: * in the minute field, it means "every minute".

9.6. Question mark (?): This character is only used in the date and week fields in the month. It is usually specified as "meaningless value", which is equivalent to a placeholder.

9.7. Minus sign (-): indicates a range. If "10-12" is used in the hour field, it means from 10 to 12 points, i.e. 10,11,12.

9.8. Comma (,): indicates a list value. If "MON,WED,FRI" is used in the week field, it indicates Monday, Wednesday and Friday.

9.9. Slash (/): x/y represents an equal step sequence, where x is the starting value and Y is the incremental step value. If 0 / 15 is used in the minute field, it means 0, 15, 30 and 45 seconds; While 5 / 15 represents 5,20,35,50 in the minute field; You can also use * / y, which is equivalent to 0/y.

9.10. 50: This character is only used in the date and week fields in the month, which means "Last". L in the date field, it represents the Last day of the month, such as the 31st of January and the 28th of February in a non leap year. If l is used in the week, it means Saturday, which is equivalent to 7. However, if l is in the week field and there is a value D in front of it, it means "the Last week D of this month". For example, 6L means the Last Friday of this month.

9.11. W: This character can only appear in the date field in the month. It is a modification of the leading date and represents the working day closest to the date. For example: 15W indicates the nearest working day from the 15th of the month. If the 15th of the month is Saturday, it matches Friday on the 14th; If the 15th is Sunday, match the 16th Monday; If the 15th is Tuesday, the result is Tuesday the 15th. However, it must be noted that the associated matching date cannot span months. For example, if you specify 1W, if the 1st is Saturday, the result matches Monday, the 3rd, rather than the last day of last month. W string can only specify a single date, not a date range.

9.12. LW combination: LW can be combined in the date field of the month, which means the last working day of the month.

9.13. Pound sign (#): this character can only be used in the week field, indicating a working day of the current month. For example, 6#3 represents the third Friday of the current month (6 represents Friday, #3 represents the current third), while 4#5 represents the fifth Wednesday of the current month. If there is no fifth Wednesday in the current month, ignore and do not trigger.

9.14. C: This character is only used in the date and week fields in the month, which means "Calendar". It means the date associated with the plan. If the date is not associated, it is equivalent to all the dates in the Calendar. For example, 5C in the date field is equivalent to the first day after the 5th day of the Calendar. 1C in the week field is equivalent to the first day after Sunday.

9.15. cron expressions are not sensitive to the case of special characters, nor are they sensitive to the case of abbreviations representing weeks.

Keywords: Java Spring Boot

Added by trparky on Sun, 30 Jan 2022 05:05:50 +0200