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

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

动态微博

查看: 1989|回复: 0

SpringTask任务案例源码实现

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2017-04-22 14:46:31 |只看该作者 |倒序浏览
    写在开始
    2 ?* x  G* z( B9 d1 W- f一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。' l5 c& _  |! e& i% ^* j5 a2 b) o1 k
    举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。
    " z0 R# n, r% S: u) X5 \
    $ U3 \; g; A, \' x6 k0 P! K* t任务介绍  A) o% @! f  l2 H3 Q# Y
    就目前自己接触的定时人来来说,这里简单的聊下一下三种:* ~3 o* m( S  t* h4 I

    / ~+ w  j7 ]$ ]$ y% P1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。9 H. H+ J! S" C* S' e
    " ?2 Z$ Z) ?. N5 ^7 W
    2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)7 ?$ Q* U( G' J
    ; s' s3 a4 c( n* Q
    3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。! V) w1 n2 C# A2 @6 y0 y
    . ?) y6 k' B6 _  \  J
    使用案例
    : g$ I; D' r# n6 I4 j关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。
    + e  V- e. U. v( g* q5 x! l- Q& j; P5 J2 K
    这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。+ p2 u: _# a  {3 V& Q4 Y0 C( b
      t) `' i: |3 ~3 v5 e3 ?  `0 G
    springTask提供了基于xml配置和注解的两种实现方式。4 P. B, [- ^( M
    8 W: K3 I  F3 n8 G
    基于注解的实现TestJob.java:& q! y/ Z$ U) Y% d. F
    1. package task;4 N3 K, o& T! Z' ]
    2. ) g2 u4 M! G. \' F9 n
    3. import org.springframework.scheduling.annotation.Scheduled;
      ( I( @# @& s# p3 j; k$ T9 R
    4. import org.springframework.stereotype.Component;
      " r( ~# d" M0 R9 D+ m# ?+ K4 r
    5. /**
      4 y+ H; m$ |& Z, v
    6. * 任务测试 科帮网 http://www.52itstyle.top/9 s$ r6 J+ S8 t( E
    7. * 创建者        张志朋
      " w( R) S5 l% y2 i  J
    8. * 创建时间        2017年4月22日* r- ~( H& P9 E  X+ Z7 j& O
    9. *0 M! h+ l5 n, A1 x! k
    10. */' W; v9 g- j" G" G8 {/ q. P/ D
    11. @Component("TestJob")    _$ B5 S# ^9 m6 Q5 |/ V
    12. public class TestJob {
      6 M. v0 T4 u, v2 `
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 6 z  Y) J5 s: b/ s1 o# v7 i
    14.     public void test1()  P# A& L5 W" j/ E9 Q& b7 Q/ |: T
    15.     {4 W2 E2 T! Q9 i  ~5 r: `
    16.         System.out.println("job1 开始执行");
      . p# A* N' }1 q% N7 A
    17.     }
      + V* H$ r& b( B, x$ U3 Z
    18.         @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 : C( c9 b8 @6 O/ v2 u! ?
    19.     public void test2()/ O" u* t3 E$ x- D
    20.     {( i. J3 i2 G/ V' ]
    21.         System.out.println("job2 开始执行");) {9 e5 H* F* v
    22.     } 3 N- p, M4 c+ l. L: y
    23. }. b5 Z0 K5 J4 H0 _$ o3 A9 @
    复制代码
    0 T# q# l2 l7 S3 x% [% G1 I
    4 x4 \* o9 d. U: c# a  L: G
    基于注解的实现spring-context.xml:, \: N. C7 `* S
    1. <?xml version="1.0" encoding="UTF-8"?>( |) k6 ~5 \  I4 e- V# R
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"( P1 n7 y- J$ J2 X
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  ) l. m( d! e0 o
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      # r" L& W8 g% N7 m2 t
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="- L4 o; ~  p9 g! R3 X9 z9 S, C
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      ! z$ _1 u) P. Q" u8 D* O0 `  g
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd) i) `0 t# G( F: F
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        y; m' y' n; z; e+ z
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      9 y8 P1 s6 w# J
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd$ s" d9 T- k+ r/ M3 _
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd* F- Z& Q0 `) q2 ~
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">: o9 \- L. [( n& q) y% I! H+ b" g* j% y
    13. ' t1 p$ o0 q6 n8 o
    14.         <description>Spring Configuration</description>4 T, f$ k0 k$ R9 ~( x( {8 K
    15.         
      3 J0 R0 M" X& d9 `% I4 L, F+ {/ U
    16.         <context:component-scan base-package="task"/>
      1 D/ H/ `- f8 H+ _
    17.         
      ; P+ l  P( G# |* e' O9 S
    18.         <!-- 配置任务线性池 -->; r& D! y. P6 \! \
    19.     <task:executor  id="executor" pool-size="10" />
      3 i3 m7 Z$ P& D7 t! g! B
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      8 V. i/ X" r6 _0 y
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
        ^$ y- l2 n& b6 a
    22. </beans>
    复制代码
    7 H1 f! o" a, `2 p7 H: R7 c- Y
    , W) o% ~" V: @% a( L; b5 i
    1 W5 S3 }% K+ R: O5 r
    基于配置的实现TestJob.java:* y1 g+ K+ X3 ^* M1 E
    1. package task;! T  p+ C  K! r; ^5 B
    2. $ i1 H5 r% J  ?2 _; U
    3. import org.springframework.stereotype.Component;) R. z0 |% ^3 s9 `8 s4 Z
    4. /**
      & q) e2 n# y: @6 A) F! {( Q
    5. * 任务测试 科帮网 http://www.52itstyle.top/$ d& ?- F8 h% ~: n9 E: r
    6. * 创建者        张志朋
      # F# x& M4 D: I& p1 _! j6 N
    7. * 创建时间        2017年4月22日7 p! A4 ^7 A0 n. w1 ]
    8. *
      # {. Z2 x; [3 b
    9. */
      " s0 l8 q! P3 K8 q3 F# W
    10. @Component("TestJob")  
      & W5 x. e6 b+ J+ A1 z5 b6 X* T) t) H
    11. public class TestJob {0 y% [- s) x& {. q( E4 M: t
    12.     public void test1()
      ; @/ n# Y2 r( E( r; L
    13.     {
      & _; S. J5 w' Q- R3 q6 `
    14.         System.out.println("job1 开始执行");. X4 e) k' Z  {; _
    15.     }
      6 d/ i1 F/ U$ H  {# ]+ @8 s
    16.     public void test2()
      . \6 ?/ S% ?$ _4 {5 p* v1 K
    17.     {" H5 M8 b. l4 g) S  h- A4 N7 o
    18.         System.out.println("job2 开始执行");  s8 z# B+ b- f& \
    19.     }
      1 J/ `/ J- v: Y/ W
    20. }
      8 h2 o6 G- n3 {3 ?9 Z& z
    复制代码
    基于配置的实现spring-context.xml:
    6 r( j7 ?/ D" p) o% Y2 ?, N8 H
    1. <?xml version="1.0" encoding="UTF-8"?>
      1 {  v$ l5 _0 k0 h
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"5 \& k$ J. m! J/ p$ @
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      2 K/ A5 P3 B' F3 `
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      + i1 \4 ~) D; r1 M& J' S8 @+ Q
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
      ) c" R2 y8 J. O
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd8 |# G6 Y0 K2 o: c$ L& F$ [: y
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      6 J* [4 m' r8 ]
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd5 O! ]. \4 M3 R) u7 b
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd9 z* }/ o1 |4 c9 S  j* a' P
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd; e2 N. k0 p, _% A8 w
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      $ P; p: J* Q5 ?
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      # T6 L5 X8 p1 J: {! t

    13. 8 A4 c0 ?" c, a$ N9 q, b
    14.         <description>Spring Configuration</description>
      / g/ v2 S  \. c2 i7 ~2 e2 z5 K# W
    15.         ! s" I/ t/ H2 R' |7 E$ W
    16.         <context:component-scan base-package="task"/>
      . x! _" Q! l0 }: d7 F
    17.         
      " K$ p) R3 J+ b7 K& {; N& E- Z
    18.         <!-- 配置任务线性池 -->
      , ^  M6 y) b  y$ G
    19.     <task:executor  id="executor" pool-size="10" /> 0 x; J% D, b3 a0 z4 e0 F
    20.     <task:scheduler id="scheduler" pool-size="10"/>: l7 d2 J& z3 Q8 L/ n, z
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      8 k" X) M, h2 i
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->0 \% p: O" k( _/ J" D2 L% h7 ]
    23.     <task:scheduled-tasks scheduler="scheduler">  & h2 E3 [7 I. {; W% L
    24.         <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  
      + \" Y' J3 B7 W; R+ C. w/ u
    25.         <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  
      - ^) t2 P" h, Z% b  G9 Z
    26.     </task:scheduled-tasks>
      / O( y7 _# e. z& }% h) W7 s! L
    27. </beans>
    复制代码
    $ y' Z0 u; Z) q& `
    ! X" k# |5 y0 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    , - * /
    - 区间  
    * 通配符  
    ? 你不想设置那个字段
    下面只例出几个式子

    0 \0 v3 p. G/ _! W' F; }8 @
    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币)
    ) n2 I( {+ {8 a- S0 F  W

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