科帮网

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

动态微博

查看: 2487|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2017-04-22 14:46:31 |只看该作者 |倒序浏览
    写在开始
    - O! d5 ?/ T; j( m7 j; t' y* }一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。2 ~& F7 R8 d2 F1 R9 \& y
    举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。- Q' k( ~1 _  r& ~- [+ ^/ t

    & t' n; ?- g; _3 S任务介绍
    8 n; S. P+ }8 ]* P% ?就目前自己接触的定时人来来说,这里简单的聊下一下三种:
    0 x: O) Y( s4 i5 a& x1 E  W
    & ]/ q. O$ y0 w1 O  P4 X9 c1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。
    3 w6 c# ]3 I1 G" M8 D1 ?; _
    3 J5 U* X# r9 b: B2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)$ Y# P& o& B" u  v
    ( [' {  r1 n8 f4 {7 H
    3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。
    $ V7 O; |$ k; Q3 y7 W
    ( k9 R- s8 Z3 E, T) ]使用案例; I- K" p1 F" M, i% r
    关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。
      G5 z# r+ L9 _% m0 H8 h5 Z4 n; y
    这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。' i$ O! e+ x5 U; ^, K" _

    ( F: V8 W! D! g9 YspringTask提供了基于xml配置和注解的两种实现方式。
    " V& @7 w  b# j' S4 ~& f
      ~: Y* P* D2 @5 _& G& G基于注解的实现TestJob.java:
    6 q, s5 [6 y  {2 ?
    1. package task;
      4 o) }! S6 B9 L# F

    2. $ o8 F: Y& J. \* H* ~& @
    3. import org.springframework.scheduling.annotation.Scheduled;
      2 ]  P* m3 E' x8 N; u
    4. import org.springframework.stereotype.Component;
      . Y* k) @+ a/ T# O" h5 l6 i7 F1 o9 |
    5. /**9 m% L7 m  l' S
    6. * 任务测试 科帮网 http://www.52itstyle.top/
      , c5 p# t8 M9 _
    7. * 创建者        张志朋: y' `$ u% @9 ~& A5 b+ O
    8. * 创建时间        2017年4月22日& F" [- W9 G7 |8 `% \3 a
    9. *2 Q' S2 i+ X+ [+ `. h/ d, t
    10. */
      0 F1 g; h, U0 W2 ?4 O( b
    11. @Component("TestJob")  9 I" g$ p( m/ z; T1 u2 N! C2 ]
    12. public class TestJob {" [/ Q( q! A" H& X: U5 ?, \. S
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 $ b( y$ |: f. W* V7 C% [: U4 p
    14.     public void test1()
      / Q3 l& w+ I5 N  [- I
    15.     {
      % U! ]; v+ w8 t& N7 f! P
    16.         System.out.println("job1 开始执行");
      ' T. `& s, e9 Y- V( I' T$ X! t
    17.     }
      ( e, y6 [8 \& \6 N2 V
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      7 {  Z0 s# m, W7 q, o9 g
    19.     public void test2()/ M7 b7 B2 B  X5 W  |
    20.     {
      / N- D4 K  X4 j7 ^4 p, \6 Y  v
    21.         System.out.println("job2 开始执行");' z. e, o6 W' f, ~2 Q% _
    22.     } ( X5 `; w# t. j4 \) x
    23. }
      2 V4 O$ ?3 {' |, K& I, s9 a
    复制代码
    , u' W$ S5 C( X5 x# A% H& ^3 M! \

    / R. y* J; M' J; E3 x基于注解的实现spring-context.xml:" @- v1 W+ ~6 u
    1. <?xml version="1.0" encoding="UTF-8"?>
      $ S  c! N- h; R$ f2 [
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      ! b: A8 g, W+ A7 ?  ]# B
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      # d: V6 L: \6 a+ U4 O+ `
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      1 ]# F. J8 F. s: |% h0 j
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="* T4 \5 K6 F. V" E9 e( `
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      * S+ K4 p# t! F
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd& h1 Q- P! f/ j
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      9 N7 Z+ P7 b) i" y; A' x3 G$ P
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      3 i: ~) ?- t! k2 k
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd: z* ~8 t4 {; \$ L. l. L/ F
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      ' }. w9 c4 o. R  t1 I+ ?* m
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      + G$ `) W$ L1 d+ Y: R! H$ }0 K
    13. 2 i' ?8 \+ i" g% i
    14.         <description>Spring Configuration</description>3 K) e; x- s, A6 [: v1 b. \6 @" n/ Z
    15.         
      8 C2 P4 q& V: ~) J" }
    16.         <context:component-scan base-package="task"/> % {: a# S9 I, W
    17.         
      " _7 i7 e( E( H* [
    18.         <!-- 配置任务线性池 -->
      . d: K! U& ]% O  B: I/ t
    19.     <task:executor  id="executor" pool-size="10" />
      6 u3 z  F5 a% m/ M
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      5 y6 j# m# e" Y4 @
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      & N9 p$ f+ M7 z8 B$ d% g3 A0 w
    22. </beans>
    复制代码
    6 g9 Q' ~7 _+ V( w; K! r
    - v( ]! e1 b' q
    ! g- u+ ], G& ^" W9 ^$ |! J8 u$ Q
    基于配置的实现TestJob.java:/ r5 ~" V2 z. f1 g$ D
    1. package task;- c& k1 `- ]2 _2 |3 l, `

    2. + G$ F8 R) B3 k9 [* W: X* T& I
    3. import org.springframework.stereotype.Component;
      2 ~/ k7 {9 H' ]. Z3 |
    4. /**6 q9 F; M4 T; Q
    5. * 任务测试 科帮网 http://www.52itstyle.top/) x6 X- e) u+ ^2 ^+ O6 f! {4 ]
    6. * 创建者        张志朋
      0 N9 o+ Q8 P7 O8 t
    7. * 创建时间        2017年4月22日
      " c* A& z' s4 [
    8. *+ j6 _  @. R# B4 A( A. s7 P
    9. */
      * a; n1 N: f$ N6 I  T  i
    10. @Component("TestJob")  
      / l, A+ A6 R) Y1 |. A1 H- m% p
    11. public class TestJob {7 F9 i/ @; ]/ ^$ E4 ~
    12.     public void test1()
      7 i# O; [  t0 `/ x! m; j8 S$ b
    13.     {6 D' W# z1 t; T# ^% ]$ B5 ^
    14.         System.out.println("job1 开始执行");, x9 s+ e3 E& y9 E% W* f1 c
    15.     }
      4 l2 @% s3 e( t! V7 e
    16.     public void test2()* b& \* m7 g" W6 D7 a0 q  q
    17.     {$ |7 t& o9 S$ g0 g4 z
    18.         System.out.println("job2 开始执行");
      , S  I- n0 B% \  C6 @5 Y0 m
    19.     }
      % }2 v" ?7 n: O$ i" P
    20. }- x$ h! E8 j' [; ~
    复制代码
    基于配置的实现spring-context.xml:/ s7 V/ K' V; n
    1. <?xml version="1.0" encoding="UTF-8"?>0 b9 h* ^" }% d. G
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance") D% n$ ?' |1 X, N' U# H8 D
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      * t2 F( e  T1 C. ~1 [# i, y4 d4 L
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"3 e- u' H5 V0 Y" f+ R. O( b
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
      5 F: `6 h2 ~$ _. M9 N
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      0 y' [. V2 C4 |9 P
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd3 j, Y6 ?! U# S5 J  `, W% V  X7 r. F
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd  s/ p" h! v" i5 G
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd. Y1 v* p) m2 i- t* ]
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd! H3 `/ \2 p9 B* _/ {
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      " C7 ?( b. J, {1 r, b1 W
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      1 T7 w* s1 K  g$ f; X  d- O

    13. / I* p) F7 K! {' f+ q
    14.         <description>Spring Configuration</description>
        u6 P9 s4 ~' r' o2 V3 j
    15.         
      . b, X' z5 f& n0 L' k3 ~' ?, X) o% M
    16.         <context:component-scan base-package="task"/>
      ' ?  ~: l$ M5 G# w$ A+ B
    17.         
      2 @- a. }3 d' L. B: M+ g! S2 {
    18.         <!-- 配置任务线性池 -->
      : A- `" r. k' q5 s3 P6 X5 @( o
    19.     <task:executor  id="executor" pool-size="10" /> 3 v/ o+ I5 I! ^0 m9 C  o5 `$ k- I# O
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      * q% b6 P' m1 i9 H+ D
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>( d) f  S6 ~# R+ D
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->7 j( r  |: I$ V% S+ L- c
    23.     <task:scheduled-tasks scheduler="scheduler">  1 k" E' t$ D9 Y0 e) c( q/ S+ \7 ]: o
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  
      + Y% h* z+ G, p) l" \6 a
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  7 J0 Q' e, o7 \7 w8 u
    26.     </task:scheduled-tasks>
      1 W/ G' U% ~  y
    27. </beans>
    复制代码
    " v- X3 @% \# F$ _8 u2 `0 U  \
    $ [. g- s7 b: l3 ^- b+ j# L( 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    , - * /
    - 区间  
    * 通配符  
    ? 你不想设置那个字段
    下面只例出几个式子

    5 P( R+ \+ Z# V$ L) _7 j& w4 g! Y
    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币)
    % V- f$ @9 n9 d. h2 v' [

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


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

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

       

    关闭

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

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