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

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

动态微博

查看: 2004|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2017-04-22 14:46:31 |只看该作者 |倒序浏览
    写在开始
    ; k3 T* t$ W* c7 E一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。
    ( t9 J' Q3 g! e" }+ ^举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。2 {1 W8 A9 u5 [! S2 @

    8 F  i0 ]% l# c" H* L5 c' Z; O任务介绍
    , `+ L0 P3 V2 E7 o, }就目前自己接触的定时人来来说,这里简单的聊下一下三种:- ?. ]' \3 F" Q1 p
    - s/ k( X# Y1 n
    1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。( M4 p$ m  U1 m8 {

    * P) p7 C0 C7 V+ @2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)5 v; ~* A) G7 G
    ! f2 i9 K, d4 h" g) b
    3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。; K& F  p3 R! b; b6 v6 k
    3 a6 [2 g4 T# K  O# i
    使用案例
    : F2 j0 i  t9 v' o2 G关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。2 n" ]5 G0 t3 a2 _1 i: I8 V
    ; E3 D3 \0 [; S! z/ [) f
    这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。' B- I# V$ y! ~# i, U9 j! d
    9 @7 ?) X* o" z! \
    springTask提供了基于xml配置和注解的两种实现方式。
    ( J7 J2 n4 @$ l: o$ z: R8 x# m: _0 x- V
    基于注解的实现TestJob.java:
    & Y) |0 C0 Y! w6 v% m4 y! O
    1. package task;2 o" Z' ^4 [! S  h! W
    2. 8 F( k, p7 ?, i8 V2 v7 S
    3. import org.springframework.scheduling.annotation.Scheduled;4 ~# v) a5 P4 q5 a+ ~* Z4 q2 k
    4. import org.springframework.stereotype.Component;
      4 J2 A' i- X( p; [5 M- i% Z2 Z6 v
    5. /**
      6 M9 o/ b2 k3 j) S
    6. * 任务测试 科帮网 http://www.52itstyle.top/
      " ~1 ~5 _$ S9 q+ h# |0 A& M
    7. * 创建者        张志朋; [# {& g, |' a2 J5 q% }( K& H
    8. * 创建时间        2017年4月22日) A' y1 I# V& J5 y" G; {
    9. *- O2 I% r, U! ?$ i- R
    10. */
      ) N; w' f" {3 M+ |! W
    11. @Component("TestJob")  
      " {" c, q, u" t5 v
    12. public class TestJob {
      . n6 |* C+ z6 O3 Y3 a+ [
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      + _1 Z3 |+ _0 U- a
    14.     public void test1()
      & y8 H6 I) n& N
    15.     {( j! y6 \9 N0 E0 S6 b' J# F
    16.         System.out.println("job1 开始执行");
      - b' `( w7 F3 b
    17.     }
      ) r5 e. x( r. M5 Q
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 ) j9 j. J( X' k2 O
    19.     public void test2()$ |# b. \. [& J. J" o
    20.     {
      $ D* g( ~5 i: l) s1 i7 l
    21.         System.out.println("job2 开始执行");: t, L! p& {* w; o' C
    22.     } - {* i: G6 e  S0 }0 {
    23. }& X: O4 I: X% C8 L
    复制代码

      @4 C" x. C: ~
    ) y: m% F$ H- M基于注解的实现spring-context.xml:
    * T( g" O) V' y" ]; b% A
    1. <?xml version="1.0" encoding="UTF-8"?>
      4 _1 t6 [+ c1 K6 v: k
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      9 D/ H7 u3 J; R- G. w% G
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      % R) ~2 f+ t! Y7 P2 P! t6 q
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      ; ?* R6 J  t; Z  y% H/ E! b" `. l! n
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="; K( r2 W4 O6 k2 L' ]9 f; ~& k
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        J  c0 W' }$ Z. R5 z9 l
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      * r5 O1 ?$ g, q& y) f  x
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      5 r1 ?( W* V. U2 E9 R
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      5 c* `" q5 c& H5 _
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      , s" F; _4 C/ k4 ~3 y
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      + r2 ?8 R2 z+ P/ I0 U
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      4 m9 e) h. \/ G+ b
    13. 7 i% _9 o; }4 ^# {' C' s
    14.         <description>Spring Configuration</description>: x5 o% n! x" R, d. }
    15.         
      ) j5 ~- |  x' F1 M' y+ z, k. s6 k  I' k
    16.         <context:component-scan base-package="task"/> , `( j' X7 G( e
    17.         
      6 j  `6 U/ k2 T9 t* q
    18.         <!-- 配置任务线性池 -->
      3 X+ l5 h9 b5 f) s3 o
    19.     <task:executor  id="executor" pool-size="10" /> " o5 q" V0 T$ {5 @4 a! N
    20.     <task:scheduler id="scheduler" pool-size="10"/>3 P8 p4 ^# |/ c1 R3 v% H$ P
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      7 X) T7 k  S, J0 C
    22. </beans>
    复制代码
    : r) x& Y+ s) g& ]& a* n

    2 ?# u) Z1 _* W- r4 r2 E( i% `  c3 |" s8 n' E: ]5 y& ]: I; Z
    基于配置的实现TestJob.java:4 b0 t9 T( \& T) f
    1. package task;
      / s" v# g: J" `# M2 \2 G# J" E& x
    2.   q" A) [. A$ p6 ~- t
    3. import org.springframework.stereotype.Component;
      6 B( c: U6 j7 g6 O9 O% H
    4. /**6 u0 X# t; I3 _) w: s
    5. * 任务测试 科帮网 http://www.52itstyle.top/2 I9 f- t6 Q; r6 j! y
    6. * 创建者        张志朋1 m1 V! o6 W' Y! l" {" V
    7. * 创建时间        2017年4月22日$ ^( V' `8 s. S) r" o" M
    8. *1 t3 W7 r1 R( |$ e+ C' E
    9. */
      1 r% x! \) e. f. U6 z, U# o, \
    10. @Component("TestJob")  
      * p' W( v4 @" B' s
    11. public class TestJob {( b7 F5 k+ z2 |8 u9 i
    12.     public void test1()
      / R: [3 f2 K' A% b7 s
    13.     {8 ?/ t9 E, g7 K# l9 t
    14.         System.out.println("job1 开始执行");; x0 ^# V; j9 p& R, Q4 G; B! k
    15.     }
      # z% j9 h7 z3 E
    16.     public void test2()6 K: k8 q7 G) x3 z: M
    17.     {
      ; y+ Q( A$ ]6 ]5 u
    18.         System.out.println("job2 开始执行");2 u* i1 b  w& s7 ]1 h' J* e( i0 f6 _
    19.     }
      * H, P  v& d" b' t: S
    20. }
      8 n8 h/ [; o8 e# h# h9 c
    复制代码
    基于配置的实现spring-context.xml:& }( t3 d1 h! A& |- d3 [
    1. <?xml version="1.0" encoding="UTF-8"?>( i2 L8 I. F- M; N% R1 N9 ]
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/ l9 d; h$ {0 Z9 w4 e/ Z
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  4 K* x7 K' b* w  D1 i7 I  H
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx". y2 U# u$ h9 O. X% Y5 H
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
      ( u8 e" m3 E' j4 B
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd' L. @: }. P/ a, t
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd) n/ p6 H" J2 \' y" D
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      0 Z9 L2 B; N1 ]6 O; J" q' p! Q
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd" Z5 F  P2 `( ~  F! B
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      ) U" L! r: R: X2 _" _' {% L, o0 q7 ?" q2 H
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd+ H; E; S& b# L( e' \
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      4 }; L: f4 p* R
    13. * r: i: g! r1 g# _8 L7 z2 ]
    14.         <description>Spring Configuration</description>
      " e5 E  u- z' c2 Q
    15.         
      + F! v7 O8 y* y! c$ r" G6 W
    16.         <context:component-scan base-package="task"/> ' P0 d1 _' a3 d2 b. I; ]# m" C
    17.         
      / Y* b% n" I/ M  q
    18.         <!-- 配置任务线性池 -->
      # }" M' b( b" o! s. H/ s) Z
    19.     <task:executor  id="executor" pool-size="10" /> 7 p; j( i6 o( L( i4 ?9 x
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      " b! x& W* h: f
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>5 J. L) ?. q6 c6 l! }7 n0 i
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
      - ^( ]7 p8 f" O% u
    23.     <task:scheduled-tasks scheduler="scheduler">  & c4 }0 }; z* L
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  6 e. ?% i3 Y( I# G& ]9 ^' f. U3 [
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  
      # m- M4 b2 i% B, X. u
    26.     </task:scheduled-tasks> . Z9 G; Y. ?! g* g
    27. </beans>
    复制代码

    0 n6 T3 l% [# k, Z& @7 s6 ?
    ( t. g! Z, z4 X) o
    附:
    cronExpression的配置说明
    字段   允许值   允许的特殊字符
    秒    0-59    , - * /
    分    0-59    , - * /
    小时    0-23    , - * /
    日期    1-31    , - * ? / L W C
    月份    1-12 或者 JAN-DEC    , - * /
    星期    1-7 或者 SUN-SAT    , - * ? / L C #
    年(可选)    留空, 1970-2099    , - * /
    - 区间  
    * 通配符  
    ? 你不想设置那个字段
    下面只例出几个式子
    / o/ i, T9 \# c, x6 j) |
    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币)

    : U6 a2 O# w' P% D- y

    科帮网-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群 科帮网手机客户端
    快速回复 返回顶部 返回列表