我的日常

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

动态微博

查看: 2110|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2017-04-22 14:46:31 |只看该作者 |正序浏览
    写在开始1 n0 B8 z0 q% m3 D( [; W
    一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。2 P# J/ W6 _* V9 O; F
    举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。
    + l+ a6 C- l7 K6 r8 ~" l% ^- e/ X
    ) U: F. u6 M" e3 M: ^任务介绍- Y9 Z( t7 O% U; l$ E8 }# J. y, t
    就目前自己接触的定时人来来说,这里简单的聊下一下三种:
    8 U9 G0 V8 y6 g- a- }8 C) l$ J
      d# w. R% s. Y: \7 T" T1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。
    5 Y8 M3 Y9 K4 r: ?
      v6 Z4 x" w6 e# f3 X1 |1 [4 n) X2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)0 `1 u7 g7 A9 B: [+ |+ x9 V
    " W! G3 y9 W! s; K/ @7 q$ A6 f
    3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。
    - V$ v+ V$ Q  W; y& M3 v' c! K5 C$ \1 ~& u+ Y  L4 V; m4 f- U
    使用案例. n5 ]* Q5 m/ [9 R3 Q% v3 X( e, f$ [$ b
    关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。" o( @+ M- `$ Z$ o' w7 z% V! b# v  V6 {
    3 d7 t- }# R; ?4 Y7 ^/ J# H0 V0 O
    这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。
      _" [; K2 \  c3 {6 V1 S; |. W7 p, [) @0 M9 v9 p" _
    springTask提供了基于xml配置和注解的两种实现方式。/ P2 x. b" U  S8 H- [

    ' D" ]% N4 @4 C; d3 f" p# ?1 R基于注解的实现TestJob.java:6 d9 W3 |: }; W5 P1 ?6 n  J9 i
    1. package task;; _) X) R8 L8 X0 W! w# N- x: h2 x

    2. 0 s/ b1 w5 t4 }0 r4 t  y
    3. import org.springframework.scheduling.annotation.Scheduled;
      5 C5 F6 [  ]; C
    4. import org.springframework.stereotype.Component;
      2 O: q& A; \4 X3 k4 ], f
    5. /**
        K. N. _+ I# Z6 z
    6. * 任务测试 科帮网 http://www.52itstyle.top/' E$ G0 _& G* T: m4 r" z6 n7 W
    7. * 创建者        张志朋
      ( t* y; ~0 s, X9 O  b; m
    8. * 创建时间        2017年4月22日
      9 u3 g6 O! n0 B  u5 F
    9. *7 f6 c. e/ H0 B& u: K( u2 [# I2 G
    10. */
      ( M2 }# d. o- G1 s1 L
    11. @Component("TestJob")  
      # x: M( e) z# i5 w1 v; d1 c
    12. public class TestJob {
      , L$ W0 \6 i; D9 R$ D1 V
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 ( t  L% @- g$ `9 B, }2 e5 f" B
    14.     public void test1(); |6 o! i. P( H- V# b. J$ S
    15.     {
      % P- c1 K  z+ z  K; f
    16.         System.out.println("job1 开始执行");. ~; g, a& f$ ?: P2 b
    17.     }
      1 v. C6 @$ l8 K& J0 N, J) G5 @4 l
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      ; ]2 L( B' g% W( E  J& q
    19.     public void test2()7 C2 n, M2 [* b2 ~5 ]% Q
    20.     {
      2 Z9 G: F( P8 v% O, H9 G& k4 P9 I
    21.         System.out.println("job2 开始执行");9 Y+ n. P, i0 V) r1 E
    22.     }
      $ B1 Y1 p" a4 t. f4 n+ q9 q
    23. }
      5 D5 E. d& r' I2 J! q: v7 Y
    复制代码

    2 v0 S; D% @0 i9 ^& P+ s
    * s4 u5 T7 r4 G. _0 Z$ L基于注解的实现spring-context.xml:% Q  k1 w. z. q- [( |: T1 N
    1. <?xml version="1.0" encoding="UTF-8"?>" `3 R3 E* ~& i3 v5 M
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"6 f4 O* p9 @& N4 a2 D9 B
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      $ i! n! M8 R  n; O0 J5 }0 p8 p
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      1 A" Q, Y) V3 @1 r- `" C
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="" e7 a, `# F0 ^4 e; n/ l' \
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd1 H" {& a7 X! S6 v5 ~/ Q% l# E
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      1 E/ o# |1 o# H" p
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd* m0 q1 G% {1 e& \& p! B, C
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd3 j7 Q3 W# t) m( l
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd* }% j. R+ v4 T0 r# Z1 \- U( y3 _
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd  R. N( n: m. y" {
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">! X4 W- [4 n+ Z
    13.   ]5 ]7 q  f7 m: |3 Q: ?% `3 L
    14.         <description>Spring Configuration</description>1 I+ D7 w+ K2 k. l% J: H: _
    15.         ) w( y- z( Q8 [: i* O  m4 k9 {
    16.         <context:component-scan base-package="task"/> 6 K0 e) o& i" _& d  H( [4 L
    17.         9 ]5 c5 r" N1 L; {- w! z1 M  q
    18.         <!-- 配置任务线性池 -->7 n0 g; J8 }7 \) t$ z
    19.     <task:executor  id="executor" pool-size="10" /> ) w" f% [  \; s3 i
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      $ o" r& k$ I* m
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      ) [. l$ \& z, P3 [9 c5 E
    22. </beans>
    复制代码

    * V  }" F, M5 g( w5 [& M9 y
    & Y( @9 r6 Q# }! d( Q- C: A' V; \% \0 g- H1 o5 `0 X# w3 }$ d( A+ ~
    基于配置的实现TestJob.java:
    # E/ i# ]0 Y' k( y$ ~
    1. package task;2 s* s/ i. I1 D" V; y/ _

    2. ) G! X+ p! M$ B2 L& z
    3. import org.springframework.stereotype.Component;) u  S; d# r" e% S8 ]! ?) Y
    4. /**
      . V7 D; v( }5 @
    5. * 任务测试 科帮网 http://www.52itstyle.top/8 n3 r1 S% |+ R9 C3 Y( \
    6. * 创建者        张志朋
        ~/ ?. W8 t8 R' j& Z, h
    7. * 创建时间        2017年4月22日
      1 f( L9 O: T. Z) W: p2 D9 z1 n6 A
    8. *+ Q3 g# ]/ K2 I* }
    9. */
        h  O" A. D9 s: ^# p4 }6 c
    10. @Component("TestJob")  
      ; g% I) D: d. o! a& v6 o
    11. public class TestJob {8 `) @; l% q; F+ q( X
    12.     public void test1()( z& Q1 d6 i) v2 U7 e& M1 V
    13.     {
      ; l' W1 U% B3 T. t
    14.         System.out.println("job1 开始执行");
      5 ]& _* h; o! R
    15.     } 3 i) v" F! v  v. |& z
    16.     public void test2()
      * m, }  k; {1 D: D/ X
    17.     {
      2 ^) X" g" m0 u$ m
    18.         System.out.println("job2 开始执行");* r5 Y9 C( {3 L9 A1 l+ O
    19.     }   h) Y% P- }# b( ~$ E5 l5 \: F! c
    20. }+ U& i1 {7 i9 Z) E4 s
    复制代码
    基于配置的实现spring-context.xml:+ j- z$ O) R* Q
    1. <?xml version="1.0" encoding="UTF-8"?>
      ( z* \/ E+ W* q6 g4 M% b( j
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      + G! f. S8 p- Y' {  D: `
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  : f( y" O$ g6 C: [* M1 x
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"3 @- |; q) ^( ?+ X6 M5 t3 l
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
      + t3 Q* Y4 R7 _- b' J
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      / d% K$ P' K+ I6 s# s: o: [
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd4 S" q2 O1 e2 l( K6 i! y
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd! U  ]! g1 }2 E3 b* s
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      3 M7 N6 X1 Q$ ~+ Z# Q  r9 |( y
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd2 R+ A4 X/ _! f6 S' Y+ g
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      $ O5 J9 d8 o2 L- L5 K- q
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      : e& k" t9 K$ I/ y

    13. 9 B+ O3 m' H2 O8 z9 z. a- F/ M; b: W
    14.         <description>Spring Configuration</description>
      0 u3 h1 x3 [# R/ a1 q2 S' M% g1 l
    15.         
      , N$ W2 K0 Q; b! D
    16.         <context:component-scan base-package="task"/> ' @2 I4 |  x5 w; O/ F8 O  r6 j
    17.         7 u+ o3 f, c5 _# f' ^9 S. Z& h
    18.         <!-- 配置任务线性池 -->
      * ~  T# E  h, V6 z- ]
    19.     <task:executor  id="executor" pool-size="10" />
        ]. v1 g$ ^% g- q' K' Y+ q
    20.     <task:scheduler id="scheduler" pool-size="10"/>. m2 v$ [: O3 c$ ]  K
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/># ^5 @) v; S; B9 f
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->- f: R3 ?% A7 l5 N  Y
    23.     <task:scheduled-tasks scheduler="scheduler">  
      ! I/ x) U( e7 i: R$ R& {: L
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  - s+ p8 |0 M1 g5 V" u) I5 T: ?5 h
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  
      6 n* v& p/ ]" A- m, f# @
    26.     </task:scheduled-tasks> & M; m: Q$ A, S  w6 Z' B9 Y
    27. </beans>
    复制代码
    # K# t6 a( g3 t. ?9 o5 S/ y

    ) U9 ]7 v/ k: \- s- x
    附:
    cronExpression的配置说明
    字段   允许值   允许的特殊字符
    秒    0-59    , - * /
    分    0-59    , - * /
    小时    0-23    , - * /
    日期    1-31    , - * ? / L W C
    月份    1-12 或者 JAN-DEC    , - * /
    星期    1-7 或者 SUN-SAT    , - * ? / L C #
    年(可选)    留空, 1970-2099    , - * /
    - 区间  
    * 通配符  
    ? 你不想设置那个字段
    下面只例出几个式子
    3 O3 Z1 r9 o" O
    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币)

    $ |+ S# Q" O' j" {" ~

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


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

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

       

    关闭

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

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