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

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

动态微博

查看: 2036|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2017-04-22 14:46:31 |只看该作者 |倒序浏览
    写在开始/ D( @: v$ \' W& C( L5 a4 {
    一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。
    $ s- x/ @/ k+ H4 X; u' |举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。
    & p2 A( D: T6 K8 \
    1 x. d* f& H: W) p3 F任务介绍
    4 N, I5 y8 C& j3 z. I3 m就目前自己接触的定时人来来说,这里简单的聊下一下三种:4 t  Y9 [5 |: ?- \  S4 w5 F4 w
    1 n7 R* w! I! h
    1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。  L4 q# {7 c( ?; t

    5 \1 r7 [) q: O2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)
    ! @- i% u* _/ m. }- s0 v7 Q5 Z- [; i8 }! C* B* ^* d' @% T
    3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。# S4 k" ~6 ~! T& W# K  f
    ' c- S: q/ s% M' `% L3 F
    使用案例
    : y* T; i: b, S关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。
    & ~0 F7 t! A1 P. ?# v; o* F
    , [3 w- ]& z2 W+ I( h. Y& ?这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。2 e  T7 |, P9 j- V  m6 T4 c  o
    ( Z( N5 G; l+ i% u/ f% J
    springTask提供了基于xml配置和注解的两种实现方式。8 E: V" e' W: H: c
    4 d7 \4 J. Y. w9 _  F7 {
    基于注解的实现TestJob.java:
    ! S' Q8 e) Z0 {" {4 G0 s* s" b
    1. package task;& ?6 [( T1 S+ X7 \
    2. 0 ?1 M4 G; M' o( }) |# A
    3. import org.springframework.scheduling.annotation.Scheduled;, h" z% [9 z7 y$ r5 K& O
    4. import org.springframework.stereotype.Component;3 A" j; i. y) U8 w/ g6 m& ^
    5. /**
      - N+ g: B- c4 ]2 E6 K
    6. * 任务测试 科帮网 http://www.52itstyle.top/; _- ~& q/ X: d# D# J- r
    7. * 创建者        张志朋3 Q) P/ j  H1 z& T9 s) N* x
    8. * 创建时间        2017年4月22日+ |6 e/ p( o  p& D" b! d
    9. *
      % L) y+ a3 G9 q7 j, M, z. }
    10. */
      , i' |2 @6 V! o  R) ^
    11. @Component("TestJob")  
      ( {9 p: M9 j- x
    12. public class TestJob {
      - @; t, [4 }& y: Q( N
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      ! Q- X8 a8 D/ }' E( H
    14.     public void test1()! {! N& I7 q& C& c2 _9 H/ D: z/ S
    15.     {0 ]$ Y0 H& s2 [( F! ~! x% ]/ m
    16.         System.out.println("job1 开始执行");
      ) _0 q8 i6 Q, z) N4 }- E
    17.     } + R* Q+ i- P* ~: e, X$ K7 ^
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次
      0 q% B6 k! x& _* e5 W7 y' a
    19.     public void test2()
      ! p: O  y% `/ q
    20.     {" S8 F7 B  R; u* q1 `
    21.         System.out.println("job2 开始执行");
      ) ]5 {0 [. U7 L
    22.     }
      ' S7 @  D, Z8 J. i/ a+ @5 t- |
    23. }
      2 Y; u) m$ M6 v2 l6 x
    复制代码
    : V; R- Q1 T; z, I. O

    0 Y4 M2 G* i6 z' h5 F4 W- A基于注解的实现spring-context.xml:8 P. E$ F4 \! E7 E
    1. <?xml version="1.0" encoding="UTF-8"?>5 N: J% r, Y) K
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance": \% S/ {" m1 f/ ~
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  ! S6 N0 m2 z: N4 t9 i# l9 ~
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      " U1 R, K9 c# I4 ~3 y
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="3 P+ G( V. r% V. s$ m# @8 }
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      * @" c9 t7 ^* O7 W* a
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      0 q! x* l+ A5 r% W
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      1 `8 r7 d/ m' X- i
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd, G# A* o& R2 Q) l6 m
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd: w/ w9 |9 ^/ b* W8 H% h
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      & r! C, G/ Q& i4 U" ~
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">( {6 v; b1 w7 ^8 }* C; w
    13. . \  q/ Z1 R3 H; f/ W
    14.         <description>Spring Configuration</description>6 z1 t5 t/ ~) Y2 c) t
    15.         
      + h# @  ~$ M+ V7 q% L
    16.         <context:component-scan base-package="task"/> % x, l* T) H  B$ J
    17.         + P7 }  L! M2 L- T
    18.         <!-- 配置任务线性池 -->" `# E1 D& f& g
    19.     <task:executor  id="executor" pool-size="10" /> ; u8 r+ A% c5 A& v& z
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      3 z! @& k" ]3 `5 G2 o5 h
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      " P* V! ]0 B% J2 s* W1 p
    22. </beans>
    复制代码

    0 k' S+ R7 Z# v+ n0 P  x! m0 V5 {) O  d! d: T0 Z0 d; o0 [

    : n4 j' b2 g! s) h- j# c( K* M基于配置的实现TestJob.java:8 O" {: T! ~, V& V/ o
    1. package task;3 Y- `; _; l1 @/ r

    2. 7 H, H# k) J* G4 G& f3 S8 t& g
    3. import org.springframework.stereotype.Component;+ P5 k* r5 z% |. f7 K3 E# e
    4. /**, W- W5 x! @6 G7 ?4 t8 D  z
    5. * 任务测试 科帮网 http://www.52itstyle.top/! K6 }8 {# c3 A1 u4 s, O
    6. * 创建者        张志朋0 B, U0 X7 s1 P2 b$ @+ Z3 v
    7. * 创建时间        2017年4月22日
      ; t8 f2 P* h$ I1 a2 c
    8. *! }, J  a* k% f
    9. */
      & k2 b* g: a. T! S' V, W8 I
    10. @Component("TestJob")  , w, N% j& H. ~. o; O  U
    11. public class TestJob {
      + z; }5 C, u3 p& p1 r
    12.     public void test1()) s; {/ j3 ?9 Z  J; [: P
    13.     {- M9 ^5 J& f: h1 b. x
    14.         System.out.println("job1 开始执行");
      , p( P  X7 J* ^+ J7 {
    15.     } 5 v' S8 y+ ^+ R/ ~, L; B6 _
    16.     public void test2()& S7 Y/ u1 p1 m% Z( `! p! s7 A
    17.     {
      ; y, O9 X# T9 u6 `
    18.         System.out.println("job2 开始执行");; l/ P0 L2 R* y
    19.     } 3 [* A" \5 R/ V' t1 k; V
    20. }
      & B, A8 }6 c8 o6 n* g, j& f& h
    复制代码
    基于配置的实现spring-context.xml:
    * k9 v8 m5 ^* K8 O+ l
    1. <?xml version="1.0" encoding="UTF-8"?>
      0 j# O. Z1 }# Y/ ]; M% w7 ]/ X
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      - h9 h; M, N% I# J( T& Z2 X% A
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      5 F2 M; n; o$ F1 |
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"9 A. m" r& B' v; v6 g% R# ^0 k, K
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
      ( \0 i6 G6 j. Q- w7 l5 y% e/ g0 s2 J
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd% }; |2 _6 ~: a! L% i: L1 n
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      ' k+ N) m! u. P* q# z
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd. f' S% f4 u8 v$ t8 {
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd) h6 X. J& U9 `3 B7 U
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      7 \1 B& y, m6 F% f
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd. F' ]8 i! p' ?+ y0 [' a5 t
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      $ }9 v; C+ \1 D, C# J% K9 {
    13. ; Z1 F( N; I9 W) Z4 Y# F
    14.         <description>Spring Configuration</description>3 k1 D# l  C1 c' X0 P) H/ p# Z
    15.         ' x" ?) l: F8 l) P# P
    16.         <context:component-scan base-package="task"/>
      " K; ]- o4 b, T. u
    17.         3 f+ I6 I) k; z! l5 G8 l" ^* u
    18.         <!-- 配置任务线性池 -->
      " k6 t- O  d$ Y
    19.     <task:executor  id="executor" pool-size="10" /> ' b5 }5 k# t* O/ u0 P  F' q
    20.     <task:scheduler id="scheduler" pool-size="10"/>! o9 V; M  y- b1 F; j  p% t
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>+ F! y1 }$ m$ l$ @; [
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
      5 i8 X1 b" d0 d- B
    23.     <task:scheduled-tasks scheduler="scheduler">  
      % W3 e# q4 \( h
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  ' l& d; |4 \) }  I/ _
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  
      5 h% f: a9 _( V8 r$ G: d* v# C
    26.     </task:scheduled-tasks> + ]* t& ~5 r3 W# N' g
    27. </beans>
    复制代码
    / I' {3 X1 ~5 Z- b
    ( G9 z1 S$ ?7 ~9 ^
    附:
    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 x+ C! `. g/ r6 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币)

    # I! F* @$ o& ~& U  M) O

    科帮网-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群 科帮网手机客户端
    快速回复 返回顶部 返回列表