quartz定時器實現
阿新 • • 發佈:2017-11-17
quartz
總結:要實現定時器quartz,我們註意兩步就行了,一是寫好實現類註意實現類的方法名要和配置中一致,二是做好配置。然後就可以測試了。
①定時器實現類
HealthRecodersTokenScheduler.java
public class HealthRecodersTokenScheduler { public void execute() throws Exception { System.err.println("=========定時去獲取tokenId,tokenId的時效是30分鐘有效========:"); }
②定時器配置xml
healthrecoders_token-scheduler.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <!-- quartz --> <bean id="healthTokenScheduler" class="com.kentrasoft.entity.healthrecodes.HealthRecodersTokenScheduler"> </bean> <!-- JobDetail --> <bean id="getTokenDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="healthTokenScheduler" /> <property name="targetMethod" value="execute" /> <!-- 是否允許任務並發執行。當值為false時,表示必須等到前一個線程處理完畢後才再啟一個新的線程 --> <property name="concurrent" value="false" /> </bean> <!-- Scheduler --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="simpleTrigger" /> </list> </property> </bean> <!-- 啟動後40秒執行一次,後面每隔25分鐘執行一次, repeatCount 是執行的次數,-1代表永久,0代表不執行,1代表執行一次 --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="getTokenDetail" /> <property name="startDelay"> <value>40000</value> </property> <property name="repeatInterval"> <value>1500000</value> </property> <property name="repeatCount"> <value>-1</value> </property> </bean> </beans>
③在ApplicationContext.xml中引入配置的定時器xml
ApplicationContext.xml
<import resource="classpath:scheduler/healthrecoders_token-scheduler.xml"/>
註意我們引入的依賴為:
(要註意其他地方是否也包含了quartz,因為有可能會沖突)
<!-- 定時器quertz --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.0</version> </dependency>
本文出自 “JianBo” 博客,請務必保留此出處http://jianboli.blog.51cto.com/12075002/1982705
quartz定時器實現