科帮网

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

动态微博

查看: 3297|回复: 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,不然注解会失效(这个坑找了好久)。% l9 l# V% E5 l: O6 w& L
    一、说明        
    ! U! y3 X. G. Q# `% d+ b( \+ F

    4 h4 w( y8 E7 s/ j         以前项目一直使用Quartz的定时任务,虽然其功能强大,但是配置文件极其复杂,并且一个class下只能执行一个方法(貌似是)。定时任务多了以后对于维护xml配置文件时一件极为头疼的事情。
    0 z  ~! C" P2 k: N9 H$ P1 O% O% d4 ]7 t* ]+ v1 |) ]

    8 Z( ~. V0 C. w% e        前段时间把Quartz整合实例化入数据库了,做了一个任务列表,进行增删查改,的确是简单多了。在项目不重启的情况下可以对任务进行各种你想要的操作。如图所示操作:
    1 g1 a& j- R5 Q' s0 f/ |
    4 U, n. }0 C2 G( e

    0 }" H! \. H, d" P/ R7 _. L8 k& u4 l* |3 X5 I7 G
    4 g  z; |" J1 s) \
            但如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。
    7 _* R5 Y0 |1 K$ z
    , X5 _; r1 ]. c3 C( X& q: c

    ; ?& C' x8 R* i; }( T6 t4 F3 Y       Spring Task提供两种方式进行配置,注解和配置文件。使用注解虽然简单,不用配置xml,但是相对于修改比较频繁的任务来说,打包编译的过程也是挺麻烦的,建议使用配置文件实现。
    9 r. O% p% i& c9 v2 ]8 F  Y& {4 H2 H/ H1 ]( m( T. M- q' {

    0 O0 r3 x/ M3 A0 a二、配置5 k, w7 p; t/ Z8 @* _
    1)xml配置, e# Z7 [* M. N
    1. <?xml version="1.0" encoding="UTF-8"?>
      4 ?5 U4 ]5 }- A9 O4 T1 @! u. g9 G' W
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      / S7 [2 @, C& l. u# w6 L7 j  m% J
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      # }* ]' E5 c+ W6 y
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"/ ]+ u3 N! o* s% g( _* k# `
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="- U4 D; O* u, }; k( b
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd3 }) ~. E1 m6 g7 R0 \. Z! p3 }0 @* \
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd2 h$ d6 v, S; ^: I( ~
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd- b' n/ M2 O4 q* D- R6 u2 n: X
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd* D/ j, u; y2 q% y5 j
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      2 ^  S- d6 r( z9 u" ]. P
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd; _! K" v. ]/ L; l- c
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">! T" P1 Y; P- r& H  k9 m5 s* {
    13. : ?2 b' X0 ^8 y1 W
    14.         <description>Spring Configuration</description>
      4 g, p. g- ], G; D, Z
    15.           x* z2 e' m! k: @. ~4 ^1 I/ C% g5 E
    16.         <context:component-scan base-package="task"/>
      8 T) t, p! v" V2 h- I3 d
    17.         ) h4 j( A+ Z! v+ L8 i
    18.         <!-- 配置任务线性池 -->$ y$ U2 H- ^0 \4 G  g
    19.     <task:executor  id="executor" pool-size="10" /> . n0 _- S3 G+ ~# |) q3 P' K+ C
    20.     <task:scheduler id="scheduler" pool-size="10"/>+ w% p" ?3 G* q. ~
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>. |8 ^  F: T& _! N1 ]& q- p
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->0 w9 Y, h$ M; D6 s* v
    23.    <!--  <task:scheduled-tasks scheduler="scheduler">  
      3 J7 H8 s4 W+ x% L. v. i
    24.         <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>  / }. O/ @; v) M1 A7 |
    25.     </task:scheduled-tasks>  --> + \9 @) W. b8 Y% P
    26. </beans>
    复制代码
    - [6 \- S; N8 c
    2)代码实现
    ' r; |) ?# u/ E" `
    1. package task;
      5 b* T: ?% x0 E( K* k! ]8 p8 W; y% n

    2. + ?. r1 V: M* K* m( [
    3. import org.springframework.scheduling.annotation.Scheduled;
      ; k+ f, J7 L. u
    4. import org.springframework.stereotype.Component;
      ' c) r  h/ \1 z2 a; R) ]7 X

    5. / U, V, B& N+ x/ n
    6. @Component("TestJob")  
      5 b9 d+ l/ Y& g$ U& E
    7. public class TestJob {* y' o6 W6 |, g; h2 c/ R8 v6 M
    8.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次 4 [) I* z, P. X) w" h# E
    9.     public void test1()3 n( p) `' a5 }- K& y# I3 Y! z
    10.     {' }  K5 B, A  R3 B: L
    11.         System.out.println("job1 开始执行");' d2 D" K& x4 [  i0 V$ Z% l7 ~
    12.     } & M: R' X. O. q" W) {: f% d
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次 " e( B0 O- c% N" K8 C! S" T
    14.     public void test2()
      ) _. w$ s' ?) u+ `: {& R* B! v0 q/ z
    15.     {. S  D" ]: i( Q
    16.         System.out.println("job2 开始执行");
      $ p3 k, l; G6 Y$ d1 D; Z4 P
    17.     }
      2 j' w# x5 c2 G* @% B( s: S
    18. }
    复制代码

      m) q0 g$ l( W# j- n" l; k项目启动后运行结果:
    " d3 R2 k8 h+ n0 L, W& e( v
    * p& N5 `! J. @0 j
    1 ?9 G8 L6 L) K+ v1 _) n
    ! y" U0 ~  j8 C& U+ u
    1 M3 b0 n  Z$ ]6 n
    : p) ?' a7 ]6 J) e7 L1 }* H

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