使用spring整合quartz實(shí)現(xiàn)—定時(shí)器(maven項(xiàng)目做演示)
不基于特定的基類的方法
一,開(kāi)發(fā)環(huán)境以及依賴的jar包
spring 4.2.6.release
maven 3.3.9
jdk 1.7
idea 15.04
二,不可少的jar依賴(添加在maven項(xiàng)目里面的pom.xml文件里面)
1
2
3
4
5
6
7
8
9
10
|
<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í)行的類
spring-plan.xml --配置定時(shí)器信息的xml
四,實(shí)現(xiàn)定時(shí)器步驟:
1,創(chuàng)建 planworkexcute.java文件 ,在 cc.royao.plantask 包下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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í)器類的方法就行,不需要?jiǎng)?chuàng)建多個(gè)xml,
· 關(guān)于那個(gè)定時(shí)器多久執(zhí)行的 cron表達(dá)式 可以參考:http://www.ythuaji.com.cn/article/94843.html
·有在線生成表達(dá)式的網(wǎng)址:http://cron.qqe2.com/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<?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í)類的路徑 --> <!-- 可以創(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í)器類的方法名--> </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)在最下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
<?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ù)器之家網(wǎng)站的支持!
原文鏈接:https://www.cnblogs.com/thirteen-zxh/archive/2018/04/25/8943765.html