Spring+Quartz配置定時任務實現代碼
作為一個優秀的開源調度框架,Quartz 具有以下特點:
強大的調度功能,例如支持豐富多樣的調度方法,可以滿足各種常規及特殊需求;
靈活的應用方式,例如支持任務和調度的多種組合方式,支持調度數據的多種存儲方式;
分布式和集群能力,Terracotta 收購后在原來功能基礎上作了進一步提升。
另外,作為 Spring 默認的調度框架,Quartz 很容易與 Spring 集成實現靈活可配置的調度功能。
代碼如下
1、
<bean class='org.springframework.scheduling.quartz.SchedulerFactoryBean'> <property name='triggers'> <list> <ref local='createFileAndStuffTrigger'/> </list> </property> </bean>
2、
<bean class='org.springframework.scheduling.quartz.SimpleTriggerBean'> <property name='startDelay'><value>5000</value></property> <property name='repeatCount'><value>-1</value></property> <property name='repeatInterval'><value>36000000</value></property> <property name='jobDetail'><ref bean='createFileAndStuffTask' /></property> </bean>
3、
<bean class='org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean'> <property name='targetObject'> <ref bean='jobService' /> <!--目標Job--> </property> <property name='targetMethod'> <value>doCreate</value> <!--目標方法--> </property> <property name='concurrent'> <value>false</value> <!--定時任務串行--> </property> </bean>
4、
<bean class='com.task.CreateFileAndStuff'></bean>
5、
在CreateFileAndStuff.Java
/** * 開始生成 */ public synchronized void doCreate(){if ('yes'.equals(ConfigUtil.createFileAndSuffSwitch())) { List<Map<String ,Object>> switchDList=this.getBusInfo(); if(null==switchDList || 0==switchDList.size()) return; this.doCreateForLoopSwitch(switchDList,one_number); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: