使用spring整合Quartz實現—定時器(Maven項目作演示)html
不基於特定的基類的方法java
一,開發環境以及依賴的jar包
mysql
Spring 4.2.6.RELEASEspring
Maven 3.3.9sql
Jdk 1.7express
Idea 15.04apache
二,不可少的jar依賴(添加在maven項目裏面的pom.xml文件裏面)spring-mvc
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version>
</dependency>
三,實現定時器時使用到的文件:mybatis
planWorkExcute.java --定時器執行的類mvc
spring-plan.xml --配置定時器信息的xml
四,實現定時器步驟:
1,建立 planWorkExcute.java文件 ,在 cc.royao.plantask 包下。
package cc.royao.plantask; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.log4j.Logger;//能夠刪除 import org.springframework.beans.factory.annotation.Autowired; public class PlanWorkExecute { Logger logger = Logger.getLogger(this.getClass());//logger打印日誌,可以去掉 /** * 定時器執行的方法 */ public synchronized void withdrawNoAuditTask() { SimpleDateFormat outFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); System.out.println("開始提現免審覈任務-------------------------------" + outFormat.format(new Date())); logger.info("開始提現免審覈任務-------------------------------"); System.out.println("結束提現免審覈任務-------------------------------" + outFormat.format(new Date())); logger.info("結束提現免審覈任務-------------------------------"); } }
2,建立spring-plan.xml 配置文件 注:建立一個定時器的配置文件就行,若是須要多個定時器,直接在spring-plan.xml添加 bean和定義定時器類的方法就行,不須要建立多個xml,
· 關於那個定時器多久執行的 Cron表達式 能夠參考: https://www.cnblogs.com/javahr/p/8318728.html
·有在線生成表達式的網址:http://cron.qqe2.com/
<?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-2.5.xsd" default-lazy-init="false"> <bean id="job1" class="cc.royao.plantask.PlanWorkExecute" /><!-- 修改成你的定時類的路徑 --> <!-- 能夠建立多個定時bean --> <bean id="jobDetail_1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="job1" /> </property> <property name="targetMethod"> <value>withdrawNoAuditTask</value><!-- 定時器類的方法名--> </property> </bean> <bean id="cronTrigger_1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="jobDetail_1" /> <!-- 這裏對應上面bean--> </property> <property name="cronExpression"> <value>0/2 * * * * ?</value><!-- 0 10 0 * * ? 天天0:10執行 --> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="cronTrigger_1" /> <!-- 每加一個定時器這裏也要加--> </list> </property> </bean> </beans>
3,須要在 applicationContext.xml 中引入 spring-plan.xml 如下代碼重點在最下
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd" default-lazy-init="true"> <!-- 加載系統properties文件配置 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>WEB-INF/jdbc.properties</value> <!-- <value>WEB-INF/sms.properties</value> --> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>${jdbc.driverClass}</value> </property> <!--<property name="defaultAutoCommit" value="false"/>--> <property name="url"> <value>jdbc:mysql://192.168.14.239:3306/test?useUnicode=true&characterEncoding=utf-8</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> <property name="maxActive"> <value>20</value> </property> <property name="maxIdle"> <value>60</value> </property> <property name="maxWait"> <value>20000</value> <!-- 0 --> </property> <property name="removeAbandoned"> <value>true</value> </property> <property name="removeAbandonedTimeout"> <value>6000000</value> <!-- 180 --> </property> <!-- add --> <property name="validationQuery" value="SELECT 1"></property> <property name="testWhileIdle" value="true"></property> <property name="testOnBorrow" value="true"></property> <property name="timeBetweenEvictionRunsMillis" value="3600000"></property> <property name="numTestsPerEvictionRun" value="50"></property> <property name="minEvictableIdleTimeMillis" value="120000"></property> <!-- add --> </bean> <!-- SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="1"/> <property name="maxPoolSize" value="10"/> <property name="keepAliveSeconds" value="300"/> <property name="queueCapacity" value="50"/> <property name="WaitForTasksToCompleteOnShutdown" value="true"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--<!– 自動掃描service實現 –>--> <!--<context:component-scan base-package="com.royao">--> <!--<context:include-filter type="regex"--> <!--expression="com.royao.services.*" />--> <!--</context:component-scan>--> <aop:config proxy-target-class="true"> <aop:pointcut id="serviceOperation" expression="execution(* cc.royao.mana.auth.service.*.impl.*ServiceImpl.*(..))"/> <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/> </aop:config> <!-- 配置事務通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" rollback-for="Exception"/> </tx:attributes> </tx:advice> <tx:advice id="transactionManagerAdivice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*insert*" propagation="REQUIRED"/> <tx:method name="*add*" propagation="REQUIRED"/> <tx:method name="*update*" propagation="REQUIRED"/> <tx:method name="*Update*" propagation="REQUIRED"/> <tx:method name="*del*" propagation="REQUIRED"/> <tx:method name="*create*" propagation="REQUIRED"/> <tx:method name="doApproved" propagation="REQUIRED"/> <tx:method name="batchDelFm" propagation="REQUIRED"/> <tx:method name="editTemplate" propagation="REQUIRED"/> <tx:method name="dummyDelete" propagation="REQUIRED"/> <tx:method name="batchDelUser" propagation="REQUIRED"/> <!--<tx:method name="*" propagation="REQUIRED"/>--> </tx:attributes> </tx:advice> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage"> <value>cc.royao.mana.auth.mapper.*</value> </property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <import resource="application-servlet.xml"/>
<!-- 重點在這裏 ,我把整個xml文件內容複製出來,怕大家不知道插入在哪裏-->
<import resource="spring-plan.xml"/>
</beans>
這樣就ok了,有不懂的地方能夠加做者的QQ1983127490