总结:要实现定时器quartz,我们注意两步就行了,一是写好实现类注意实现类的方法名要和配置中一致,二是做好配置。然后就可以测试了。
①定时器实现类
HealthRecodersTokenScheduler.java
1
2
3
4
|
public
class
HealthRecodersTokenScheduler {
public
void
execute()
throws
Exception {
System.err.println(
"=========定时去获取tokenId,tokenId的时效是30分钟有效========:"
);
}
|
②定时器配置xml
healthrecoders_token-scheduler.xml
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
|
<?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
1
|
<
import
resource=
"classpath:scheduler/healthrecoders_token-scheduler.xml"
/>
|
注意我们引入的依赖为:
(要注意其他地方是否也包含了quartz,因为有可能会冲突)
1
2
3
4
5
6
|
<!-- 定时器quertz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>
2.2
.
0
</version>
</dependency>
|
本文转自建波李 51CTO博客,原文链接:http://blog.51cto.com/jianboli/1982705,如需转载请自行联系原作者