科帮网

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

动态微博

查看: 3296|回复: 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,不然注解会失效(这个坑找了好久)。
    # D/ h  _  ~/ P1 Z7 [0 L一、说明        , Z  {& q" C8 h
    ) f9 r) @4 h, j1 v; r
             以前项目一直使用Quartz的定时任务,虽然其功能强大,但是配置文件极其复杂,并且一个class下只能执行一个方法(貌似是)。定时任务多了以后对于维护xml配置文件时一件极为头疼的事情。
    ' L( w! ^( Q( K, E
    6 D1 t* p4 U  r" r

    . u* R0 d3 W5 ]: t, i        前段时间把Quartz整合实例化入数据库了,做了一个任务列表,进行增删查改,的确是简单多了。在项目不重启的情况下可以对任务进行各种你想要的操作。如图所示操作:
    ( o$ k  r- ]/ w0 x
    & T9 b( `4 [' q5 t& m- L
    0 H& h! M( X9 u7 |

    # J5 L5 D: u# \) O

      a5 H5 I& }" N8 w        但如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。. K: R: l6 V& j+ z% a7 ?2 W9 g# a

    ) p3 b$ R( b7 c
    ' A' y" q' c9 a0 o
           Spring Task提供两种方式进行配置,注解和配置文件。使用注解虽然简单,不用配置xml,但是相对于修改比较频繁的任务来说,打包编译的过程也是挺麻烦的,建议使用配置文件实现。
    : Y& D( t1 i; A2 ?' c' C0 W0 O: s! t" I

    ) u" q0 p  M1 _5 w7 f二、配置( b" R* r: U% W# I3 _, X
    1)xml配置
    + E) K. ?9 Z9 H  ]1 ?
    1. <?xml version="1.0" encoding="UTF-8"?>9 H" o2 ^! b. o5 l( }# n
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      ' ]% H0 r, D& G6 t
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      ; q4 B3 T# G! i) C  r3 I5 G5 B
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      9 r' Z5 ?3 w6 l! Y
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="2 ?  `( E' V# F; [4 W& c, G
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      ) H, P/ o9 M* z1 n* N
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd# j& ]% z1 M2 y7 q4 p3 M$ W
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      9 n; I, Z! {' h2 M" ^) h
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd5 F& H- s; V9 ^
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd) Z- \4 P' j- q: ?9 B
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd0 T6 e9 ?- U; z3 y+ N2 s3 Q
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">8 E" U3 i0 [& s6 }. Q7 M
    13. . \$ k' G  Z) u
    14.         <description>Spring Configuration</description>
      + K) a- {4 ~; e& G  [& w
    15.         
      ) R* T& s; j/ H8 t6 O0 _
    16.         <context:component-scan base-package="task"/> % z- j" v& J8 s7 X2 B; j  j: P
    17.         ) u% P5 Q: b9 ^. e4 x8 R
    18.         <!-- 配置任务线性池 -->
        r. J- e1 M, f
    19.     <task:executor  id="executor" pool-size="10" /> 2 G7 E2 R3 S' _, Q
    20.     <task:scheduler id="scheduler" pool-size="10"/>/ }/ V7 _, y8 b6 J0 Q/ }
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>7 P; M) `7 _% a" J2 s9 r
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->! T! d" t7 A% E. q/ I
    23.    <!--  <task:scheduled-tasks scheduler="scheduler">  9 z$ P) q  k+ M( x0 W
    24.         <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>  ; V# W8 Y" U2 w- E1 H$ o
    25.     </task:scheduled-tasks>  --> ( J9 a& h1 D( E& t8 u
    26. </beans>
    复制代码

      b# ~- F/ @4 z. w2)代码实现
    ( `. M6 H, C; L% k+ I+ A
    1. package task;
      7 T% N- B6 t4 j: @" D

    2.   s' s9 O  b$ _3 \, y) E
    3. import org.springframework.scheduling.annotation.Scheduled;* h$ S7 m2 g3 v# |8 ]
    4. import org.springframework.stereotype.Component;
      * x+ E) ?3 I* c* X# q+ M
    5. " S) B# q/ Z* J7 E; J2 a- g- L6 X
    6. @Component("TestJob")  ! V) A$ w: x7 Y
    7. public class TestJob {
      ; {  |' l% D3 |/ K8 F2 `3 R
    8.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次 , i6 B  ~0 S9 ^" j4 J
    9.     public void test1()5 X: j9 p3 x+ F) y
    10.     {/ X& W+ |! T' a1 ~1 E/ D- H
    11.         System.out.println("job1 开始执行");
      / G( |! e: d! k& l/ ^+ \/ k
    12.     }
      5 [- f; J& B( }7 S- Z, H
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
      9 j5 s# k, F$ U
    14.     public void test2()
      4 n) P) ~) o6 v7 Q7 \7 z
    15.     {/ _+ y; [& h, Q) q
    16.         System.out.println("job2 开始执行");
      " A" K: e' u! s  g1 ~1 A
    17.     }
      / w: H7 H4 ]) v6 J  M
    18. }
    复制代码
    8 ~. r# v' o' |& t8 C! K' |
    项目启动后运行结果:) G+ l! @; g/ G& S4 J
    4 g8 }4 m$ i) p/ J5 M( G

    ( N! l% G- g/ }* L9 k7 r
    0 k" b% h0 C7 x' C5 _) e' @; l3 L9 M6 {- O

    ! G+ a& J. }4 h* C

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