科帮网

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 项目源码 > SpringMvc自动任务调度之task实现项目源码
总共48087条微博

动态微博

查看: 3295|回复: 1

SpringMvc自动任务调度之task实现项目源码

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

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

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2016-07-15 11:10:31 |只看该作者 |倒序浏览
    前提注意:配置文件中如果 default-lazy-init="true",删掉或设置成false,不然注解会失效(这个坑找了好久)。+ i: A7 j! z5 U) i9 t2 q
    一、说明        
    $ `1 y8 Z6 ^2 M( h: j/ A0 Y$ X
    1 f/ {9 K9 e) D8 h& n
             以前项目一直使用Quartz的定时任务,虽然其功能强大,但是配置文件极其复杂,并且一个class下只能执行一个方法(貌似是)。定时任务多了以后对于维护xml配置文件时一件极为头疼的事情。0 f# m9 \, w+ J( ^- ]! W" y
    / K- M2 b7 B1 r4 }! a/ J# w
    7 g, i$ Y$ e& P( j! V
            前段时间把Quartz整合实例化入数据库了,做了一个任务列表,进行增删查改,的确是简单多了。在项目不重启的情况下可以对任务进行各种你想要的操作。如图所示操作:
    . r0 t- j  Z6 [$ d4 y6 y, h' m7 X' F
    & W; o# ]$ H: ^/ R& i. {' |6 m: i

    % |- Y& K) R7 C) m/ ]$ g% Q( H/ W$ W: F; a
    3 T/ d$ Z% z  i0 U
            但如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。$ ?0 `) I$ a6 }$ }2 ?% R, F

    ) k7 R6 S8 `! H4 j" E/ s

    8 {# l+ k6 R: Q. P       Spring Task提供两种方式进行配置,注解和配置文件。使用注解虽然简单,不用配置xml,但是相对于修改比较频繁的任务来说,打包编译的过程也是挺麻烦的,建议使用配置文件实现。6 [' p1 c: g+ x$ s4 ~0 C# F: }: X

    5 ~: U8 h3 F+ [/ m" L5 x8 b6 S9 A* W
    二、配置0 }7 Y2 q1 X7 ?, M- y& z
    1)xml配置
    0 F* G; x" |5 q9 ~7 p& B
    1. <?xml version="1.0" encoding="UTF-8"?>0 H8 m8 O8 L- _7 m
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      $ f% _2 m! B8 C5 _3 x; ?
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      7 ]/ }% E. r1 f2 Z' |
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      5 c% E1 e- s+ V! L3 M
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
      4 u  w2 q4 i* q( o
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      * p& R- t; e7 F6 A8 w! i
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      * Q$ p% E1 d# a! H, H3 M8 [% j
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      ; t- J4 f& m( L+ K, n
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      2 z( }  z9 R) [
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd- p0 |4 n; @9 O+ }
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      ; `! w) Q( I' b% p& Y- g
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">6 a1 B4 q" m* @8 N$ z

    13. ( t+ T/ N' v9 V& B8 H9 P
    14.         <description>Spring Configuration</description>6 i1 G0 @8 _4 Z0 c
    15.         
      ( n  n. Y. U0 q; T) E" z
    16.         <context:component-scan base-package="task"/> ; Q. A9 ]: h8 d6 V3 F; R
    17.         
      ! n/ r/ r' I& c9 o) f' H4 Y
    18.         <!-- 配置任务线性池 -->0 U/ p2 H0 }( Q
    19.     <task:executor  id="executor" pool-size="10" /> . G* K$ I% G0 `. q6 L
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      4 B2 e  y7 I: @( ]; x4 b. ~
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>9 g7 s- K; c# g5 p) R
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
      0 P1 i, R7 {. L1 u! b
    23.    <!--  <task:scheduled-tasks scheduler="scheduler">  $ T: M2 A  `# _( V! B4 B2 c
    24.         <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>  / _- ^2 G4 j3 o# T, ^
    25.     </task:scheduled-tasks>  -->
      ) D- G$ p0 S! M6 N" N5 u
    26. </beans>
    复制代码

    & ]7 i# [) }" Q  b2)代码实现, U% o2 d4 D0 x4 T* `
    1. package task;
      1 {+ P# M) D: X( Y( L# g5 j

    2. + A# y7 c# B! m( v! H
    3. import org.springframework.scheduling.annotation.Scheduled;' M2 h) }+ f+ s9 v  q+ h8 ?7 m
    4. import org.springframework.stereotype.Component;6 l4 v$ Z5 `$ p% u& E: Z; g9 S. K" N

    5. 0 e  _% M* R1 i* [8 p% p
    6. @Component("TestJob")  ( W# ^7 t1 \. |$ @5 Y- w6 r
    7. public class TestJob {  O1 R  H! w* {# F. ~
    8.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次   R; f" U# f% V% o! k3 B% N
    9.     public void test1()) F+ m% R0 x0 R- H5 i
    10.     {
      ! F# F* o4 t: Z7 a7 ~# x( b
    11.         System.out.println("job1 开始执行");
      ( ]' \' r% }3 [# Y; [3 p% t! c
    12.     } 7 Q" Z, h; z9 V+ d. {6 E
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
      $ A" ?2 t2 h9 L! L  W' F
    14.     public void test2()4 L$ Y! D) M  Z
    15.     {
      9 x$ O  [! D  |. r% f
    16.         System.out.println("job2 开始执行");  V5 ]3 E. F; _- l
    17.     }
      ! m. \$ h/ y! x2 v+ @4 k( g/ p
    18. }
    复制代码

    " T6 A/ A3 b& U; b% m项目启动后运行结果:
    3 w2 e# _1 K8 J2 r- d$ H4 m: J5 A) w, t* o

    8 U$ d5 D9 P; B( X& n! L! F
    * ~2 @9 Z5 E/ x) I1 i4 U5 e& p  ^, m

    ; T. n9 x* ^8 I( S; ^" w0 g

    CSDN下载:http://download.csdn.net/detail/zhulin2012/9576741

    百度云下载:http://pan.baidu.com/s/1boW6WP5


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


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

    0

    主题

    0

    听众

    104

    金钱

    三袋弟子

    该用户从未签到

    沙发
    发表于 2017-12-20 18:33:33 |只看该作者
    额1111111111
    回复

    使用道具 举报

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

       

    关闭

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

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