科帮网

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

动态微博

查看: 2483|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2017-04-22 14:46:31 |只看该作者 |倒序浏览
    写在开始
    + h7 }9 s- s# e% A# X3 A( a  h一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。. R" G) L/ y: b
    举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。
    7 U6 q- ?+ v1 z# j- q  V  o) Q* {. d, S* V! m% Z2 s9 I9 u
    任务介绍
    % V$ Q$ G# i2 Y1 n! [8 P  U就目前自己接触的定时人来来说,这里简单的聊下一下三种:
    ; ~* H0 m4 w% K1 ~. w' t
    5 \0 _1 w1 Q2 K. ?- K" n( y; S1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。
    ( p  E; s: N$ Q2 x0 E! Z4 r" _0 b  e3 b) M) K
    2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)
    1 m* E' x4 x8 C* v  h% v
    % C: g  q" r" f$ h5 V9 ^* \3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。( R/ T5 ]% S7 t

    # v" J$ Z! ^4 E; e* m8 e使用案例
      ~4 d) c7 ?8 p7 Q关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。  l4 C0 \# m: M/ S, m1 i# e7 A

    ( J# i6 A8 {  l+ R0 U3 s0 [0 X1 v这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。
    . |; ^# Y6 }! Y
    ; s: W' f8 H  g1 y: }5 V, f. ?springTask提供了基于xml配置和注解的两种实现方式。( B: e& {- e8 U" u  `$ L5 V: m
    " s; }3 v  l+ K* F: Q
    基于注解的实现TestJob.java:
    . F4 \$ O5 V2 L
    1. package task;5 E" }8 l2 F9 n5 A9 w6 @2 g6 K

    2. ) y$ L. L' U6 j) }  ^
    3. import org.springframework.scheduling.annotation.Scheduled;1 z. d' }7 d7 s/ l, `! \
    4. import org.springframework.stereotype.Component;6 x9 K3 [( W3 R* J5 {
    5. /**/ F* J0 s/ r" v% r" }7 v
    6. * 任务测试 科帮网 http://www.52itstyle.top// Z0 v% N2 S7 r6 B) a, m
    7. * 创建者        张志朋
      # v" d# R% w% o9 N, `  H& n+ v
    8. * 创建时间        2017年4月22日4 l( n1 F; ]: m5 S& A& l
    9. *% s# C, _0 \" ]) j1 a" O' _/ M
    10. */% R% |$ O  T1 Y3 O# k2 ~6 R
    11. @Component("TestJob")  5 Y6 b7 c1 I) x( }  h9 q
    12. public class TestJob {
      1 s8 d3 E) L9 \5 E: @# S! j- E
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      # D# t8 r/ x; ]/ R2 _' g
    14.     public void test1()7 {3 \  I8 P9 R' v- R" p
    15.     {# _6 d! S6 Z, ]2 r' A
    16.         System.out.println("job1 开始执行");$ N# w% A( G- N  q+ Y1 [: _
    17.     } % @+ [1 {) B7 Y8 T( T, k- e, t, R
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      ' {( P% Z: ~( k4 G0 n  U- A5 |) I
    19.     public void test2()
      8 H7 S' ~5 t, a# `2 x6 Y$ R: \! `
    20.     {6 ~1 t+ m9 M6 b1 u9 e
    21.         System.out.println("job2 开始执行");
      : m' W+ P, O: g9 W( J1 p/ @/ h) J
    22.     } ! U3 A8 R2 X% `6 b( q5 u0 d
    23. }8 X: W0 o  q8 t- F) U
    复制代码
    * z2 D9 D7 Z& G+ p( D* N

    # m6 a# i8 M+ e0 p, J基于注解的实现spring-context.xml:
      n: `# h) s* n0 R
    1. <?xml version="1.0" encoding="UTF-8"?>1 O# |/ m0 p$ F* N" j
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"2 K4 n9 d: Q0 k4 p4 r
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  + a/ u) i) C, _# ?4 v& {
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      % ]/ f. N# R  p- q. H( ?
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="% E. `: L, d" K2 F4 U
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      . y9 Y( H( u. Z
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" S% a7 l# g6 f/ F
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd, r5 ]0 c8 `" n/ H/ _  v# d% @
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd4 H$ R  Z8 Y. w  A1 R- w
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      ; @4 n: G" }- M; R% R& G- a/ T
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      , O1 e" j" c* N# |9 n  t
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      + t" t. ^$ }/ b5 h
    13. 3 v* @( u4 [3 U+ l
    14.         <description>Spring Configuration</description>
      1 Q2 x" Z1 B% h9 H( m
    15.         ! C) ~: J" f. M0 A+ d7 `
    16.         <context:component-scan base-package="task"/> - A& {4 |- C' |) f. M3 E" E* k4 q
    17.         3 z8 A* H; b" @
    18.         <!-- 配置任务线性池 -->( l+ L4 ^: L& }- x) v
    19.     <task:executor  id="executor" pool-size="10" /> + H( u2 u( y+ a; d3 M" D' n, _
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      0 S$ A+ \- Y. {/ j/ S
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>( S: w+ C7 c) d& M$ J
    22. </beans>
    复制代码

    9 t3 k9 ^7 E9 @& @: |; v
    9 ^, c6 C, T  H3 n" n. ^' S$ w7 @4 r! f* `" U. Z
    基于配置的实现TestJob.java:
    3 f8 i/ u9 a; G% f! M
    1. package task;0 D1 P% k* \" v( `# j
    2. 6 ?8 [* C7 [5 o
    3. import org.springframework.stereotype.Component;0 ~4 ]/ g7 ?. o2 R- r
    4. /**
      4 n6 i; K) T7 u% c9 H1 F
    5. * 任务测试 科帮网 http://www.52itstyle.top/7 f7 L  ?1 a: [& l1 ]2 I. p& A+ K, d
    6. * 创建者        张志朋8 T, s7 C2 j- R. E
    7. * 创建时间        2017年4月22日
      0 r& N$ P+ O8 b( c
    8. *7 Z: f2 A( p0 a# K8 M6 P
    9. */
      ( }# r5 O+ _3 i8 [2 |
    10. @Component("TestJob")  
      $ H6 O  E$ C0 w1 m1 I3 v" Q! B
    11. public class TestJob {% ?1 f6 ?6 n7 {2 K; N3 c5 s
    12.     public void test1()
      5 E1 w! S9 b5 |, r( r6 x4 F8 R* u
    13.     {; n7 N& M+ Y$ c  H- P! r1 x+ e
    14.         System.out.println("job1 开始执行");
      . c9 F; k. \9 C
    15.     } : ?- j- p1 c, s" Y/ W2 I, V  t
    16.     public void test2()
      3 t: ?1 S. Q. N  m# s7 M# l8 z2 J
    17.     {
      ' }/ ?" e% I" F* ?
    18.         System.out.println("job2 开始执行");/ [7 r5 q- b3 k9 j: k. {
    19.     }
      ( A8 [5 x6 n$ B% m3 Y8 Q" ]
    20. }
      # ^# w4 @3 H/ A6 l5 u
    复制代码
    基于配置的实现spring-context.xml:5 Q/ ]3 f$ H# H: o# a
    1. <?xml version="1.0" encoding="UTF-8"?>& L  C0 M+ K  n$ [) J# _, r+ A9 i
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      : s" P0 [% Z& T6 C& _: F" v
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  1 q4 x. ^5 u, P9 N2 a- h
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      , {5 L6 E% f# [4 Z; _2 X, `% \3 N4 p
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
      ; v7 S5 l' ?; h; J
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      , n9 ^& }$ I. v2 \# L
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        K. h/ [# j) W+ n
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      * G5 @: ]. `; }3 u1 C7 z# L7 F
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd3 ]: L. G; b! a" d; ?5 G
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd; ~5 b- L! ]% i2 g5 y; f
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      6 \, \# C# A8 w# |7 j5 Q
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">, [# k  |6 G0 x/ V7 e# e
    13. 7 x8 `; n* u, {/ s! {; z1 Q
    14.         <description>Spring Configuration</description>
      4 w8 {! _+ h4 T4 [3 h
    15.         
      ; G- X" C0 n, N$ H( l1 E/ j* W
    16.         <context:component-scan base-package="task"/>
      ' I% i% S) F! `+ z1 \' ~
    17.         1 k5 r2 K8 o/ \+ w, }0 E9 C
    18.         <!-- 配置任务线性池 -->& G. K4 y0 B1 Q) ]" ?
    19.     <task:executor  id="executor" pool-size="10" />
      $ F) r( c2 }1 j! Z: T( ]
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      6 f* s) H& g4 \3 r7 q2 G8 T2 Z$ |6 ?
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>: ^( X7 @4 ]2 Q6 o1 e: }
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
      1 P! T' ]0 O* h" }/ ~' t: b" w
    23.     <task:scheduled-tasks scheduler="scheduler">  7 g# ]& x/ b8 y  }. z9 M
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  
      9 w- x% }& J/ E8 A
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  9 Z* {6 U% j# b
    26.     </task:scheduled-tasks> 5 Q* V" v3 C/ ^  ~) d
    27. </beans>
    复制代码

    ) Q6 j  k, Q" D& X
    $ ?1 j& O3 r# P3 O* v2 ?1 X) i
    附:
    cronExpression的配置说明
    字段   允许值   允许的特殊字符
    秒    0-59    , - * /
    分    0-59    , - * /
    小时    0-23    , - * /
    日期    1-31    , - * ? / L W C
    月份    1-12 或者 JAN-DEC    , - * /
    星期    1-7 或者 SUN-SAT    , - * ? / L C #
    年(可选)    留空, 1970-2099    , - * /
    - 区间  
    * 通配符  
    ? 你不想设置那个字段
    下面只例出几个式子
    9 ?4 b8 z! m' s' O& A! }# I( @! D
    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币)
    0 o1 u! s  `4 k5 H) O7 |

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


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

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

       

    关闭

    站长推荐上一条 /1 下一条

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