使用spring整合Quartz實(shí)現(xiàn)—定時(shí)器功能
使用spring整合Quartz實(shí)現(xiàn)—定時(shí)器(Maven項(xiàng)目做演示)
不基于特定的基類(lèi)的方法
一,開(kāi)發(fā)環(huán)境以及依賴(lài)的jar包
Spring 4.2.6.RELEASE
Maven 3.3.9
Jdk 1.7
Idea 15.04
二,不可少的jar依賴(lài)(添加在maven項(xiàng)目里面的pom.xml文件里面)
<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>
三,實(shí)現(xiàn)定時(shí)器時(shí)使用到的文件:
planWorkExcute.java --定時(shí)器執(zhí)行的類(lèi)
spring-plan.xml --配置定時(shí)器信息的xml
四,實(shí)現(xiàn)定時(shí)器步驟:
1,創(chuàng)建 planWorkExcute.java文件 ,在 cc.royao.plantask 包下?! ?br />
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打印日志,可以去掉 /** * 定時(shí)器執(zhí)行的方法 */ public synchronized void withdrawNoAuditTask() { SimpleDateFormat outFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); System.out.println("開(kāi)始提現(xiàn)免審核任務(wù)-------------------------------" + outFormat.format(new Date())); logger.info("開(kāi)始提現(xiàn)免審核任務(wù)-------------------------------"); System.out.println("結(jié)束提現(xiàn)免審核任務(wù)-------------------------------" + outFormat.format(new Date())); logger.info("結(jié)束提現(xiàn)免審核任務(wù)-------------------------------"); } }
2,創(chuàng)建spring-plan.xml 配置文件 注:創(chuàng)建一個(gè)定時(shí)器的配置文件就行,如果需要多個(gè)定時(shí)器,直接在spring-plan.xml添加 bean和定義定時(shí)器類(lèi)的方法就行,不需要?jiǎng)?chuàng)建多個(gè)xml,
· 關(guān)于那個(gè)定時(shí)器多久執(zhí)行的 Cron表達(dá)式 可以參考:http://www.dbjr.com.cn/article/138900.htm
·有在線(xiàn)生成表達(dá)式的網(wǎng)址: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" /><!-- 修改為你的定時(shí)類(lèi)的路徑 --> <!-- 可以創(chuàng)建多個(gè)定時(shí)bean --> <bean id="jobDetail_1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="job1" /> </property> <property name="targetMethod"> <value>withdrawNoAuditTask</value><!-- 定時(shí)器類(lèi)的方法名--> </property> </bean> <bean id="cronTrigger_1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="jobDetail_1" /> <!-- 這里對(duì)應(yīng)上面bean--> </property> <property name="cronExpression"> <value>0/2 * * * * ?</value><!-- 0 10 0 * * ? 每天0:10執(zhí)行 --> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="cronTrigger_1" /> <!-- 每加一個(gè)定時(shí)器這里也要加--> </list> </property> </bean> </beans>
3,需要在 applicationContext.xml 中引入 spring-plan.xml 以下代碼重點(diǎn)在最下
<?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"> <!-- 加載系統(tǒng)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> <!--<!– 自動(dòng)掃描service實(shí)現(xiàn) –>--> <!--<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> <!-- 配置事務(wù)通知 --> <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"/> <!-- 重點(diǎn)在這里 ,我把整個(gè)xml文件內(nèi)容復(fù)制出來(lái),怕你們不知道插入在哪里--> <import resource="spring-plan.xml"/> </beans>
總結(jié)
以上所述是小編給大家介紹的使用spring整合Quartz實(shí)現(xiàn)—定時(shí)器功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Springboot集成定時(shí)器和多線(xiàn)程異步處理操作
- Spring Boot定時(shí)器創(chuàng)建及使用解析
- Spring Boot 2 整合 QuartJob 實(shí)現(xiàn)定時(shí)器實(shí)時(shí)管理功能
- SpringBoot集成ElaticJob定時(shí)器的實(shí)現(xiàn)代碼
- SpringBoot 動(dòng)態(tài)定時(shí)器的使用方法
- 詳解spring batch的使用和定時(shí)器Quart的使用
- 關(guān)于spring中定時(shí)器的使用教程
- java Quartz定時(shí)器任務(wù)與Spring task定時(shí)的幾種實(shí)現(xiàn)方法
- Java中Spring使用Quartz任務(wù)調(diào)度定時(shí)器
- Spring整合Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)器的示例代碼
- JAVA中 Spring定時(shí)器的兩種實(shí)現(xiàn)方式
- 淺析spring定時(shí)器的使用
相關(guān)文章
Java實(shí)例講解多態(tài)數(shù)組的使用
本文章向大家介紹Java多態(tài)數(shù)組,主要包括Java多態(tài)數(shù)組使用實(shí)例、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下2022-05-05postman中實(shí)現(xiàn)傳遞@RequestBody參數(shù)
這篇文章主要介紹了postman中實(shí)現(xiàn)傳遞@RequestBody參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java Synchronize下的volatile關(guān)鍵字詳解
這篇文章主要介紹了Java Synchronize下的volatile關(guān)鍵字詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Spring Boot應(yīng)用監(jiān)控的實(shí)戰(zhàn)教程
Spring Boot 提供運(yùn)行時(shí)的應(yīng)用監(jiān)控和管理功能,下面這篇文章主要給大家介紹了關(guān)于Spring Boot應(yīng)用監(jiān)控的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05SpringBoot+Mybatis項(xiàng)目使用Redis做Mybatis的二級(jí)緩存的方法
本篇文章主要介紹了SpringBoot+Mybatis項(xiàng)目使用Redis做Mybatis的二級(jí)緩存的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Java 實(shí)戰(zhàn)項(xiàng)目錘煉之在線(xiàn)蛋糕商城系統(tǒng)的實(shí)現(xiàn)
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+jsp+jdbc+mysql實(shí)現(xiàn)一個(gè)在線(xiàn)蛋糕商城系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11IDEA設(shè)置maven修改settings.xml配置文件無(wú)法加載倉(cāng)庫(kù)的解決方案
這篇文章主要介紹了IDEA設(shè)置maven修改settings.xml配置文件無(wú)法加載倉(cāng)庫(kù)的解決方案,幫助大家更好的利用IDEA進(jìn)行JAVA的開(kāi)發(fā)學(xué)習(xí),感興趣的朋友可以了解下2021-01-01