Spring integrates Quartz to realize the method of timing task scheduling

  • 2020-05-17 05:35:16
  • OfStack

In the recent project, it is necessary to perform tasks at a fixed time, such as calculating members' credits at a fixed time and calling the third party interface, etc. Since the project adopts the spring framework, it is introduced here in combination with the spring framework.

Write a job class

Common pojo, as follows:


package com.pcmall.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TaskA {
	private static Logger logger = LoggerFactory.getLogger(TaskA.class);
	public void taskA1(){
		for(int i=0;i<100;i++){
			System.out.println("----A1----" + i);
		}
	}
	public void taskA2(){
		for(int i=0;i<100;i++){
			System.out.println("----A2----" + i);
		}
	}
}

Set up specific tasks in the spring configuration file


<?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="taskA" class="com.pcmall.task.TaskA"></bean>
	<bean id="taskB" class="com.pcmall.task.TaskB"></bean>
	
	<bean id="taskJobA1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="taskA"></property>
		<property name="targetMethod" value="taskA1"></property>
		<property name="concurrent" value="false"></property>
	</bean>
	<bean id="taskJobA2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="taskA"></property>
		<property name="targetMethod" value="taskA2"></property>
		<property name="concurrent" value="false"></property>
	</bean>
	<bean id="taskJobB1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="taskB"></property>
		<property name="targetMethod" value="taskB1"></property>
		<property name="concurrent" value="false"></property>
	</bean>
	<bean id="taskJobB2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="taskB"></property>
		<property name="targetMethod" value="taskB2"></property>
		<property name="concurrent" value="false"></property>
	</bean>
	
	<bean id="taskA1Trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="taskJobA1" />
		</property>
		<property name="cronExpression">
			<value>0 0/1 * * * ?</value>
		</property>
	</bean>
	<bean id="taskA2Trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="taskJobA2" />
		</property>
		<property name="cronExpression">
			<value>0 0/2 * * * ?</value>
		</property>
	</bean>
	
	<bean id="taskB1Trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="taskJobB1" />
		</property>
		<property name="cronExpression">
			<value>0 0/1 * * * ?</value>
		</property>
	</bean>

	<bean id="taskB2Trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="taskJobB2" />
		</property>
		<property name="cronExpression">
			<value>0 0/2 * * * ?</value>
		</property>
	</bean>

	<bean id="scheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="taskA1Trigger" />
				<ref bean="taskA2Trigger" />
				<ref bean="taskB1Trigger" />
				<ref bean="taskB2Trigger" />
			</list>
		</property>
	</bean>
</beans>

Pay attention to

One trigger can only trigger one Job, but one Job can have multiple Trigger triggers, which can cause concurrency problems. In Quartz, if you don't want to execute one and the same Job concurrently, you can implement StatefulJob instead of Job. If you use MethodInvokingJobDetailFactoryBean in Spring, you can do this by setting concurrent="false".

endnotes

The benefits of using Quartz in Spring instead of a single application include:

Keeping all task scheduling Settings in one place makes the task easy to maintain.

Only Job is encoded, Trigger and Scheduler can be configured

You can perform job using Pojo Java Bean without implementing the Job interface

Detailed usage of Cron expressions

The special character allowed by the field allowed value

Minus 59 seconds, minus PI times PI over PI
0 minus 59, minus PI over PI
Hours 0 minus 23, minus * /
Dates 1-31, - * ? / L W C
January 1-12 or JAN-DEC, minus * /
Week 1-7 or SUN-SAT, - * ? / L C #
Year (optional) left blank, 1970-2099, - * /

Example:

0/5 * * * * ? : execute once every 5 seconds

The "" character is used to specify all values. "" means "per minute" in the minute field.

"The & # 63;" Characters are used only in the date and week fields. It is used to specify "non-explicit values". This is useful when you need to specify 1 by 1 in either of these fields. Look at the following example and you will see.

The date in the month and the date in the week should be mutually exclusive by setting a question mark to indicate which field you do not want to set.

The "-" character is used to specify a range. For example, "10-12" in the small time domain means "10 o 'clock, 11 o 'clock, 12 o 'clock".

The ", "character is used to specify additional values. For example, "MON,WED,FRI" means "week 1. Week 3. Week 5" in the week field.

The "/" character is used to specify deltas. For example, "0/15" in the second domain means 0, 15, 30 and 45 seconds per minute. "5/15" in the minute domain represents 5, 20, 35, and 50 per hour. The symbol "" before"/" (e.g., /10) is equivalent to 0 before "/" (e.g., 0/10). Remember the essence of 1: the range of each number in the expression is a set with a maximum and a minimum value. For example, the range of seconds and minutes is 0-59, the range of dates is 1-31, and the range of months is 1-12. The character "/" helps you fetch the appropriate value in each character field. For example, "7/6" in the month domain is only triggered when it is in July, not every June.

L is an ellipsis of 'last' to indicate the day-of-month and day-of-week fields, but has a different meaning in the two fields. For example, day-of-month fields represent the last day of a month. If you prefix the day-of-week field to mean '7' or' SAT', if you prefix the day-of-week field to mean the last days of the month, for example '6L' to mean the last week of the month.

The character "W" allows only date fields. This character is used to specify the last working day of the date. For example, if you write "15W" in the date field, it means the last working day on the 15th of the month. So, if week 15 is week 6, the task will be triggered on the 14th. If 15 is Sunday, the task will be triggered on the first of the week, which is the 16th. If you fill in "1W" in the date field, even if the 1st is week 6, then the task will only be triggered on the next Monday, that is, the 3rd. The most recent working day specified by the "W" character cannot span the month. The character "W" can only be used with a single value, not a single number field. For example, 1-15W is wrong.

"L" and "W" can be used together in the date field, with LW representing the working week of the last week of the month.

The character "#" is only allowed in the week fields. This character is used to specify the day of the month. For example, "6#3" means week 5 of the third week of the month (6 means week 5, 3 means week 3). "2#1" represents the first day of the first week of the month. "4#5" represents week 3 of week 5.

The character "C" is allowed in the date and week fields. This character depends on a specified "calendar". In other words, the value of this expression depends on the calculation result of the relevant "calendar", and if there is no "calendar" correlation, it is equivalent to all the "calendars" included. For example, the date field is "5C" to indicate the 1st day in the associated "calendar", or the 5th day after the 1st day of the month. The week field is "1C" for day 1 in the associated "calendar", or day 1 after day 1 of the week, i.e., day 1 after Sunday (week 1).

Expression example


"0 0 12 * * ?"  Every day at noon 12 Some trigger 
"0 15 10 ? * *"  Every morning 10:15 The trigger 
"0 15 10 * * ?"  Every morning 10:15 The trigger 
"0 15 10 * * ? *"  Every morning 10:15 The trigger 
"0 15 10 * * ? 2005" 2005 Every morning in 1997 10:15 The trigger 
"0 * 14 * * ?"  Every afternoon 2 Point to the afternoon 2:59 During each of the 1 Minutes to trigger 
"0 0/5 14 * * ?"  Every afternoon 2 Point to the afternoon 2:55 During each of the 5 Minutes to trigger 
"0 0/5 14,18 * * ?"  Every afternoon 2 Point to the 2:55 During and in the afternoon 6 Point to the 6:55 During each of the 5 Minutes to trigger 
"0 0-5 14 * * ?"  Every afternoon 2 Point to the afternoon 2:05 During each of the 1 Minutes to trigger 
"0 10,44 14 ? 3 WED"  Every year, 3 On the week 3 In the afternoon 2:10 and 2:44 The trigger 
"0 15 10 ? * MON-FRI"  weeks 1 To a week 5 In the morning 10:15 The trigger 
"0 15 10 15 * ?"  A month 15 The morning of 10:15 The trigger 
"0 15 10 L * ?"  Finally a month 1 Day morning 10:15 The trigger 
"0 15 10 ? * 6L"  End of the month 1 A few weeks 5 In the morning 10:15 The trigger  
"0 15 10 ? * 6L 2002-2005" 2002 - 2005 The end of each month of the year 1 A few weeks 5 In the morning 10:15 The trigger 
"0 15 10 ? * 6#3"  Every month of the first 3 A few weeks 5 In the morning 10:15 The trigger 

Related articles: