我的日常

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

动态微博

查看: 3249|回复: 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,不然注解会失效(这个坑找了好久)。
    : x) Q6 R2 k' {" b* Y; S一、说明        
    8 g/ u8 P# F0 M. K! U
    , }. J9 y/ z# \: D6 n( V: d1 V2 A6 A
             以前项目一直使用Quartz的定时任务,虽然其功能强大,但是配置文件极其复杂,并且一个class下只能执行一个方法(貌似是)。定时任务多了以后对于维护xml配置文件时一件极为头疼的事情。
    $ e/ D+ e: y7 X+ ~2 {9 y4 H2 W9 {/ S3 Y: i; p; ~- }' o" t0 i+ C
    * M5 [( r( H- ~3 N5 @3 v% o8 s
            前段时间把Quartz整合实例化入数据库了,做了一个任务列表,进行增删查改,的确是简单多了。在项目不重启的情况下可以对任务进行各种你想要的操作。如图所示操作:. t- T5 d1 b+ x! j+ ?2 G
    ' h  I. G3 a5 I9 f& H0 L6 |

    : J- Q. R+ i* i2 K/ @. [( E6 B( Z/ ^$ f2 o% P% S/ O& E0 _2 P$ S
    4 c' t6 M$ _0 P, M# _, j) \
            但如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。4 R# y4 V. a$ |6 i: m- ^- [

    8 }( ]5 T3 s& c1 B
    2 G% m1 |: V: S5 c+ Z/ U
           Spring Task提供两种方式进行配置,注解和配置文件。使用注解虽然简单,不用配置xml,但是相对于修改比较频繁的任务来说,打包编译的过程也是挺麻烦的,建议使用配置文件实现。% p0 n7 ~; ?* }' i& ~
    3 C5 H' d4 H3 ^9 x
    6 }$ j; h9 c7 i) x9 G  R
    二、配置2 m) ^: d& F" z$ s5 t
    1)xml配置8 @/ I, F, T) p' W0 f. m  N* t
    1. <?xml version="1.0" encoding="UTF-8"?>/ e/ N# n5 c$ B
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  v0 v0 c  J; Q) O, @! w
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  + s5 O! B* |$ d( Y+ S9 |
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx", K- d/ h' P4 {$ H: O8 @
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=") q; e4 I4 C; a+ `% e# X# T2 G
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      ' m7 ~4 D, A& o7 v% v5 c0 w
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      + E2 ]; b/ n- w6 `3 Q- B
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      ' z2 j" w5 S) C) s
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      1 c' Z7 B1 c+ }
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd2 R3 ?% P$ K2 T
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd8 c% l# ~( G& U1 s8 `) ]- I9 A1 S
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">6 H) ]7 \7 Y0 @% c4 \& g

    13. 5 l# x8 C# `& D. O5 E6 [: R* [4 H
    14.         <description>Spring Configuration</description>
        f: \$ H, T9 t1 i/ q, B5 @' k
    15.         ! }, Q0 D5 V! B+ W% V
    16.         <context:component-scan base-package="task"/>
      & N+ D3 ~; l- O* k  r
    17.         + @/ K; Q7 K& V( Y
    18.         <!-- 配置任务线性池 -->
      ) O. G1 D8 Q# y! m, D
    19.     <task:executor  id="executor" pool-size="10" /> # j! @; D* M' P1 ?
    20.     <task:scheduler id="scheduler" pool-size="10"/>& K# @, X5 T8 T
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>- K; ~) s7 {! D3 Y6 }8 B& X
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->/ w* }2 J+ Q; ]0 f  n
    23.    <!--  <task:scheduled-tasks scheduler="scheduler">  - T& M1 L& [' i$ x1 r3 `1 ?1 z
    24.         <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>  ' @( h# W2 d; ]7 q
    25.     </task:scheduled-tasks>  -->
      5 y) F" T! r7 \, v6 O
    26. </beans>
    复制代码

    0 ?; j+ g  t6 f& r5 Y2)代码实现, a; J( _/ x$ I1 u: v& q% {
    1. package task;
      9 Z0 Q$ w& c! x+ u

    2. * j( a+ F2 w; P7 j- w6 _: S0 y
    3. import org.springframework.scheduling.annotation.Scheduled;( S& |9 y$ D- h6 |" t
    4. import org.springframework.stereotype.Component;
      . p6 I: N: O2 g

    5. ; b$ t! B  F, g+ t
    6. @Component("TestJob")  
      ; d& \$ k4 y' e( N
    7. public class TestJob {5 d" a: s: g& r5 s, B7 _
    8.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次   w+ g0 Y" W1 Q. M  D
    9.     public void test1()" ^  X8 ?- ?$ P, W. \: N2 W
    10.     {
      , p5 v/ ~; |7 f- e0 n- t& T9 t9 T
    11.         System.out.println("job1 开始执行");- s, a9 Y  u2 |& j, s" k
    12.     } % @; f+ A: t1 ?3 N/ I# [( i/ \, P
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次 - J- a2 W& ^% F1 ?  @3 C( @7 T( }$ \
    14.     public void test2()
      2 T7 L1 ~/ I1 D3 a
    15.     {
      0 y4 y1 h+ l: i
    16.         System.out.println("job2 开始执行");
      8 z6 q' G2 U" g
    17.     }
      $ o( ^/ u& x$ R, w
    18. }
    复制代码
    2 _' J% N5 p9 B$ u4 j9 E
    项目启动后运行结果:
    " N6 l! }2 g" |* S9 w3 y
    * t- S, }: F! N6 |) d7 f% E* Y

    / h7 N2 I: m# O& I
    6 U  x6 E. B# [6 t0 G3 {. k; {9 r% y, N0 R+ W. n9 p& ?2 G- @2 I8 [
    % H, Q8 J% k& K$ B) _. N

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