1. Release Notes
Spring 4.0.0
Quartz 2.2.1
Springmvc 4.0.0
2.Quartz-dependent JAR Package
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version></dependency><dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency>
There is a spring jar package, if there is no pom.xml inside, you need to add
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.0.0.RELEASE</version></dependency>
3. Code 3.1 Updateexpressjob Task class (can inject service)
package com.tsjsr.quartz;import java.util.Date;import org.apache.bcel.generic.NEW;import org.springframework.beans.factory.annotation.Autowired;import com.tsjsr.model.FileInfo;import com.tsjsr.service.FileInfoServiceI;public class UpdateExpressJob {// @Autowired// private FileInfoServiceI fileInfoService; protected void execute() { System.out.println(new Date()); } }
Configuring Quartz tasks in 3.2 spring.xml
<!--Quartz--<!--======================== Configuration Task Bean class ========================--<bean id= "Spri Ngqtzjob "class=" Com.tsjsr.quartz.UpdateExpressJob "/> <bean id=" Springqtzjobmethod "class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> <property name=" TargetObject "&G T <ref bean= "Springqtzjob"/> </property> <property name= "Targetmethod" > <!--the method name to execute -<value>execute</value> </property> </bean> <!--====== ================== Dispatch Trigger ========================-<bean id= "Crontriggerbean" class= "Org.springframework.sc Heduling.quartz.CronTriggerFactoryBean "> <property name=" jobdetail "ref=" Springqtzjobmethod "></proper ty> <property name= "cronexpression" value= "0/5 * * * * *?" ></property> </bean> <!--===================== = Dispatch Factory ========================---<bean id= "Springjobschedulerfactorybean" class= "Org.springframework.sch Eduling.quartz.SchedulerFactoryBean "> <property name=" triggers "> <list> <ref bean= "Crontriggerbean"/> </list> </property> </bean>
3.3 Running Results
4. Multiple Task configurations
The example above is a configuration for a single task. Multiple task configurations are also simple. Simply modify the configuration in the Spring.xml.
4.1 Adding a second task class UpdateExpressJob2
package com.tsjsr.quartz;public class UpdateExpressJob2 { protected void execute() { System.out.println("this is job2"); } }
4.2 Multi-tasking XML configuration
<!--Quartz--<!--======================== Configuration Task Bean class ========================-<!--first task Bea Class N--<bean id= "Springqtzjob" class= "Com.tsjsr.quartz.UpdateExpressJob"/> <bean id= "springqtzjobme Thod "class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> <property name=" tar GetObject "> <ref bean=" springqtzjob "/> </property> <property name=" target Method > <!--the name of the methods to execute--<value>execute</value> </property> </ Bean> <!--second task Bean class--<bean id= "SpringQtzJob2" class= "Com.tsjsr.quartz.UpdateExpressJob2"/> <bean id= "SpringQtzJobMethod2" class= " Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> <property name=" TargetObject "&G T <ref bean= "SpringQtzJob2"/> </property> <property NAme= "Targetmethod" > <!--the name of the method to execute--<value>execute</value> </property> </bean> <!--======================== Schedule trigger ========================-<!--the first one Scheduler-<bean id= "Crontriggerbean" class= " Org.springframework.scheduling.quartz.CronTriggerFactoryBean "> <property name=" jobdetail "ref=" Springqtzjob Method "></property> <property name=" cronexpression "value=" 0/5 * * * *? " ></property> </bean> <!--the second task's dispatch-out trigger configuration-<bean id= "CronTriggerBean2" class= "Org.s Pringframework.scheduling.quartz.CronTriggerFactoryBean "> <property name=" jobdetail "ref=" Springqtzjobmetho D2 "></property> <property name=" cronexpression "value=" 0/5 * * * * *? " ></property> </bean> <!--======================== Dispatch factory ========================-- <bean id= "SpringjoBschedulerfactorybean "class=" Org.springframework.scheduling.quartz.SchedulerFactoryBean "> <property name=" Triggers "> <list> <ref bean=" Crontriggerbean "/> <!--first task-- <ref bean= "CronTriggerBean2"/> <!--a second task--</list> </property> </bean>
4.3 Running results
5. Cronexpression description of the Dispatch trigger
expression |
Description |
"0 0 12 * *?" |
Triggered 12 o'clock noon every day |
"0 15 10?" * *" |
Triggered 10:15 daily |
"0 15 10 * *?" |
Triggered 10:15 daily |
"0 15 10 * *?" *" |
Triggered 10:15 daily |
"0 15 10 * *?" 2005 " |
2005-10:15 trigger per day |
"0 * 14 * *?" |
Triggers every 1 minutes from 2 o'clock in the afternoon to 2:59 daily |
"0 0/5 14 * *?" |
Triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily |
"0 0/5 14,18 * *?" |
Every 5 minutes from 2 o'clock in the afternoon to 2:55 daily and from 6 o'clock in the afternoon to 6:55 |
"0 0-5 14 * *?" |
Triggers every 1 minutes from 2 o'clock in the afternoon to 2:05 daily |
"0 10,44 14?" 3 WED " |
Every March, Wednesday, 2:10 and 2:44 triggers. |
"0 15 10?" * Mon-fri " |
10:15 trigger from Monday to Friday |
"0 15 10 15 *?" |
15th 10:15 per month, triggered |
"0 L *?" |
10:15 trigger on the last day of the month |
"0 15 10?" * 6L " |
Last month of Friday 10:15 Trigger |
"0 15 10?" * 6L 2002-2005 " |
2002 to 2005 the last Friday 10:15 trigger |
"0 15 10?" * 6#3 " |
The third Friday 10:15 of the month is triggered |
0 6 * * * |
Every morning at 6. |
0 /2 * * |
Every two hours |
0 23-7/2,8 * * * |
Every two hours between 11 o'clock and 8 in the morning, eight in the morning. |
0 11 4 * 1-3 |
Every month, number 4th and Monday to Sunday, three a.m., 11. |
0 4 1 1 * |
January 1 morning, 4. |
Spring4.0 Integrated Quartz2.2.1