前提注意:配置文件中如果 default-lazy-init="true",删掉或设置成false,不然注解会失效(这个坑找了好久)。% l9 l# V% E5 l: O6 w& L
一、说明
! U! y3 X. G. Q# `% d+ b( \+ F
4 h4 w( y8 E7 s/ j 以前项目一直使用Quartz的定时任务,虽然其功能强大,但是配置文件极其复杂,并且一个class下只能执行一个方法(貌似是)。定时任务多了以后对于维护xml配置文件时一件极为头疼的事情。
0 z ~! C" P2 k: N9 H$ P1 O% O% d4 ]7 t* ]+ v1 |) ]
8 Z( ~. V0 C. w% e 前段时间把Quartz整合实例化入数据库了,做了一个任务列表,进行增删查改,的确是简单多了。在项目不重启的情况下可以对任务进行各种你想要的操作。如图所示操作:
1 g1 a& j- R5 Q' s0 f/ |
4 U, n. }0 C2 G( e
0 }" H! \. H, d" P/ R7 _. L8 k& u4 l* |3 X5 I7 G
4 g z; |" J1 s) \
但如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。
7 _* R5 Y0 |1 K$ z
, X5 _; r1 ]. c3 C( X& q: c
; ?& C' x8 R* i; }( T6 t4 F3 Y Spring Task提供两种方式进行配置,注解和配置文件。使用注解虽然简单,不用配置xml,但是相对于修改比较频繁的任务来说,打包编译的过程也是挺麻烦的,建议使用配置文件实现。
9 r. O% p% i& c9 v2 ]8 F Y& {4 H2 H/ H1 ]( m( T. M- q' {
0 O0 r3 x/ M3 A0 a二、配置5 k, w7 p; t/ Z8 @* _
1)xml配置, e# Z7 [* M. N
- <?xml version="1.0" encoding="UTF-8"?>
4 ?5 U4 ]5 }- A9 O4 T1 @! u. g9 G' W - <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
/ S7 [2 @, C& l. u# w6 L7 j m% J - xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
# }* ]' E5 c+ W6 y - xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"/ ]+ u3 N! o* s% g( _* k# `
- xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="- U4 D; O* u, }; k( b
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd3 }) ~. E1 m6 g7 R0 \. Z! p3 }0 @* \
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd2 h$ d6 v, S; ^: I( ~
- http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd- b' n/ M2 O4 q* D- R6 u2 n: X
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd* D/ j, u; y2 q% y5 j
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
2 ^ S- d6 r( z9 u" ]. P - http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd; _! K" v. ]/ L; l- c
- http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">! T" P1 Y; P- r& H k9 m5 s* {
- : ?2 b' X0 ^8 y1 W
- <description>Spring Configuration</description>
4 g, p. g- ], G; D, Z - x* z2 e' m! k: @. ~4 ^1 I/ C% g5 E
- <context:component-scan base-package="task"/>
8 T) t, p! v" V2 h- I3 d - ) h4 j( A+ Z! v+ L8 i
- <!-- 配置任务线性池 -->$ y$ U2 H- ^0 \4 G g
- <task:executor id="executor" pool-size="10" /> . n0 _- S3 G+ ~# |) q3 P' K+ C
- <task:scheduler id="scheduler" pool-size="10"/>+ w% p" ?3 G* q. ~
- <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>. |8 ^ F: T& _! N1 ]& q- p
- <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->0 w9 Y, h$ M; D6 s* v
- <!-- <task:scheduled-tasks scheduler="scheduler">
3 J7 H8 s4 W+ x% L. v. i - <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/> / }. O/ @; v) M1 A7 |
- </task:scheduled-tasks> --> + \9 @) W. b8 Y% P
- </beans>
复制代码 - [6 \- S; N8 c
2)代码实现
' r; |) ?# u/ E" `- package task;
5 b* T: ?% x0 E( K* k! ]8 p8 W; y% n
+ ?. r1 V: M* K* m( [- import org.springframework.scheduling.annotation.Scheduled;
; k+ f, J7 L. u - import org.springframework.stereotype.Component;
' c) r h/ \1 z2 a; R) ]7 X
/ U, V, B& N+ x/ n- @Component("TestJob")
5 b9 d+ l/ Y& g$ U& E - public class TestJob {* y' o6 W6 |, g; h2 c/ R8 v6 M
- @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次 4 [) I* z, P. X) w" h# E
- public void test1()3 n( p) `' a5 }- K& y# I3 Y! z
- {' } K5 B, A R3 B: L
- System.out.println("job1 开始执行");' d2 D" K& x4 [ i0 V$ Z% l7 ~
- } & M: R' X. O. q" W) {: f% d
- @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次 " e( B0 O- c% N" K8 C! S" T
- public void test2()
) _. w$ s' ?) u+ `: {& R* B! v0 q/ z - {. S D" ]: i( Q
- System.out.println("job2 开始执行");
$ p3 k, l; G6 Y$ d1 D; Z4 P - }
2 j' w# x5 c2 G* @% B( s: S - }
复制代码
m) q0 g$ l( W# j- n" l; k项目启动后运行结果:
" d3 R2 k8 h+ n0 L, W& e( v
* p& N5 `! J. @0 j1 ?9 G8 L6 L) K+ v1 _) n
! y" U0 ~ j8 C& U+ u
1 M3 b0 n Z$ ]6 n
: p) ?' a7 ]6 J) e7 L1 }* H
CSDN下载:http://download.csdn.net/detail/zhulin2012/9576741 百度云下载:http://pan.baidu.com/s/1boW6WP5 |