The process of configuring Quartz in Spring:
1. Import JAR package
The JAR package required by quartz is already included in spring, located in quartz-all-1.6.1.jar under /lib/quartz in the directory after spring decompression.
Just copy it to the project's WEB-INF/lib.
2. Configure web.xml to load the quartz configuration file when spring starts
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-*.xml</param-value> </context-param> <!-- spring listening--> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
3. Write quartz configuration file: application-quartz.xml In fact, these configurations can be written in other spring configuration files.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- Task Entity--> <bean id="testTaskBean" /> <!-- Timed Update Policy Task Method--> <bean id="testTaskTimerMethod" > <property name="targetObject" ref="testTaskBean" /> <!-- targetMethod Method name for configuring timing execution --> <property name="targetMethod" value="executeAction" /> <property name="concurrent" value="false" /> </bean> <!-- Timed update policy trigger--> <bean id="testTaskTrigger" > <property name="jobDetail" ref="syncPolicyTaskTimerMethod" /> <!-- Execute every 3 minutes of 0th second-> <property name="cronExpression" value="0 0/3 * * * ?" /> </bean> <!-- Custom task list--> <bean id="scheduler" > <property name="triggers"> <list> <ref bean="testTaskTrigger" /> </list> </property> </bean> </beans>
4. Write a JAVA class TestTask that executes tasks
package com.jp.task; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class TestTask extends QuartzJobBean{ Logger log = Logger.getLogger( PolicyServiceTest.class ); public void executeAction(){ log.info("Hello quartz"); } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { } } 5. All work is ready and run. Basically, it was reported an error. It's about a NoSuchMethod error.
This is due to the low version of commons-collections.jar. Need to commons-collections-3.2.jar or above.
At this time, you are looking for a commons-collections-3.2.jar in the project. What's going on? If you look for it again, you will find that there is a commons-collections-2.1.1.jar in hibernate.
Just delete commons-collections-2.1.1.jar.
PS: cronExpression--Cron expression description
The Cron trigger utilizes a series of special characters, as shown below:
The backslash (/) character indicates the incremental value. For example, in the Seconds field "5/15" means starting from the fifth second, once every 15 seconds.
Question mark (?) characters and letter L characters are only available in the Date of Month and Date of Week fields. A question mark indicates that this field does not contain specific values. Therefore, if you specify a date within the month, you can insert "?" in the date within the week to indicate that the date value within the week does not matter. The letter L character is the abbreviation of last. Put it in the Intramonth Date field, which means that it is scheduled to be executed on the last day of the month. In the In-week Date field, if "L" exists alone, it is equal to "7", otherwise it represents the last instance of the in-week date in the month. Therefore, "0L" means that it is scheduled to be executed on the last Sunday of the month.
The letter (W) character in the Date field within the month schedules execution to the working day closest to the specified value. Putting "1W" in the month date field means that execution is scheduled within the first working day of the month.
The pound sign (#) character specifies a specific working day instance for a given month. Putting "MON#2" in the Intraweek Date field means scheduling the task on the second Monday of the month.
The asterisk (*) character is a wildcard character that means that the field can accept any possible values.
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, - * /
Expression meaning
"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 every day at 10:15 am
"0 15 10 * * ? 2005" triggered at 10:15 am every day in 2005
"0 * 14 * * ?" triggers every 1 minute between 2pm and 2:59pm
"0 0/5 14 * * ?" Triggered every 5 minutes between 2pm and 2:55pm every day
"0 0/5 14,18 * * ?" Triggered every 5 minutes between 2:55 pm and between 6:55 pm and between 6:55 pm and every 5 minutes between
"0 0-5 14 * * ?" Triggered every 1 minute between 2 pm and 2:05 pm every day
"0 10,44 14 ? 3 WED" triggers every Wednesday at 2:10 and 2:44 pm
"0 15 10 ? * MON-FRI" triggered from 10:15 am Monday to Friday
"0 15 10 15 * ?" Triggered at 10:15 am on the 15th of every 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" Triggered at 10:15 am on the last Friday of each month from 2002 to 2005
"0 15 10 ? * 6#3" Triggers every day at 10:15 am on the third Friday of each month at 6 am
0 6 * * *
Every two hours
0 */2 * * *
Every two hours between 11pm and 8am, 8am
0 23-7/2, 8 * * *
On the 4th of each month and from Monday to Wednesday every week at 11 a.m.
0 11 4 * 1-3
January 1 at 4 a.m.
0 4 1 1 *