科帮网

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

动态微博

查看: 2486|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2017-04-22 14:46:31 |只看该作者 |正序浏览
    写在开始
    ; R$ n. C8 `6 T  y4 K一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。0 M/ b" D! G, `4 z/ B5 }# N
    举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。
    : P3 w! H$ a: D% z( ?7 R  U
    7 {+ Y7 A( \& B8 j任务介绍
    1 w* c, M1 N. _% {2 x, q* [就目前自己接触的定时人来来说,这里简单的聊下一下三种:& r. \& t& R% G  v- ]5 y# H( j) p7 k; Z
    + W8 K! z# P% Q9 N$ M, j
    1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。; s. u: t: y; e/ a& `8 t1 A' c$ n

    ( N3 q8 _; D+ L' z# N+ O4 C/ K2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)8 Z, @" {( W6 u/ a3 U2 g

    0 A& X0 ]8 f6 T* \5 A1 L3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。
    1 O. d8 M" u8 }2 W: j4 {
    6 h2 f% s3 x  L+ G: i使用案例5 A% N! R6 ]; M) U; |% L
    关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。, ^5 f& f! A; S6 J" x, ]+ z$ P
    4 n% z/ d3 ^; L8 I6 I4 R! y4 ?; |
    这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。, y3 U/ a' ~. x3 u+ D4 ]6 E' ~
    ; ?" J# ^" k9 P" r' l5 Q+ P. R8 P# V
    springTask提供了基于xml配置和注解的两种实现方式。
    , g$ ^& l; O: u
    - V# D% @5 R- N/ g6 |基于注解的实现TestJob.java:
    ; t, F5 I' t5 o
    1. package task;
      : f2 r7 f* M' C

    2. ; Q: L; G' P  t: y1 G, I
    3. import org.springframework.scheduling.annotation.Scheduled;
      , U# H* Z- J5 N7 D
    4. import org.springframework.stereotype.Component;& h" d& P2 ~) m8 D7 ^
    5. /**6 g/ L! W# |" y- r
    6. * 任务测试 科帮网 http://www.52itstyle.top/7 x0 Z% f( r" M+ c, e! ~/ o7 u
    7. * 创建者        张志朋
      $ f* ]1 u  O  g, O1 _% o
    8. * 创建时间        2017年4月22日  N( k* Z- |# x( e1 m+ I
    9. *
      2 s8 d. R5 C; B0 x
    10. */
      2 h& \$ r5 R  W
    11. @Component("TestJob")  ' g) A+ v* }4 q$ `
    12. public class TestJob {- W1 A% J1 ]# {) b6 ^2 I+ B" d
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 / ?: w3 l6 k. S" E4 g7 `
    14.     public void test1()
      % D2 f) V$ i7 g% d, f% s
    15.     {
      4 R7 v! h, v  L1 n3 z* f5 D
    16.         System.out.println("job1 开始执行");! Y; B: O7 i0 o' I9 D! I/ W, `
    17.     } - O5 n$ h# c5 q8 ]. N+ ~3 z
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 6 @" u' y0 x7 J: R9 u
    19.     public void test2()
      8 B/ L+ X! q$ d2 q* m* q1 U
    20.     {
      8 c( O& i6 k7 N9 h0 H
    21.         System.out.println("job2 开始执行");
      . i* a+ Z; U% j
    22.     } 8 T4 u! r/ \3 b  V
    23. }0 X% i# ~1 B# s( y
    复制代码

    8 Y# T1 d7 F* H# i
    # j& @, q! b/ v+ o基于注解的实现spring-context.xml:7 h0 i! K) }4 P" w6 d) R) b
    1. <?xml version="1.0" encoding="UTF-8"?>* w9 k  ^7 O# o* b% \; s1 k+ o
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      3 M$ k. f, t* M* ^& n3 m& T
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      ( W5 W( A. \$ D! M
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      ) h7 E1 Q4 Q) i! ~3 z' F
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="# N3 g8 U& c0 `3 G( R# W* d
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd% q1 l( r2 P' |$ E9 j; |
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      - G4 O5 Y1 R' m; T7 u
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      6 }* ]' I; b5 P' D2 M
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd% ^* T! f# J$ I7 ]4 t
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      ( s& l  J3 X0 p" z' s2 k. a
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      2 V3 y& V- e; m" p
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">5 |0 ~1 K1 F( g- f' l4 H, P

    13. % ^! ?+ u* g: ~( R5 [
    14.         <description>Spring Configuration</description>
      - D- L6 \. ?( Y
    15.         
      ) B# r3 \* v; l- @9 O
    16.         <context:component-scan base-package="task"/> # Y0 ]# Q+ z9 [" y3 S
    17.         ; Q6 R) j+ M9 P4 M4 z: ^
    18.         <!-- 配置任务线性池 -->
      - P. R9 z5 L( P8 U6 h) p5 f
    19.     <task:executor  id="executor" pool-size="10" />
      ; b% h7 {  s; d  d1 [4 P+ Q
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      . n) Z4 t' T" ~0 p& `; S
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>; @9 f3 p2 _  ^: R( k
    22. </beans>
    复制代码

    7 P$ J! T0 i: P! c; w
    / z0 I" J) g' R: p8 m
    ! A( C& J: v' W基于配置的实现TestJob.java:* I4 M: C& s% V0 H5 f
    1. package task;, W" {  }- ^; N. \' U

    2. - W$ L3 O6 ]6 T" p) e
    3. import org.springframework.stereotype.Component;
      / G; m. O6 J; r# L; l, l
    4. /*** T  I$ I+ e- y
    5. * 任务测试 科帮网 http://www.52itstyle.top/, ]2 n& T8 o; d4 p  x' a5 E  k' g
    6. * 创建者        张志朋
      ( g4 s, @0 w3 W6 n5 r
    7. * 创建时间        2017年4月22日
      $ _2 u' E9 a* T% }& C  E8 Q3 o
    8. *
      + ]' _) l3 u2 s# z
    9. */
      6 B1 j" q% K# v3 k) E
    10. @Component("TestJob")  
      $ M. Y0 `0 z: a8 G& @
    11. public class TestJob {7 ?4 u9 x1 W& H! i( t
    12.     public void test1()5 [$ A4 |; V# F6 Y: s( T- ^
    13.     {5 A9 H7 ]! A# u4 A% ^7 T" E( h
    14.         System.out.println("job1 开始执行");& @' X0 T+ s# h# U7 r
    15.     } ; n0 y7 y/ v% w. L
    16.     public void test2()
      2 P3 H( a: I2 m: D. _8 B
    17.     {
      ( r& S% o0 h* t7 X5 }6 B6 t
    18.         System.out.println("job2 开始执行");
      2 n" K8 K1 \+ v9 m& G0 q# Z
    19.     }
      / g! u7 @( R: w+ o, ~2 ~
    20. }
      ( g$ Z, j% _. k/ l! @3 i1 ?
    复制代码
    基于配置的实现spring-context.xml:
    * K5 T& ^8 Y" M
    1. <?xml version="1.0" encoding="UTF-8"?>
      0 i/ Q9 u" Z5 H# |6 o  f8 N: _0 y
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      4 e. S& g3 L0 S
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  : Y1 ^, h& u# q! n
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"( g1 U0 Q* y6 {2 }
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="" e0 W$ U4 B) `- _0 j2 N- s
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  {) j7 r8 C2 z# `1 W
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      ; l% e  g5 ^+ ]  D, ^
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd' u" {/ j0 t4 E: p7 F% T
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd1 P1 {0 ^/ M. O
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" \9 g9 [- R% z+ W/ u2 ^
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      7 M7 T5 d0 B4 ^% a" g1 r. X; b
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      " H/ ?) J( d, t% P8 G2 R. e

    13. ' h9 S, I$ |, O! M8 ^/ ]5 B
    14.         <description>Spring Configuration</description>: a! W* L: ]: S
    15.         
      * z4 K7 G" Y( F. u8 B+ X
    16.         <context:component-scan base-package="task"/>
      8 f4 ^5 Y, S9 A" ?9 ]& Z4 q
    17.         . e8 b3 X0 R5 G% v; P0 W
    18.         <!-- 配置任务线性池 -->6 P/ s. ]% X4 b# Y
    19.     <task:executor  id="executor" pool-size="10" />
      - o# H/ p1 B( f( z7 v3 l
    20.     <task:scheduler id="scheduler" pool-size="10"/>7 h4 [0 l, B( f9 h1 U1 D* o
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>$ l3 d  I' i, C0 J& r
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->+ Q- d6 x8 G* O& w8 ^& Z, [
    23.     <task:scheduled-tasks scheduler="scheduler">  
      & X$ l7 |8 p. O) Q4 \; |' b
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  
      . {- _7 V, C$ W) w( ?! e
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  
      4 X$ p9 Q8 }& @; @2 }
    26.     </task:scheduled-tasks> ' M" P: ?: q% ^, J6 f
    27. </beans>
    复制代码

    5 r, S8 C5 X; ?1 ?0 K0 r& ]  \( Z6 Z( S* y5 ^% 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    , - * /
    - 区间  
    * 通配符  
    ? 你不想设置那个字段
    下面只例出几个式子

    ; C* h5 e0 P- X, 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币)
    ! ]4 z. G; N3 i; O/ c7 X4 G

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


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

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

       

    关闭

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

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