Springを使用してQuartzを統合してタイマーを実装します(デモンストレーションのためにMavenプロジェクト)
特定の基本クラスに基づいていない方法
1。開発環境と依存関係のJARパッケージ
春4.2.6。リース
Maven 3.3.9
JDK 1.7
アイデア15.04
2.不可欠なJAR依存関係(Mavenプロジェクトのpom.xmlファイルに追加)
<Dependency> groupId> org.springframework </groupid> <artifactid> spring-context-support </artifactid> <version> 4.2.6.Release </version> </dependency> <seplency> <groupd> org.quartz-scheduler </groupid> </artifactid> </artificid> </groupid> </依存関係>
3。タイマーを実装するときに使用されるファイル:
planworkexcute.java-タイマーによって実行されるクラス
spring-plan.xml -xmlタイマー情報を構成するため
第四に、タイマーの手順を実装します。
1. CC.Royao.Plantaskパッケージの下に、PlanWorkexcute.javaファイルを作成します。
パッケージ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.util.concurrent.executorservice; import java.utorser.utorser.utorser.utorser.utorrent. org.apache.log4j.logger; // import org.springframework.beans.factory.annotation.autowired; public class planworkexecute {logger logger = logger.getlogger(this.getClass(); returectoaudittask(){simpledateFormat offormat = new simpledateFormat( "yyyyy mm month ddd ddd hh:mm:ss"); System.out.println("Start withdrawal exemption task------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- logger.info( "撤退を開始し、レビューの免除を開始します タスク - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2。Spring-Plan.xml構成ファイルを作成します注:タイマー構成ファイルを作成するだけです。複数のタイマーが必要な場合は、spring-plan.xmlに豆を追加して、タイマークラスの方法を定義します。複数のXMLを作成する必要はありません。
cronタイマーの実行期間のクロン式については、http://www.vevb.com/article/138900.htmを参照してください。
express式をオンラインで生成するためのURL:http://cron.qqe2.com/
<?xmlバージョン= "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://ww.springframework http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"default-lazy-init="false"> <bean id = "job1"/> <! - タイミングクラスへのパスを変更 - > <! <ref bean = "job1"/> </property> <property name = "targetmethod"> <balue> returnownoauditask </value> <! - タイマークラスのメソッド名 - > </パイプロパティ> </bean> <bean id = "crontrigger_1"> <プロパティ名= "jobdetail"> < <プロパティ名= "cronexpression"> <value> 0/2 * * * *?</value> <! - 0 10 0 * *?毎日0:10に実行 - > </パイプロパティ> </bean> <bean> <プロパティ名= "> <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.springfrimework 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/spring/spring/spring/spring/d.d.chema/ 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/schema/ http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework/schema/cache http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd <list> <value> web-inf/jdbc.properties </value> <! - <balue> web-inf/sms.properties </value> - > </list> </property> </bean> <bean id = "dataSource"> <property name = "driverclassname"> <value> $ {jdbc.drivercliclclass} < name = "defaultautocommit" value = "false"/> - > <property name = "url"> <value> jdbc:mysql://192.168.14.239:3306/test useunicode = true&charaterencoding = utf-8 </value> </propertion </property> <Property name = "password"> <bale> $ {jdbc.password} </value> </property> <property name = "maxactive"> <value> 20 </value> </property> <property name = "maxidle" name = "removeabandoned"> <balue> true </value> </property> <property name = "removeabandoned timeout"> <balue> 60000000 </value> <! - 180-> </property> <! - add-> <add-> <add-> <add-> < value = "true"> </property> <property name = "testonborrow" value = "true"> </property> <property name = "timevevictionrunsmillis" value = "3600000"> </property> <property name = " - > </bean> <! - sqlsessionfactory-> <bean id = "sqlsessionfactory"> <プロパティ名= "dataSource" ref = "dataSource"/> </bean> <bean id = "swreetpooltaskexecutor"> <プロパティ名= <"corepoolsize" name = "KeepalivesConds" value = "300"/> <プロパティ名= "queuecapacity" value = "50"/> <プロパティname = "waitfortaskStocompleteonshutdown" value = "true"/> </bean> <bean id = "transactionmanager"> <Propertial name = "datasource" <! - <! - < id = "service-operation" expression = "execution(*cc.royao.mana.auth.service。 Transaction-Manager = "TransactionManager"> <tx:attributes> <tx:method name = "*" lollback-for = "Exception"/> </tx:attributes> </tx:advise> <tx:advise id = "transactionmanageradivice" transaction-manager = "transactionmanager" propagation = "必須"/> <tx:method name = "*add*" propagation = "experagation ="/> <tx:method name = "*update*" propagation = "" expecation "/> <tx:method name ="*update*"propagation ="必須 " <tx:method name = "batchdelfm" propagation = "required"/> <tx:method name = "edittemplate" propagation = "experagation"/> <tx:method name = "dummydelete" propagation = "/> <tx:tx:method name =" batchdeluser "propagation ="必須= " </tx:属性> </tx:アドバイス> <bean> <プロパティ名= "basepackage"> <balue> cc.royao.mana.auth.mapper。ここでは、XMLファイルのコンテンツ全体をコピーしました。挿入する場所がわからないことを恐れています - > <リソースをインポート= "Spring-Plan.xml"/> </beans>要約します
上記は、エディターがスプリング統合クォーツを使用してタイマー関数を実装したものです。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!