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

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

动态微博

查看: 2008|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2017-04-22 14:46:31 |只看该作者 |正序浏览
    写在开始2 r5 K0 E4 M# b3 h/ s
    一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。" O- S# B/ d3 v& N0 N% _
    举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。
    & K5 U7 @( {: C- M6 c" L* X" P8 e6 A, `4 U& _8 _! w) t, H9 p; V
    任务介绍
    & \4 Z) k' e6 ~1 e. N. c就目前自己接触的定时人来来说,这里简单的聊下一下三种:
    : y+ Q4 R4 K  j7 @6 v2 z' X+ @/ B
    + u9 E( _' R/ B; _8 f) g1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。
    $ E9 K8 K6 i) ~  d! W  b# D7 `4 c5 C! h  S
    2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)
    9 T  d: R. \7 _
    , b) f! }0 V4 `0 v3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。$ f: J1 U, V& T
    # N$ L% T8 P7 C  r7 a% H4 W
    使用案例
    , K; o* p8 l. E, z7 ^关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。6 {+ i/ \% Q) B' Z

    0 D# v( H) n" k0 B' s8 e这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。
    3 F2 T& e8 P! c4 \; A5 W4 v( B
    " i( x$ }( V" s" lspringTask提供了基于xml配置和注解的两种实现方式。! r/ `( U- o3 r1 V

    * w% d( q2 c* s, L基于注解的实现TestJob.java:
    2 p3 U! e9 K4 N% T1 r! I
    1. package task;# B& h$ k/ L1 r" ~% i. W! G; @
    2. ! n" K4 x- |: n" g
    3. import org.springframework.scheduling.annotation.Scheduled;0 m1 i6 Y- t* V  k0 [7 u
    4. import org.springframework.stereotype.Component;3 N, B3 C: F- b4 t+ Y1 v9 x0 w% q
    5. /**8 r* h! E. T. y' r, H
    6. * 任务测试 科帮网 http://www.52itstyle.top/
      1 z+ T+ d: s" f% t
    7. * 创建者        张志朋
      - ]( \( l/ K+ ~; B6 f7 ~8 j
    8. * 创建时间        2017年4月22日* L0 N: z' i: ^+ T, g( Y
    9. *4 \" P7 z: m; |% ~
    10. */4 N1 s/ E: @9 q+ o, X9 |# |
    11. @Component("TestJob")  
      3 D% q# [; z+ m/ {* Z  h8 W
    12. public class TestJob {" l; P6 Z0 R) B6 g  W
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 , W5 c/ W; N" |. |4 u5 m- _( G
    14.     public void test1()
      4 X6 m9 Q, j# P2 [- o1 v0 X; |, M
    15.     {; d* b; t4 i9 b* W. T: _3 N
    16.         System.out.println("job1 开始执行");
      6 r6 s. A" A/ X$ H4 r
    17.     }
      1 \  {1 A  r$ f6 T
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      & Z+ j- V7 \6 h. A7 t. Q( Z
    19.     public void test2()
      + J: `6 Z  ^5 b6 y: l
    20.     {* ?" i2 @4 m# B- z0 S( I6 D& z) _% N
    21.         System.out.println("job2 开始执行");" C! M* r3 M' x& j6 h% ?# m
    22.     } 8 d) l, |# z* V6 R5 L/ U! w
    23. }
      ( q0 S) R+ t" Q7 g
    复制代码
      A  y3 q; t" |/ u# z4 m; q

    ; x/ h/ ]1 }( J1 ~4 O( x% D( Y基于注解的实现spring-context.xml:
    # z. C) ^7 s: d2 o
    1. <?xml version="1.0" encoding="UTF-8"?>
      $ I* A, z! b; k4 T
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"* E3 V. X) M* c
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      2 E/ e' {2 s% q- @9 U) X  z
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      : Z' k7 L0 g1 A' V: A# b
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
      ) c7 A2 z0 w4 v, s$ Z& _
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      ; Y0 }' @7 W% r
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      6 O: ?1 o8 L4 W
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      % _) n! e% D( |9 [# i$ w( }
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      % b0 H. e( T$ @- D$ o
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      ! z$ L1 N7 O- U1 P! ~7 W! F! j
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      0 b4 F+ \  a/ N; x
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
        f! O; D1 Y+ V
    13. 4 s- [+ X& h3 _  r2 B' G
    14.         <description>Spring Configuration</description>
      1 P1 ^7 K5 b! ~
    15.         
      5 ~" C3 v/ G' t+ Y/ e
    16.         <context:component-scan base-package="task"/>
      % B& w# ]; N( Q, q8 n
    17.         
      0 s1 |+ s" X* l* ]' L
    18.         <!-- 配置任务线性池 -->
      9 i# M& i) j0 {
    19.     <task:executor  id="executor" pool-size="10" /> ; G! q* s8 f, q! F/ Y& m" O. _
    20.     <task:scheduler id="scheduler" pool-size="10"/>; S1 }8 ~" f2 b
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      6 I; U% B3 a8 q% x4 S  g
    22. </beans>
    复制代码

    " r; F# c2 @- d* Y! H+ U; H4 s- T2 E, y
    ( V& b0 Z! I4 [. e; V6 i
    基于配置的实现TestJob.java:
    ' b' X/ |, v2 H, c6 x( a/ u8 ^
    1. package task;" S7 Z0 O7 n4 L; p
    2. $ Y2 U( G5 h) {: {  _) s: [; v% ~& C
    3. import org.springframework.stereotype.Component;
      3 K9 a7 X! q, I( ?2 u( n3 h
    4. /**' [3 u" t) _& B+ R8 u
    5. * 任务测试 科帮网 http://www.52itstyle.top/
      " B) c! m" f) F  S
    6. * 创建者        张志朋
      , H" Z5 ^% {0 s/ g
    7. * 创建时间        2017年4月22日
      ) m* z! D4 W- n( m4 n$ t9 \
    8. *
      ! ~; [( z2 [7 \% a; }0 }
    9. */
      4 }$ s" c8 [. d- j: `8 o5 D3 u5 v  t
    10. @Component("TestJob")  
      5 ^* R5 r, m) m; D" H
    11. public class TestJob {
      7 O5 G% w5 x8 \; X5 e
    12.     public void test1()
      % V" ]' h0 t0 _/ V" Y: s$ G
    13.     {
      # e, H. n0 M# W9 s
    14.         System.out.println("job1 开始执行");# l0 T6 B9 e( N
    15.     }
      0 O/ S2 }- M# Q: b
    16.     public void test2()) {+ P# z2 Z8 a) @/ v) I- \0 }
    17.     {& q+ o9 @: {7 \0 P, G$ l0 c
    18.         System.out.println("job2 开始执行");
      ) M8 Y5 G4 C% c/ u
    19.     } * a) k" ?0 l5 G4 x2 k2 q: t4 h/ l
    20. }; i7 L" O) t0 n5 `$ r( ?6 e) h
    复制代码
    基于配置的实现spring-context.xml:' C# N+ ~) |+ T* @7 S. x
    1. <?xml version="1.0" encoding="UTF-8"?>
      0 w$ \* T1 |. H% d) l
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      ( p. B4 L; v% p" |6 Y2 ^  Y- \
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  8 o# b2 a' L! O3 u
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"( S, z( c# ^8 E0 _' y
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=") i+ k# x/ x/ I7 V3 D3 n
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd% x+ L) D3 L+ q7 Y/ d+ e
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      + H: ~8 F; N- p1 d
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd- V: V5 E! K$ \# J" Z' p- h
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd% T( \" k2 i* e6 `- d
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd- P$ ~- Q- z% F9 F8 E2 s
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      7 P6 x5 [5 M" ~7 d* q
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">$ d- y) p) ~) ~) U( z
    13. 9 w% h3 ]1 e1 h" {$ Y/ H$ L" K; A* D
    14.         <description>Spring Configuration</description>
      / r0 P9 h4 a' p8 u
    15.         % R7 R; S% A# _; g# ^* R5 B! C
    16.         <context:component-scan base-package="task"/>
      6 y( M! C, Y* s- \
    17.         
      7 Z! }4 l% r1 G$ _2 c$ f9 d/ M5 e$ r$ [
    18.         <!-- 配置任务线性池 -->
      / j: ]; p6 t& K5 e2 h
    19.     <task:executor  id="executor" pool-size="10" />
      ! u( G2 l" a) p: p; c4 G0 q& I, a
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      * _; O* L2 _3 K3 b
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      0 k7 `( Q! P& R% i9 k/ Y9 Q% j
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->& d  Y: y7 V$ r# p$ C" T9 b0 ~
    23.     <task:scheduled-tasks scheduler="scheduler">  
      8 A4 E$ A9 I& w8 ?( }  w, k
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  
      : q- }' y/ c3 U, ^" g  Q1 U+ s2 y' |
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  
      3 x1 d. U7 t, F5 u
    26.     </task:scheduled-tasks>
      ' k9 t5 X4 s- h( D6 a5 f7 L8 N
    27. </beans>
    复制代码
    $ n# D1 Y* J( C: v2 M

      [1 a* D6 K4 y: }. W
    附:
    cronExpression的配置说明
    字段   允许值   允许的特殊字符
    秒    0-59    , - * /
    分    0-59    , - * /
    小时    0-23    , - * /
    日期    1-31    , - * ? / L W C
    月份    1-12 或者 JAN-DEC    , - * /
    星期    1-7 或者 SUN-SAT    , - * ? / L C #
    年(可选)    留空, 1970-2099    , - * /
    - 区间  
    * 通配符  
    ? 你不想设置那个字段
    下面只例出几个式子

    ' m' D4 D. e, E) n6 w! V0 S
    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币)
    , J, y: B& L" W/ j

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