科帮网-Java论坛、Java社区、JavaWeb毕业设计

登录/注册
您现在的位置:论坛 新手区 新手教程 > SpringTask任务案例源码实现
总共48085条微博

动态微博

查看: 2012|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

    2021-2-2 11:21
  • 签到天数: 36 天

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2017-04-22 14:46:31 |显示全部楼层 |倒序浏览
    写在开始
    ' M% i1 r% d/ L- e3 T2 [+ A4 L2 m9 w一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。  q. D" |, J* `7 P
    举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。
    5 t, T( m6 _" l' [
    + w- A. |2 @$ P5 `任务介绍
    6 `! r+ ~! W5 S就目前自己接触的定时人来来说,这里简单的聊下一下三种:) p; y- {5 ?0 V: ]" I+ s9 O# B

    ) a1 t5 P/ R( j: {1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。
    2 @1 q* v8 K8 Q6 N
    $ T0 Z- L2 L! l2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)
    7 `  T$ W) |, G/ u0 q1 {. J- I) R! Y2 b0 ^; K0 X* Z* X3 E
    3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。& p) O# K$ a; d; `- f

    1 C/ H9 j$ h( @$ J: s使用案例  e. T& @4 J" K) @1 h- w
    关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。# r" R$ u! ?: B  i
    , N0 H! ~6 z0 g# k' ?. I
    这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。1 {5 H; p7 G& ?7 `% s

    0 q! f1 O% s# B3 z+ E4 IspringTask提供了基于xml配置和注解的两种实现方式。, U9 e7 H! o" f5 U' @

    . t' z9 H$ G3 l9 F1 X- z( _7 ^基于注解的实现TestJob.java:( q& c( Q1 t# f8 a- o2 [" _! K
    1. package task;
      6 O7 D! S5 I  d. n  P  p% c% k
    2. 6 z2 [9 P- ]5 T4 b6 A8 W2 v. L5 f
    3. import org.springframework.scheduling.annotation.Scheduled;
      ) H' C/ A* G) P/ ]5 E/ w2 T: q" B2 @
    4. import org.springframework.stereotype.Component;. x8 k3 K+ r0 w8 Y. y
    5. /**4 u4 ?( A5 e9 h- e% B. c- i) b
    6. * 任务测试 科帮网 http://www.52itstyle.top/& R, ]4 }# l) s
    7. * 创建者        张志朋
      ! }: {7 b1 j$ B# o7 i+ W
    8. * 创建时间        2017年4月22日& \0 R  ~4 X2 t' s5 W  E& @# l. l
    9. *6 ^2 r) `  C# r
    10. */3 A* G% m" @; o- p4 j: T
    11. @Component("TestJob")  / y0 c/ ^* k) `- i0 l+ Q
    12. public class TestJob {! D' N: I. V. t9 N3 `0 p5 O
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      8 E1 F# Q; c% ~9 |3 f4 {' o  `
    14.     public void test1()
      , q, r; n8 B' X+ \9 t5 K
    15.     {4 j0 [# @& r* P- o
    16.         System.out.println("job1 开始执行");+ ?' h3 c' i+ n% O! J, }1 o$ |' R
    17.     }
      # G- z9 r; L: B" f) I
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      ! ?1 _6 h3 C% R& X& s
    19.     public void test2()! P( `$ t" a7 ]; X1 i1 w5 q9 a& v
    20.     {
      7 N- E/ H) S1 T. t
    21.         System.out.println("job2 开始执行");! T: W- ^. P) L
    22.     } , s$ k) b2 H6 \0 P
    23. }$ Y6 n6 u4 c( @0 C! J4 I$ d; N
    复制代码

    - H6 R& @5 H) z. k1 X$ O- s0 p3 p3 T4 X! I
    基于注解的实现spring-context.xml:- d/ O! n& B; Y$ x# v  a# |# Q
    1. <?xml version="1.0" encoding="UTF-8"?>1 q9 D8 h, F0 D" D5 R+ k
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"7 w* `+ y8 o7 a5 {
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      / J' V( B4 o2 t* ~, H8 ~
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"2 c4 v6 V) a" Z5 c+ w' H* L1 W
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="( |. M! [* Q8 z1 p
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      $ O. _$ }4 ?: T0 x1 p
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd3 ^8 Q* U7 V( N7 t9 ^
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd0 ~) j/ c; r1 g2 j8 H
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd: }2 t8 q  a$ U2 O) j# _( F5 b
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      9 L) Z6 B+ G4 @4 [2 t( W+ Y3 G
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      % u+ u1 s8 X, T) |  K3 b* d+ |" {" R
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      / J$ I4 l& i. g

    13. ' u+ A$ y5 C9 u
    14.         <description>Spring Configuration</description>
      7 b1 d8 e: q3 }: G4 f' q* V
    15.         
      " J1 M. N: V/ y5 d8 r
    16.         <context:component-scan base-package="task"/> ( Y; x* y* [& X- q% U
    17.         ! W( }2 `1 N+ X, z# c5 z7 m, v
    18.         <!-- 配置任务线性池 -->' P6 ^" R+ w2 F* _* [& B: R' @0 N
    19.     <task:executor  id="executor" pool-size="10" />
      & P- _) f2 D2 T
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      2 h, p% }2 @. n5 Q$ B0 Z9 L1 [+ s
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>6 z8 h: o/ o3 C* b/ V
    22. </beans>
    复制代码

    1 z; S4 O% S- l6 K/ f2 f$ `0 |
    + I5 D# h6 A0 _" z
    ( x1 x$ H3 h2 N: c& i8 @7 Y基于配置的实现TestJob.java:, T. }. n; O6 C2 F4 D
    1. package task;  d9 v! |9 K9 {+ {

    2. 1 _0 W) [, |# a3 [2 a
    3. import org.springframework.stereotype.Component;
      ) j$ D. h- Q% ?/ P6 S
    4. /**( |: _; k2 |& s  [3 Y' N* H( L
    5. * 任务测试 科帮网 http://www.52itstyle.top/
      # }3 f+ }  Y& l# A0 [! r
    6. * 创建者        张志朋. {* l0 @! r" l1 L
    7. * 创建时间        2017年4月22日
      . I' u5 `/ G4 B( T
    8. *8 O! V- Z+ l% @2 l" q& V: A
    9. */5 I& s7 Y$ ^0 _
    10. @Component("TestJob")  , X0 R% j' v5 z7 ^% C5 U% C9 R
    11. public class TestJob {
      ! T4 y4 l3 X8 n8 V: K
    12.     public void test1()9 [* c  n- P9 u3 t7 c2 K4 P
    13.     {
      & |: _0 F1 b( c
    14.         System.out.println("job1 开始执行");" a4 ]  C# j+ N0 ~0 g3 u; C
    15.     }
      / Z) [, F7 N5 x/ N+ w5 C
    16.     public void test2()
      & x. M  k1 B( S0 A* ^0 v* \/ y
    17.     {
      ) R# H/ q) Z* e; Q
    18.         System.out.println("job2 开始执行");4 R9 m0 {% c" O
    19.     }
      * c' `  X# Y' ~) \
    20. }. B) L/ r1 R6 ~3 k8 f- I4 u. _
    复制代码
    基于配置的实现spring-context.xml:
    9 Z: \5 k- z' d- \- c
    1. <?xml version="1.0" encoding="UTF-8"?>
      ) {  }& F! z6 ^; J
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"$ w: s' I. ]  J  y2 `8 v
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      # \& {' r0 M/ ?0 B% [6 y
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      ) g8 i. |) g6 h5 }! ?- L8 t
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="& A3 N3 \; s0 ?  e/ q$ {- e
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      & l* Y+ r. k2 v  n, I, ?% {8 b4 W1 W
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      7 [) l# C# n1 w) B' P+ f# X
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      / q# t" ^4 L& o4 w' |
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd' X$ }" _3 P% u/ w- s7 I4 ?( N+ j9 W
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      8 m4 @' }( ^' B, z# X/ C
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      . W3 p4 M0 I; `: w
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      8 p8 n7 z, {& b# c

    13. + s/ Y" g) \' E. Y
    14.         <description>Spring Configuration</description>" ]6 y+ }9 V7 i; d5 D
    15.         - v$ u6 f% _. i# d% `  o# F3 H0 g
    16.         <context:component-scan base-package="task"/>
      " \* C4 N5 o3 E  k0 W" r/ O. V8 @
    17.           n! g8 y: I" ?) G6 h) H/ O8 j  t
    18.         <!-- 配置任务线性池 -->" R1 H! ~- {5 n0 s9 I- w% ]
    19.     <task:executor  id="executor" pool-size="10" /> , ]5 O; O* M9 w- w# `
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      0 A) H6 S  |0 z, t; i. w/ ?
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>) }% [# @0 e( C# W7 z3 v) e  x" ]6 X
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->7 |1 Z* u- B; Y2 J$ v
    23.     <task:scheduled-tasks scheduler="scheduler">  7 n5 p3 s6 L; h% Y/ }5 P: u
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  
      ' u& d$ a( d2 s) G
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  
      ! w  ~* g; N# ~. P
    26.     </task:scheduled-tasks> : A1 v3 x% r% {8 y! |/ _1 {$ N0 a% q; I
    27. </beans>
    复制代码
    % E% G* d$ y4 |$ X
    $ L) W. B4 D  _1 y1 d- @1 M' }
    附:
    cronExpression的配置说明
    字段   允许值   允许的特殊字符
    秒    0-59    , - * /
    分    0-59    , - * /
    小时    0-23    , - * /
    日期    1-31    , - * ? / L W C
    月份    1-12 或者 JAN-DEC    , - * /
    星期    1-7 或者 SUN-SAT    , - * ? / L C #
    年(可选)    留空, 1970-2099    , - * /
    - 区间  
    * 通配符  
    ? 你不想设置那个字段
    下面只例出几个式子

    " f/ d1 I0 q# X4 n. q
    CRON表达式    含义
    "0 0 12 * * ?"    每天中午十二点触发
    "0 15 10 ? * *"    每天早上10:15触发
    "0 15 10 * * ?"    每天早上10:15触发
    "0 15 10 * * ? *"    每天早上10:15触发
    "0 15 10 * * ? 2005"    2005年的每天早上10:15触发
    "0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发
    "0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发
    "0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
    "0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发
    "0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发
    "0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发
    SpringTask任务案例源码实现.txt (31 Bytes, 下载次数: 0, 售价: 1 IT币)

    / |. Q/ x3 q) ~) E( }

    科帮网-Java论坛、Java社区、JavaWeb毕业设计 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与科帮网-Java论坛、Java社区、JavaWeb毕业设计享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网-Java论坛、Java社区、JavaWeb毕业设计的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、科帮网-Java论坛、Java社区、JavaWeb毕业设计管理员和版主有权不事先通知发贴者而删除本文


    JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

    快速回复
    您需要登录后才可以回帖 登录 | 立即注册

       

    发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
    快速回复 返回顶部 返回列表