科帮网

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

动态微博

查看: 2484|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2017-04-22 14:46:31 |只看该作者 |倒序浏览
    写在开始3 ~. K3 B% v; i3 b! F3 P
    一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。1 V2 K4 w& [$ _6 B( M0 r# H
    举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。
    " c+ v1 T& ?1 e) t3 T* S6 }5 \
    ; u7 b1 H: C2 G$ J! k+ Z+ x任务介绍
    , |  d' M6 D" i' w8 @就目前自己接触的定时人来来说,这里简单的聊下一下三种:! C$ n/ O! v+ ^4 f4 u6 e8 Z' u
    * p7 V& d5 b/ ~$ N
    1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。
    5 r" O3 t- c& J1 Q+ n% G# t8 X
    ( @  V5 C% H! z2 B/ X8 v# A2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)
    9 o6 s( S+ M0 H% J8 `5 D* B9 [; w6 U/ {7 m$ R: F9 H, b9 f
    3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。6 D7 \+ ~0 c" p% S  E- Q

    . X% X; [* t- c& n- ^使用案例% O; ~: U7 g' B* h$ {8 q
    关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。
    8 @0 w- I0 Z  i9 s4 T0 f# G% x3 A+ P9 ]3 Y8 t- d+ {
    这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。
    % T' ]1 H" H4 ^" i9 |* J+ H
    & s1 M! O7 z9 [8 LspringTask提供了基于xml配置和注解的两种实现方式。( m% K1 e' V2 V

    1 |( f( @" B: M. l& v. p基于注解的实现TestJob.java:
    7 V0 T  y; n' ]
    1. package task;
      * Q4 A4 s( y/ L% x0 C5 p( ?
    2. ; P/ e, U1 o' T9 ]  n$ q( X
    3. import org.springframework.scheduling.annotation.Scheduled;
      8 A# R6 u; D  z) L5 P, |
    4. import org.springframework.stereotype.Component;- R3 B1 D% y. I4 j5 x
    5. /**7 b8 [2 G* j' b: |$ T% p
    6. * 任务测试 科帮网 http://www.52itstyle.top/
      ; }8 g6 k) `5 S3 x7 ~! k4 d
    7. * 创建者        张志朋
      + y3 r' [& A2 @4 R
    8. * 创建时间        2017年4月22日1 b. h4 ]7 d7 N- g7 k5 D2 m+ K
    9. *$ j1 Q& b2 @5 E6 O
    10. */5 [  ?, T( K1 _( b% Q
    11. @Component("TestJob")  4 B2 Q( z( J$ d! \
    12. public class TestJob {
      / K) }7 f9 y/ N. @. p5 B$ N
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      ; I& f7 W/ J5 d" ~5 {" J6 @2 b
    14.     public void test1()6 h& `5 _2 j' s5 s
    15.     {
      . S0 f1 [! M; I5 r8 z  t
    16.         System.out.println("job1 开始执行");
      ! Y; u1 a  N, D
    17.     }
      7 I# X3 x" ^, W" A( Q
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      4 ~$ |7 D+ Y$ b6 ^- _/ O  Y
    19.     public void test2()  }* H+ X% q; e3 S. B& o# ~* D3 y; x+ H2 g
    20.     {  D+ n; M$ t: z2 ]& H; e+ F+ h
    21.         System.out.println("job2 开始执行");9 o$ ?) l7 i6 l% T  Q& o# r
    22.     } 0 ]0 b1 i- W! X; o$ f
    23. }! q* ^5 _& V; C7 S
    复制代码

    - g' ~: }: M1 M2 P# E/ g  A$ z
    ! n1 k' E+ w4 `9 e; u基于注解的实现spring-context.xml:
    ' x7 e! S1 ?; _5 ^; z* E, O- L; d
    1. <?xml version="1.0" encoding="UTF-8"?>8 Z# k# C, s" z# ?
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/ S  j7 H' n) _) G, u( }* G
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      , }% P( r. Y7 f) x0 d. z
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"0 G# @# L# z) C/ b
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="6 B. w. X0 M; v& k3 S
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      ) i1 V) Z0 j# G( G3 T7 U3 K
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd( b$ D1 C; M) a/ [+ i  {# v9 H; n* I
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      ' i6 ?) X% g: G+ q# s1 d
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd7 E* B# N$ [; ], m" }0 ?3 R2 a5 S
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd' L# l) g( K3 @) b
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      . k2 L8 F3 E/ g
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      2 u% g+ p  {8 |; I

    13. 8 g6 l" k+ g, l% n& n% u
    14.         <description>Spring Configuration</description>
      4 e. j- |2 Z) F- k7 a: S- S! o, w
    15.         
      2 s1 S+ l, e5 t* E
    16.         <context:component-scan base-package="task"/>
      # V4 c$ G8 z3 K  L( n
    17.         4 R4 y9 M" _' t/ u0 x
    18.         <!-- 配置任务线性池 -->9 i- w2 X( ?2 p4 [* X/ s
    19.     <task:executor  id="executor" pool-size="10" />
      ) T3 }, s  ?+ r- }
    20.     <task:scheduler id="scheduler" pool-size="10"/>0 M4 _/ i' v+ T
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      , U' }" {% J3 C  v
    22. </beans>
    复制代码
    # p8 }" p' C: a, s7 s, g" ?5 a

    ! ?4 \( U; c3 q  r6 C  _3 N4 Y/ H* Y: X. X# n7 y* M
    基于配置的实现TestJob.java:# f: Z/ A+ L6 r5 D8 E" T
    1. package task;
      ) `: j7 U1 F% e2 e2 \, p& |* g
    2. ) j$ t9 n8 O% q9 ]1 M5 ^" Q
    3. import org.springframework.stereotype.Component;0 g( `4 b9 L7 S! ]' m
    4. /**
      0 Y7 `: o+ I' u8 S% j. D
    5. * 任务测试 科帮网 http://www.52itstyle.top/
      , k& z$ n! U4 H& U
    6. * 创建者        张志朋$ ]0 L$ D( v$ T. E/ S! q# ^0 F
    7. * 创建时间        2017年4月22日
      $ v6 Y- h8 I( d7 Y6 d
    8. *
      : y( J4 M3 V4 w9 z7 w
    9. */
      * l# ^! m7 @4 }* u* c. V8 }% j4 C
    10. @Component("TestJob")  
      5 J, S1 a; j) _5 L
    11. public class TestJob {
      : n5 r7 N& E: V1 m
    12.     public void test1()
      6 L2 K- d, S# B9 C# G0 c! `
    13.     {2 Z9 c8 N9 p7 u- W* q5 g  l
    14.         System.out.println("job1 开始执行");0 X5 |% G2 w  ^( P; Q. Y* q
    15.     } ) \/ h  |8 W" U" N3 K
    16.     public void test2()
      0 c2 k$ K/ {, J/ G
    17.     {/ o0 r( h8 _* H
    18.         System.out.println("job2 开始执行");' K: w# b9 U# e' W/ W3 D$ N+ Y
    19.     }
      6 e5 S3 `- f& ^& _
    20. }- L' L" \  V, y" C/ }6 P
    复制代码
    基于配置的实现spring-context.xml:
    2 I- l/ g. R; Z  K
    1. <?xml version="1.0" encoding="UTF-8"?>
      % l. i% ?" F# _! l) S" ~5 t; |
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      4 B6 A/ K8 b$ {4 s- _
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      3 I6 R* }0 `! V* \( P
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      1 l. }  L4 h6 a* |
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
      ( C5 r6 Z( s/ @9 A" k$ F
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      - {9 I9 n7 ]: U3 V2 C5 Y
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd0 {9 y+ `8 H, s6 t) g5 b/ |
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      " ^" z8 e* t3 ]% o7 m
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      9 Z# f0 `( H+ m6 j
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd# Z$ y+ h* w0 a; z# ]% a
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd- z) j1 e8 S% ~, K# q* m% K
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      * N& o- ]5 [) k5 O& O
    13. ' f- f% o+ \1 C
    14.         <description>Spring Configuration</description>. r; I9 C) M4 q0 I" R9 B3 l" C
    15.         
      , ?, S* @0 `/ N( P" i
    16.         <context:component-scan base-package="task"/>
      2 J# p9 v9 E% _1 C9 n
    17.         0 }" S# ?( ^2 B( b
    18.         <!-- 配置任务线性池 -->
      3 K" z# o9 f5 l3 G2 n' }/ O
    19.     <task:executor  id="executor" pool-size="10" /> 6 N7 f. l8 `  Z0 c2 x2 R
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      / L( Y3 [+ Y* {% A: }$ N, l
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>: d" `/ G9 h' X! }1 n  Z5 p! {- i
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
      9 H2 y) ^  `% E  R
    23.     <task:scheduled-tasks scheduler="scheduler">  $ x+ v. n; g- e
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  
      + {( t+ u) Z+ t0 s- _
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  
      " U# w0 c7 u- Y, o
    26.     </task:scheduled-tasks> + r4 I3 Y  D0 P" L
    27. </beans>
    复制代码

    $ W* K- ], w2 J' C8 i% z& G9 ]& O9 C" R
    ; x' m" j: X6 w) L, f5 X' n  z6 v6 Y
    附:
    cronExpression的配置说明
    字段   允许值   允许的特殊字符
    秒    0-59    , - * /
    分    0-59    , - * /
    小时    0-23    , - * /
    日期    1-31    , - * ? / L W C
    月份    1-12 或者 JAN-DEC    , - * /
    星期    1-7 或者 SUN-SAT    , - * ? / L C #
    年(可选)    留空, 1970-2099    , - * /
    - 区间  
    * 通配符  
    ? 你不想设置那个字段
    下面只例出几个式子
    : r- |5 Z. o& b0 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币)
    ) \+ @' R- [0 L- H: T& M

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


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

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

       

    关闭

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

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