科帮网-Java论坛、Java社区、JavaWeb毕业设计

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

动态微博

查看: 1716|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1242

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    发表于 2017-04-22 14:46:31 |显示全部楼层
    写在开始, N# K  o, m) G" I. L
    一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。, T2 E) V. A: m- Y; ?  A
    举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。
    & h3 T; b: s' W1 Y' C" p3 Z3 W) l
    & b/ b4 Q9 M* Q( U任务介绍4 F& B2 Y5 Q" E
    就目前自己接触的定时人来来说,这里简单的聊下一下三种:/ l3 e9 }( O( F& C6 o; Y' s
    ) t6 D, @' c7 ]* K
    1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。
    ; S& A4 L0 X, G: L6 G' T* m) c+ ?% o5 ?7 H: f8 e& Y1 `8 W
    2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)
    " b' V' V9 r# Y) u; k; o* p& R
    9 v4 P3 k" b) J( |3 |3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。
    3 A, H3 E0 _6 C7 W5 q
    5 @3 b7 A; I& w1 V; e使用案例- s' ^+ q) T; O3 ]
    关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。) B) v# h5 @$ R% b. D$ A$ ?9 o8 D2 b
    ' W7 J+ G0 I7 C4 K/ |5 z  U( s7 @# K
    这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。
    1 l5 ^- e1 T; x7 Z) ?% N5 P3 u. G. k1 w
    springTask提供了基于xml配置和注解的两种实现方式。
    4 ^4 ]* y( ~: d6 j5 g9 |
    7 E; O3 B$ G% m/ w6 Z; I+ x6 X基于注解的实现TestJob.java:9 |" W) r9 ?5 O3 v+ L; e
    1. package task;2 e. c7 ?" z3 m

    2. - ~) ^: N0 [; P. p: A9 U/ O+ q
    3. import org.springframework.scheduling.annotation.Scheduled;
      # A1 e% N, s, K" u0 Y7 c
    4. import org.springframework.stereotype.Component;# i+ p7 v6 ]& M; U2 x& H
    5. /**( |  T  ?0 ]3 ?5 a$ t/ P1 j3 x/ H
    6. * 任务测试 科帮网 http://www.52itstyle.top/, h, ~# S8 {$ l
    7. * 创建者        张志朋) p# m$ [* @' F9 u+ O
    8. * 创建时间        2017年4月22日9 _/ J: L, o( k& c0 o! ?( P6 |, d
    9. *
      2 e3 F4 o2 ^0 W, `' ~9 Y
    10. */+ B, m% ^9 d) ~4 w
    11. @Component("TestJob")  ) Z; F- M6 W3 W* \
    12. public class TestJob {
      & l7 U# K8 S; f" p' L1 n8 W
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      ( A& D: j  B7 z" C8 D  F
    14.     public void test1()5 A" y4 J4 ?; Y% a
    15.     {" v$ s1 v8 Z3 U3 P3 Q
    16.         System.out.println("job1 开始执行");' H( w5 m$ _% b5 X0 ~2 n( u! Z
    17.     } 1 _) @* `9 P3 m! ^
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      / S, X; [4 k8 X, L
    19.     public void test2()
      1 z0 `) y. R, G1 a/ W
    20.     {0 ^7 }2 q( I( v. {; _; ], N/ Q8 x
    21.         System.out.println("job2 开始执行");; M9 `% v- U5 n% c5 x
    22.     } % c) e' t! ^" f8 i* ^, b
    23. }
      ' v! _" W* {9 ?- n; L
    复制代码

    6 n  I' J6 d6 R; r( c) ?3 t4 l' p3 n7 Y
    基于注解的实现spring-context.xml:  i7 P+ E; J2 R- I0 p0 i; h7 C
    1. <?xml version="1.0" encoding="UTF-8"?>. k  r' \, W4 T) A' [5 f3 @5 ?+ l
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"6 }+ m/ y2 ], A/ J' {$ d# l* |8 }
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      & j8 j* D4 r' M+ _+ S7 m: b$ _+ V
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      4 |2 R- |1 Q+ B; g8 l
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="; h% t7 l: [9 d- a4 R- b1 \
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      1 Q2 m; ?, C( d) ]7 l
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      9 ^7 h4 G6 u# d0 }* J' J
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd/ }, v0 N2 O! k0 l( W
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd$ e1 ^0 D0 Z/ ?- ]+ @
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd0 j- a- o2 a! m) p
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd2 s( O; Y8 T2 R( v2 T9 J6 k# R
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      1 c1 r7 V& e. A8 J5 y) H
    13. / d3 N9 a& @% f. ?8 f2 }; N9 r/ F5 @6 L: s
    14.         <description>Spring Configuration</description>- O3 d1 o6 ~' |0 {8 L
    15.         
      ! Q1 B1 Y6 D/ x. l' S! F8 P3 v
    16.         <context:component-scan base-package="task"/>
      : a+ n+ D4 m9 z# ]: J
    17.         
      $ r9 m4 ]9 V- C, E$ P% L
    18.         <!-- 配置任务线性池 -->" G; ^( E, ]1 X9 W7 [8 Q* U
    19.     <task:executor  id="executor" pool-size="10" />
      ( r5 T% p: y$ E, j% J! ]
    20.     <task:scheduler id="scheduler" pool-size="10"/>4 I7 ]  g7 r3 {7 G; c  f; W
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      / \2 \  \: @! @% I
    22. </beans>
    复制代码

    . A) \! R5 X' x$ A$ G( L0 o# [1 e/ W! ~' C" ]% Z

    - y! K* \! y! V! |1 i基于配置的实现TestJob.java:! }3 S+ i% h# U( k0 L, m
    1. package task;
      ! u/ f. x% c+ C

    2. 3 N0 {/ {: A: Z# ~( R$ q
    3. import org.springframework.stereotype.Component;: |. c; `. r4 ]5 E1 L! M; O' M
    4. /**
      0 F$ g" r- }. A
    5. * 任务测试 科帮网 http://www.52itstyle.top/1 q+ B9 N. ?& k9 \. q
    6. * 创建者        张志朋
      7 \, A' E: L& e1 a/ V
    7. * 创建时间        2017年4月22日' g: f' ]! }2 T, t! g' Z0 [; I) j
    8. *9 M5 c8 N" u: u# h9 _
    9. */6 i* N% L1 S' O! r0 ^, F
    10. @Component("TestJob")  
      ( M7 f0 \/ P& y1 e7 L
    11. public class TestJob {
      ; Z0 k& o# K5 U  X7 a
    12.     public void test1()- |6 S3 `( J. G% I3 b% Q
    13.     {
      & l7 @" Z# M" K+ Q/ d( S: L9 n5 K
    14.         System.out.println("job1 开始执行");
      9 E: }, d5 V2 B% s
    15.     }
      1 {+ W' w% l+ e+ }& U- V' i' g5 l0 _7 e
    16.     public void test2()0 d3 a, N! G3 ?; m4 H
    17.     {
      % @6 ^/ ^' i, u3 ]% }
    18.         System.out.println("job2 开始执行");  r7 g: B# {$ w9 p* S, z
    19.     }
      8 f( E% \" M) J4 Q" U! N+ V" @
    20. }1 J  P2 _& W$ f+ j/ p- H; Y8 N
    复制代码
    基于配置的实现spring-context.xml:! g8 ^" c2 k' \+ P. i0 L
    1. <?xml version="1.0" encoding="UTF-8"?>
      ) G" C( A" M- D" u/ c3 @# V) g
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      8 w5 j5 W4 }9 l' W1 R) O" P
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  3 S6 Z- F" S9 C8 F$ Z/ {( p
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      . E: J# w3 K" q5 G4 A  ?. p$ C# K
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="  u1 J+ A. g2 y2 p9 M
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd! K5 Z9 F2 i/ P( q0 |# w
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd, x5 [( c4 Y, R9 z& k# }' H
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      / L1 Y' |2 d: [
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd' ~) d0 z! y( G6 T& B) k
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      $ ~+ w% h3 Q5 T8 a; u" J& C1 J
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd) E, h* E+ k4 f/ _
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      ) Y' y" @$ n, U

    13.   H; @/ N% q9 m7 _' ?+ \& R/ _
    14.         <description>Spring Configuration</description>0 j* i+ n! Y0 ]! K: ]2 y
    15.         
      8 c; e5 }6 U) o1 a* L% n5 U% R- }! L, @
    16.         <context:component-scan base-package="task"/> ! X  z, _  ^' `
    17.         
      , G& b- H  ^! J% [0 \9 P8 h9 y
    18.         <!-- 配置任务线性池 -->
      4 G- t7 }" W& F% s) D# @: U9 d9 M
    19.     <task:executor  id="executor" pool-size="10" />
      - {1 r' L+ b/ c+ i
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      ! l# z- ?0 q. `, {2 H; K6 w* B& {* Y
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      " P9 ~* o; `7 V
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
      6 L  [$ W$ p/ z' S
    23.     <task:scheduled-tasks scheduler="scheduler">  5 x  j' U: v5 K; a* |) l. g
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  8 H/ ]" o8 n: @
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  " v4 k7 F* b$ H& h( u. ]9 u
    26.     </task:scheduled-tasks> % s" E5 l% y  v" s
    27. </beans>
    复制代码
    0 G1 U/ R! g0 l

    / d9 O4 w6 \2 H9 U2 e
    附:
    cronExpression的配置说明
    字段   允许值   允许的特殊字符
    秒    0-59    , - * /
    分    0-59    , - * /
    小时    0-23    , - * /
    日期    1-31    , - * ? / L W C
    月份    1-12 或者 JAN-DEC    , - * /
    星期    1-7 或者 SUN-SAT    , - * ? / L C #
    年(可选)    留空, 1970-2099    , - * /
    - 区间  
    * 通配符  
    ? 你不想设置那个字段
    下面只例出几个式子
    8 N# H! h/ l& k: _, X2 i( Z
    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币)

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


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

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

       

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