第一步:引入依賴web
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.0</version>
</dependency>spring
注意:Spring3.1如下的版本必須使用quartz1.x系列,3.1以上的版本才支持quartz 2.x
個人spring版本是3.1以上的,因此這裏引入2.X。這裏須要注意下你的spring-context和spring-context-support已經導入且版本>3.1緩存
爲何導入spring-context-support,解釋以下:
<!--
含支持UI模版(Velocity,FreeMarker,JasperReports),郵件服務,腳本服務(JRuby),緩存Cache(EHCache),
任務計劃Scheduling(quartz)方面的類。外部依賴spring-context, (spring-jdbc, Velocity, FreeMarker,
JasperReports, BSH, Groovy, JRuby, Quartz, EHCache)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>測試
第二步:建立spring-quart.xmlspa
<?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-3.0.xsd">
<!--定時器一:測試定時任務 1分鐘-->
<!--定時任務所在類路徑-->
<bean name="gpOrderFlush" class="com.service.UserService"/>
<bean id="jobDetail_1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="gpOrderFlush" />
</property>
<!--定時任務方法 本實例爲UserService.task( )方法-->
<property name="targetMethod">
<value>task</value>
</property>
<property name="concurrent" value="false" />
</bean>
<!-- 若是quartz的版本 <2 那麼此處是 org.springframework.scheduling.quartz.CronTriggerBean -->
<bean id="cronTrigger_1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobDetail_1" />
</property>
<!--定時任務間隔時間,此處爲一分鐘-->
<property name="cronExpression">
<value>0 0/1 * * * ?</value>
</property>
</bean>xml
<!-- 啓動工做 -->
<bean id="SpringJobSchedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--定時任務一啓動-->
<ref bean="cronTrigger_1" />
</list>
</property>
<property name="autoStartup" value="true"></property>
</bean>
</beans>blog
第三步:在web.xml中引入ci
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-quartz.xml
</param-value>
</context-param>get
定時任務到此已經配置好了,啓動項目觀察控制檯:it