Recently, it is necessary to implement scheduled execution tasks in projects, such as timed calculation of member points, calling third-party interfaces, etc. Since the project uses the spring framework, it is introduced here in conjunction with the spring framework.
Writing homework
That is, ordinary 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 specific tasks in 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"></bean><bean id="taskB"></bean><bean><bean id="taskJobA1"><property name="targetObject" ref="taskA"></property><property name="targetMethod" value="taskA1"></property><property name="concurrent" value="false"></property></bean><bean id="taskJobA2"><property name="targetObject" ref="taskA"></property><property name="targetMethod" value="taskA2"></property><property name="concurrent" value="false"></property></bean><bean id="taskJobB1"><property name="targetObject" ref="taskB"></property><property name="targetMethod" value="taskB1"></property><property name="concurrent" value="false"></property></bean><bean id="taskJobB2"><property name="targetObject" ref="taskB"></property><property name="targetMethod" value="taskB2"></property><property name="concurrent" value="false"></property></bean><bean id="taskA1Trigger"><property name="jobDetail"><ref bean="taskJobA1" /></property><property name="cronExpression"><value>0 0/1 * * * ?</value></property></bean><bean id="taskA2Trigger"><property name="jobDetail"><ref bean="taskJobA2" /></property><property name="cronExpression"><value>0 0/2 * * * ?</value></property></bean><bean id="taskB1Trigger"><property name="jobDetail"><ref bean="taskJobB1" /></property><property name="cronExpression"><value>0 0/1 * * * ?</value></property></bean><bean id="taskB2Trigger"><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>
Notice
A trigger can only trigger one job, but a job can be triggered by multiple Triggers, which will cause concurrency problems this time. In Quartz, if you don't want to execute the same job concurrently, you can implement a StatefulJob instead of a Job. If you use MethodInvokingJobDetailFactoryBean in Spring, you can do it by setting the concurrent="false" property.
End Note
The benefits of using Quartz in Spring instead of a single application include:
Putting all task scheduling settings in the same place makes tasks easy to maintain.
Job encoding is only possible, Trigger and Scheduler can be set by configuration
You can use Pojo Java Bean to execute jobs without implementing the job interface
Detailed usage of Cron expressions
Special characters allowed by field allowable values
0-59 seconds, - * /
0-59, - * /
Hours 0-23, - * /
Date 1-31, - * ? / LWC
Months 1-12 or JAN-DEC, - * /
Week 1-7 or SUN-SAT, - * ? / LC #
Year (optional) Leave blank, 1970-2099, - * /
example:
0/5 * * * * ? : Execute every 5 seconds
The "" character is used to specify all values. For example: "" means "per minute" in the field of minutes.
The "?" character is only used in the date and day of week fields. It is used to specify "non-explanatory values". It is useful when you need to specify something by one of these two domains. You will understand by looking at the example below.
The two elements of the date in the month and the date in the week are mutually exclusive together. Set a question mark to indicate that you do not want to set that field.
The "-" character is used to specify a range. For example: "10-12" means "10, 11, 12".
The "," character is used to specify an additional value. For example: "MON, WED, FRI" means "Mon, Wednesday, Friday" in the week field.
The "/" character is used to specify the increment. For example: "0/15" in the second field means 0, 15, 30 and 45 seconds per minute. "5/15" represents 5, 20, 35 and 50 per hour in the minute field. The symbol "" is before "/" (such as: /10) is equivalent to 0 before "/" (such as: 0/10). Remember one essence: each numerical field of an expression is a set of maximum and minimum values, such as: the set of seconds and minute fields is 0-59, the date fields are 1-31, and the month fields are 1-12. The character "/" can help you get the corresponding numeric value in each character field. For example: "7/6" will only be triggered in July when the month domain, and does not mean every June.
L is an omitted word for 'last' which can represent the day-of-month and day-of-week fields, but the meaning is different in the two fields, for example, the day-of-month field represents the last day of a month. If the day-of-week field means '7' or 'SAT', if the number is preceded in the day-of-week field, it means the last few days of a month, such as '6L' means the last Friday of a month.
The character "W" only allows the date field to appear. This character is used for the most recent working day of the specified date. For example: If you write "15W" in the date field, it means: the most recent working day of this month on the 15th of this month. So, if the 15th is Saturday, the mission will be triggered on the 14th. If 15 is better Sunday, the task will be triggered on Monday, that is, the 16th. If you fill in "1W" in the date field, even if the 1st is Saturday, the task will only be triggered on next Monday, that is, the 3rd. The most recent working day specified by the "W" character cannot be crossed to the month. The character "W" can only be used with a separate value, and cannot be a numeric field, such as: 1-15W is wrong.
"L" and "W" can be used in a joint manner in the date field, and LW represents the working day of the last week of the month.
The character "#" is only allowed to appear in the day of week domain. This character is used to specify a certain day of the month. For example: "6#3" means Friday of the third week of the month (6 means Friday, 3 means week 3). "2#1" means Monday of the first week of the month. "4#5" means Wednesday of the fifth week.
The character "C" is allowed to appear in the date and day of week fields. This character relies on a specified "calendar". That is to say, the value of this expression depends on the calculation result of the relevant "calendar". If there is no "calendar" association, it is equivalent to all included "calendars". For example: the date field is "5C" to indicate the first day in the associated "calendar", or the last 5 days of the first day of the month. The day of the week is "1C" which means the first day in the associated "calendar", or the next day of the first day of the week, that is, the day after Sunday (Monday).
Example expression
"0 0 12 * * ?" Triggered at 12 noon every day "0 15 10 ? * *" Triggered at 10:15 am every day "0 15 10 * * ?" Triggered at 10:15 am every day "0 15 10 * * ? *" Triggered at 10:15 am every day "0 0/5 14 * * ? 2005" Triggered at 10:15 am every day "0 * 14 * * ?" Triggered at 10:15 am every day "0 0/5 14 * * ?" Triggered at 2:15 pm to 2:59 pm every day "0 0/5 14 * * ?" Triggered at 2:15 pm to 2:55 pm every day "0 0/5 14,18 * * ?" Triggered at 5 minutes every day from 2:15 pm to 2:55 pm every day "0 0/5 14,18 * * ?" 0 0-5 14 * * ?" 0 10,44 14 ? 3 WED" 0 10 10 10 10 Wednesdays of March each year at 2:10 and 2:44 pm each year at 2:10 and 2:44 pm each year at 2:10 and 2:44 pm each year at 2:10 and 2:44 pm each year at 15 10 ? * MON-FRI" 0 15 10 15 * ?" 0 15 10 15 * ?" 0 15 10 15 10 15 * ?" 0 15 15 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1 10 ? * 6L 2002-2005" Triggered at 10:15 am on the last Friday of each month from 2002 to 2005 "0 15 10 ? * 6#3" Triggered at 10:15 am on the third Friday of each month
The above article Spring integrates Quartz to implement timing task scheduling is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.