我的日常

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

动态微博

查看: 2479|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2017-04-22 14:46:31 |只看该作者 |倒序浏览
    写在开始" e) ~' n: m& J# K+ S7 y& d7 h
    一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。7 W7 R9 t  ]% Y" ?
    举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。
    ! b. D3 |7 k  G, d$ d" D9 J7 X/ r, z
    任务介绍5 ]1 F' {' {- m  j/ i. {
    就目前自己接触的定时人来来说,这里简单的聊下一下三种:' o9 C! O9 m' H/ Y& W; g4 _# }6 \

    ' K) \5 f4 y" {+ P) ]8 w+ U1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。
    , ?& A1 q' M4 ?0 o9 o$ a: v; }. S: T, u& B( P
    2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)0 i$ ^9 q+ M0 v0 F
    $ x. g3 N! I3 @4 J3 X( f9 U
    3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。$ f7 j9 A- Z( |! ]

    & ?2 L/ {$ l. \: h2 G: d0 `使用案例+ o& r. ^# x. l4 w. u
    关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。
    " R7 n: _& x1 z9 Y# y
    6 A4 K; E9 B$ ]4 q$ E这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。: L4 W8 [- ^& k- @8 m8 t! ^
    % H. k" Y$ r( u; C* p$ W
    springTask提供了基于xml配置和注解的两种实现方式。6 w' y' Q0 v. n. r% i) v& Q& \
    ! a' g6 _; l6 V3 U
    基于注解的实现TestJob.java:
    ! [8 b2 a) E) o7 c6 m0 s
    1. package task;' H$ }; u( v+ h+ `. F

    2. + R* {/ h. }8 }: l3 o  Q- F
    3. import org.springframework.scheduling.annotation.Scheduled;# J; m  s+ K8 c/ N" b
    4. import org.springframework.stereotype.Component;  e9 E- N0 [$ S% I
    5. /**
      ( U+ n% j: g1 @$ q
    6. * 任务测试 科帮网 http://www.52itstyle.top/
      : h  O" t2 @" a6 b
    7. * 创建者        张志朋
      ( d; G4 U5 J# l: s( k0 q' P
    8. * 创建时间        2017年4月22日
      ' s8 F) `$ S  K$ O" z9 u
    9. *
      ; G9 \: u% l* q$ G' @7 C- w
    10. */' t3 R8 J# I  E  R2 Q- i
    11. @Component("TestJob")  
      2 ?; J8 E5 u' |0 |4 E
    12. public class TestJob {
      3 z3 h  y6 p; l8 f* T* [
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 ! y$ D/ S  g( [: |; p4 N( E
    14.     public void test1()+ z% N. G- ]$ x& U- n* s+ W# Y
    15.     {
      ) f) w- u% _  Q# b% i/ _7 W  h
    16.         System.out.println("job1 开始执行");
      ! ~/ k6 [2 j, h
    17.     } 5 [! [# o) D. B" N' p7 S5 N, o7 h
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 8 A) Q0 D2 R% t( P% j0 T5 f6 T
    19.     public void test2(); a/ K5 M# u; i
    20.     {
      9 n7 n7 Y9 X, p
    21.         System.out.println("job2 开始执行");
      5 p) a/ m0 f6 \" `/ _3 q
    22.     } , x, I+ C8 w% a) g1 \" h8 u
    23. }
      7 b) J* D5 D/ u: u7 ^; g5 p8 C
    复制代码
    - {  p* y( \  V8 a
    : f7 F! J& ^& Z7 n+ ~2 e2 Q, N3 o
    基于注解的实现spring-context.xml:) v3 }; v9 {/ D+ p  B: j# z3 e
    1. <?xml version="1.0" encoding="UTF-8"?>2 s/ `  G, n2 {" |- f
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  v1 B" o( c' v# P, k
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      8 A1 l; B+ e6 ~( h; X
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      ; a. ?1 R* r: _# h7 {( W8 [
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="/ Q! \$ F6 k; }3 j; l# A
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      0 c% Q* d7 a$ g% u, P) Y
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      ( U  t& G/ I( \! n: V% w% F' m
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      $ \- B. j% C) Y0 q
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      - _; b1 Q2 Q7 f4 z+ H2 T
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd6 K% p+ y9 O, ~- Y; h( f2 o0 z
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd4 x4 K! `1 J* W7 M) b" W
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      5 S- v! z. g# r7 b% a

    13. " O" I6 p: ~# Z4 L/ e
    14.         <description>Spring Configuration</description>, \* }* [% g! a3 P! E" F
    15.         
      / J; l- T  ^( `" M3 o* L5 P9 Y
    16.         <context:component-scan base-package="task"/>
      7 J8 ~4 \$ M% F5 o- Q5 m
    17.         
      7 l' s1 D5 n. y& B0 ^1 l( e
    18.         <!-- 配置任务线性池 -->
      3 v4 D# ^/ Z" M4 a  \2 Q
    19.     <task:executor  id="executor" pool-size="10" /> 3 o; d  n6 l8 e1 f4 E7 W& Y
    20.     <task:scheduler id="scheduler" pool-size="10"/>( y$ ?, O& P5 f
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>/ B3 k) m9 C. g* S( E
    22. </beans>
    复制代码

    2 s$ ~+ l4 f# l  P! `' ~
    - P2 x9 O3 i% ~. M% D, H; _- l0 |4 C6 z9 C2 R
    基于配置的实现TestJob.java:- z; O/ I# i  S0 t4 O+ v: m
    1. package task;: V1 T! k. [! \

    2. 2 h4 j3 e& F3 \' {0 b- n- N
    3. import org.springframework.stereotype.Component;
      5 b( g  l4 o  V8 J  J, y# i
    4. /**7 S# g- T! T; v( t2 r# f8 ^
    5. * 任务测试 科帮网 http://www.52itstyle.top/6 ?! \! K$ ]$ x  d1 u( r
    6. * 创建者        张志朋
      4 X9 Q- u# u. x( f8 M
    7. * 创建时间        2017年4月22日5 ^+ I5 j4 o' W+ F$ t; g7 |
    8. *
      $ p7 m, k  o2 N
    9. */% g' E. P& u- I
    10. @Component("TestJob")  2 A, Z+ }6 v7 v0 K9 G
    11. public class TestJob {
      1 D% t! w( d' G; U
    12.     public void test1()
      0 `$ c0 I" v+ Q; o7 Y( a
    13.     {* f2 g5 U. j7 V* I9 H
    14.         System.out.println("job1 开始执行");3 D* X& f/ T2 i, s* O
    15.     } 5 U; P7 V0 l) o' D) M  y; r- J
    16.     public void test2()
      : I7 B/ R0 A8 Z& J  z, H' R
    17.     {
      7 a. i: E3 J: \9 t9 j) b- o0 W; v# q
    18.         System.out.println("job2 开始执行");
      ( {% H2 C9 e+ w' f% V
    19.     }
      $ Y& f6 l/ }! a! a5 \
    20. }
      ; O0 i! q9 _& ~: ~3 v+ [; v1 Y
    复制代码
    基于配置的实现spring-context.xml:
    3 W* c  B1 g, @* B; I
    1. <?xml version="1.0" encoding="UTF-8"?>
      6 i! {* W6 P6 S4 O5 W/ Q
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"# h, [8 f  g% M9 Y' V' u0 i
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"    _% P! F" X1 g8 [- Y8 ^
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      4 Z, S! B7 n# ^5 L+ ~1 B
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="2 t' v. y0 h4 d
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd' w4 h, C" l' A" c
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd* X  K! X$ i9 S$ j0 q
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd1 x/ K5 a, r9 P8 T: I
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd. W$ P: Z# g4 B/ ~
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd9 s. e/ [- C0 h4 X$ v
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      & O# f) g8 m& I$ w4 {; Q
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      9 l5 t* U6 Q( h$ w' F& C
    13. 5 }: a- N5 Y9 _# D: i+ S
    14.         <description>Spring Configuration</description>9 a# }( Q9 [! z- k, T# ~7 K
    15.         
      # m0 E0 k* X. }% a( K' Y$ d" K
    16.         <context:component-scan base-package="task"/>
      7 b3 w! P3 g# F1 w# U( T
    17.         : c9 V3 j) f; o
    18.         <!-- 配置任务线性池 -->4 N" _& J5 U( i+ C1 K9 S9 h
    19.     <task:executor  id="executor" pool-size="10" /> 8 [4 \! b9 c9 O/ |5 s
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      2 z+ F- n7 g* u$ D
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>% o0 Y# G. ?" ^& h: N  I, X# k; A
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
      9 V; {- j0 K. u0 t3 l
    23.     <task:scheduled-tasks scheduler="scheduler">  
      " ~: e0 f/ f, E6 ?
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  
      , t1 }& U+ `3 X( _  \) n$ l
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  , N7 u; d5 g3 h  f- ^" a. O
    26.     </task:scheduled-tasks> , b8 e* b$ Z- A: T" v. D
    27. </beans>
    复制代码
    ' P) q, a9 E% v# Z) P& f1 I' R

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

    / ?: P# v! C9 y, ~2 t* a5 y( X
    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币)

    5 A3 E$ d9 y3 {+ Z, g4 |4 f

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


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

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

       

    关闭

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

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