Java implements timing tasks (Quartz framework) to execute a task regularly (Maven version)

Quart framework

1. Introduction to Quartz

Quartz is another open source project in the field of Job scheduling by the OpenSymphony open source organization. It is completely written in Java and designed for use in J2SE and J2EE applications.
Quartz can be used to create simple or complex programs that run ten, hundreds, or even tens of thousands of Jobs.
It provides great flexibility without sacrificing simplicity.
You can use it to create simple or complex schedules for executing a job.

1、Job

Represents a task (work), the specific content to be performed.

2、JobDetail JobDetail

Represents a specific executable scheduler, Job is the content to be executed by the executable scheduler, and
JobDetail also contains the plan and strategy for this task scheduling.
Tell the scheduling container to execute which method of which class (job) in the future

3、Trigger

It is a class that represents the configuration of a scheduling parameter and describes the time triggering rules that trigger the execution of the Job.
One Job can correspond to multiple Triggers, but one Trigger can only correspond to one Job

4. Scheduler represents a scheduling container. Multiple JobDetails and Triggers can be registered in a scheduling container.

The Scheduler can bind the Trigger to a JobDetail, so that when the Trigger is triggered, the corresponding Job is executed.
Note: After the JobDetail and Trigger are registered on the scheduler container, an assembled job (a pair of JobDetail and Trigger) is formed, and it can be scheduled for execution as the container starts.

2. Getting Started Demo

1. Create a Demo project and add dependencies

Insert picture description here
Insert picture description here

2. Add dependency

Insert picture description here

<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.itzheng</groupId>
	<artifactId>JavaMailDemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.4</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>
	</dependencies>
</project>
3. Create the JobTest class, the code is as follows

Insert picture description here

4. Configuration file

Add applicationContext-job.xml file
Insert picture description here

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation=" 
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 定义一个任务类 -->
	<bean id="job" class="com.itzheng.demo.quartz.JobTest">
	</bean>
	<!-- 任务类描述 -->
	<bean id="jobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="job"></property>
		<property name="targetMethod" value="deJob"></property>
	</bean>
	<!-- 触发器 -->
	<bean id="mailTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="jobDetail"></property>
		<!-- cron 表达式 每 10 秒执行一次 -->
		<property name="cronExpression" value="0/10 * * * * ?"></property>
	</bean>
	<!-- 总管理容器 -->
	<bean id="startQuartz"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="mailTrigger" />
			</list>
		</property>
	</bean>
</beans>
5. Test

Create a test class QuartzTest. Note: You cannot use junit for testing, because the program will end as soon as it runs.
Insert picture description here

6. Running results (executed every 10 seconds)

Insert picture description here

Three, related concepts

1. Detailed explanation of cron expression

Cron expression is a string, the string is separated by 5 or 6 spaces, divided into 6 or 7 fields, each field represents a meaning. Also called seven sub-expression.

2. Domain

1) Seconds (seconds): " , - * /" can appear four characters, the valid range is an integer from 0-59

2) Minutes (minutes): " , - * /" can appear four characters, the valid range is an integer from 0-59

3) Hours (hours): " , - * /" can appear four characters, the valid range is an integer from 0-23

4) DayofMonth (day of month): "there may be , - * / ? LW C" eight characters, the effective range 1-31of integer

5) Month (month): , - * /Four characters " " can appear , and the valid range is an integer from 1-12

6) DayofWeek (day of week): can appear " , - * / ? L C #" eight characters, the valid range is an integer of 1-7, 1 means Sunday, 2 means Monday, and so on

7) Year (year): , - * /four characters " " can appear , and the valid range is 1970-2099

3. Character meaning

(1) It *means that it matches any value of this field. If it is used in the Minutes field *, it means that an event will be triggered every minute.

(2) ?Means no value is specified. It can only be used in DayofMonth and DayofWeek fields. Because DayofMonth and DayofWeek will affect each other. For example, if you want to trigger scheduling on the 20th of each month, regardless of whether the 20th is the day of the week, you can only use the following writing:, 13 13 15 20 * ?The last digit can only be used , but not *. If it is used *, it will trigger regardless of the day of the week.

(3) -Represents the range, for example, 5-20 is used in the Minutes field, which means triggering once every minute from 5 minutes to 20 minutes

(4) It /means that the trigger starts at the start time, and then it is triggered every fixed time. For example, if you use 5/20 in the Minutes field, it means trigger once every 5 minutes, and trigger once at 25, 45, etc

(5) ,Indicates that the enumeration value is listed.
For example: using 5,20 in the Minutes field means that it will be triggered every minute at 5 and 20 minutes.

(6) It Lmeans that at the end, it can only appear in the DayofMonth and DayofWeek fields.
If you write L in DayofMonth, it means the last day of the month, if you write L in DayofWeek, it means the last day of each week (Saturday).
If 5L is used in the DayofWeek field, it means it will be triggered on the last Thursday.

(7) It Wmeans the last valid working day (Monday to Friday), which can only appear in the DayofMonth field, and the system will trigger the event on the nearest valid working day from the specified date.

For example: Use 5W in DayofMonth, if the 5th is Saturday, it will be triggered on the nearest working day: Friday, that is, the 4th. If the 5th is Sunday, it will be triggered on the 6th (Monday); if the 5th is a day from Monday to Friday, it will be triggered on the 5th. Another point, W's recent search will not span the month.

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

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

4. Example

" 0 0 12 * * ?" Triggered at 12 noon every day.
" 0 15 10 ? * *" Fires at 10:15 am every day.
" 0 15 10 * * ?" Fires at 10:15 am every day.
" 0 15 10 * * ? *" Fires at 10:15 am every day.
" 0 15 10 * * ? 2005" Triggered at 10:15 am every day in 2005.
" 0 * 14 * * ?" Is triggered every 1 minute from 2 pm to 2:59 pm every day.
" 0 0/5 14 * * ?" Is triggered every 5 minutes from 2 pm to 2:55 pm every day.
" 0 0/5 14,18 * * ?" Is triggered every 5 minutes from 2 pm to 2:55 pm and from 6 pm to 6:55 pm every day.
" 0 0-5 14 * * ?" Is triggered every 1 minute from 2 pm to 2:05 pm every day.
" 0 10,44 14 ? 3 4" Fires at 2:10 and 2:44 pm on Wednesday in March every year.
" 0 15 10 ? * MON-FRI" Triggered at 10:15 AM from Monday to Friday.
" 0 15 10 15 * ?" Fires at 10:15 am on the 15th of each month.
" 0 15 10 L * ?" Triggered at 10:15 am on the last day of each month.
" 0 15 10 ? * 6L" Triggered at 10:15 am on the last Friday of each month.
" 0 15 10 ? * 6L 2002-2005" Fires at 10:15 am on the last Friday of each month from 2002 to 2005.
" 0 15 10 ? * 6#3" Fires at 10:15 am on the third Friday of every month.

Guess you like

Origin blog.csdn.net/qq_44757034/article/details/111560248