我的日常

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

动态微博

查看: 3251|回复: 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,不然注解会失效(这个坑找了好久)。: P1 p) q2 e' s
    一、说明        
    $ ]% M6 y* {# U# K) S) V9 R
    4 y( a( |0 R% A7 y1 s3 Z
             以前项目一直使用Quartz的定时任务,虽然其功能强大,但是配置文件极其复杂,并且一个class下只能执行一个方法(貌似是)。定时任务多了以后对于维护xml配置文件时一件极为头疼的事情。
    % u- k6 S* ]5 P- B! U7 {! ~5 l/ c7 B  x7 S+ [' [8 l* K
    & [# b# O+ q5 ~$ s3 `4 R9 q( P) y0 {& Z
            前段时间把Quartz整合实例化入数据库了,做了一个任务列表,进行增删查改,的确是简单多了。在项目不重启的情况下可以对任务进行各种你想要的操作。如图所示操作:0 z5 P: z5 @  h

    , V5 d" g3 w" T8 S' ~

    - p6 w. }( J2 Q, s1 M& x) V8 ^/ D5 u5 Q/ k0 c6 R' N

    & z8 q: D" h% \% ]        但如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。" r, g" n& g/ |5 M; w0 A
    $ E+ |& T) ^- _7 h, g$ p
    1 ]9 p0 v, j0 U% T( r
           Spring Task提供两种方式进行配置,注解和配置文件。使用注解虽然简单,不用配置xml,但是相对于修改比较频繁的任务来说,打包编译的过程也是挺麻烦的,建议使用配置文件实现。/ d* r! ~) U, r  ]6 e/ h# w9 Y3 F
    / Y+ q; r# R8 Y, ^$ |0 ?

    & O4 }& u2 q5 {8 H& b5 s7 t9 Q二、配置+ M' H& T8 g3 s* L" S
    1)xml配置
    6 W: ^# t1 k' S1 p. D+ Z2 u
    1. <?xml version="1.0" encoding="UTF-8"?>
      $ a4 Q0 `! U) C. v
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      1 S+ L+ v! ?2 }) \# e2 M7 A& L
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      0 u0 O7 s6 i  ?& V0 y
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      ' w0 x. v$ u: I
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
        g; ~0 Y. o0 A$ X6 F  M6 N
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      & t+ j1 o* z$ d* m) c6 b) D
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd( @: ?. d7 L) {& W2 f
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd  n" i! M( E8 e0 w
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      8 q" R! l# F  U- v
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd9 u8 o1 A' _4 J5 O& |4 D) N0 ]
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      * e; I" p0 `9 S
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      0 K5 U3 C' {# a% L
    13. 4 g6 h) n8 X. N) W& h+ s
    14.         <description>Spring Configuration</description>
      & C: E- T! F# {$ O( n# K7 Y
    15.         
      % i3 m  X9 G6 l& C
    16.         <context:component-scan base-package="task"/>
      1 g( U0 g4 Q9 {. W2 |# Z8 H
    17.         
      # ^0 @' N4 Y! l
    18.         <!-- 配置任务线性池 -->
      ; n7 L/ h/ P/ `+ ?0 k
    19.     <task:executor  id="executor" pool-size="10" /> , [! |8 E; L- o! [1 b
    20.     <task:scheduler id="scheduler" pool-size="10"/>5 o- O6 A; s8 K& f3 F
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      % Q1 b2 g; j( m0 {' |
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->* O3 N4 u& X% h( ?" g( Y8 O' f, _$ l
    23.    <!--  <task:scheduled-tasks scheduler="scheduler">  
      3 x1 w8 v$ G. F8 R4 V
    24.         <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>  " B4 H' g8 M; w5 a
    25.     </task:scheduled-tasks>  -->
      / o2 h9 w1 e: J7 k8 t: n' C# {& e
    26. </beans>
    复制代码

    5 Z# {! H! P$ M" t2 B2)代码实现8 X1 o" K. c. V
    1. package task;& |2 T' b0 o* J8 ?0 M! T* m

    2. # z; r! P% N( Q5 J. f! ~
    3. import org.springframework.scheduling.annotation.Scheduled;
      6 y, @" p( I% k) G7 c
    4. import org.springframework.stereotype.Component;8 K- k8 Y# `, _6 b8 _! S
    5. , _7 T) K% A+ I# L/ @8 s
    6. @Component("TestJob")  5 F- ~  H8 }% _
    7. public class TestJob {
      7 R4 \; ~& [1 z
    8.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
      * G3 w$ B! j4 I& f: l
    9.     public void test1()8 e; ^( x  @) B: O9 o8 h, C- Z
    10.     {/ g0 P, [# c; R8 o9 ]
    11.         System.out.println("job1 开始执行");
      5 _0 }" W. N; C' p* h1 H2 X' u
    12.     }
      7 _6 Z+ H$ ?" u' N  U
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
      * j7 V: \8 R# m# M6 X0 u8 ~1 }. `- R2 h
    14.     public void test2()' s3 p. {8 W6 X8 n' t' L3 b- \
    15.     {% N. P, O: ~. m+ H5 j
    16.         System.out.println("job2 开始执行");
        V+ L6 i) w4 H1 D) T1 D7 I
    17.     } ! e5 w" H5 {4 X  Q4 I
    18. }
    复制代码

    & V1 ?: G/ U: {; d0 f; ~8 f* @" A项目启动后运行结果:1 b- c! U+ L' F7 P1 |

    % {0 q! d3 E% [2 D1 B% l

    " K- ~: h7 @5 R- }8 Z
    ; f! c& L4 W1 |" D7 R: g2 n
    3 D9 c# r; i1 \2 F( W! ?* [& D/ B2 a; W" V! y  g$ U

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