标签: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
原文地址:http://jianboli.blog.51cto.com/12075002/1982705