科帮网

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

动态微博

查看: 3298|回复: 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,不然注解会失效(这个坑找了好久)。
    $ L; w% A4 V; @3 P/ ]9 ]: E一、说明        
    ' g7 u. |3 b4 ], X# S2 `5 N

    * g  M. \: h# ]. ~! y( ^3 h, B' M0 n4 c         以前项目一直使用Quartz的定时任务,虽然其功能强大,但是配置文件极其复杂,并且一个class下只能执行一个方法(貌似是)。定时任务多了以后对于维护xml配置文件时一件极为头疼的事情。+ |# \- m5 z* `# i' ]

    5 A2 H( Y+ r& V9 H0 R0 P/ o4 o
    : f* ?6 u4 _8 d: A; B
            前段时间把Quartz整合实例化入数据库了,做了一个任务列表,进行增删查改,的确是简单多了。在项目不重启的情况下可以对任务进行各种你想要的操作。如图所示操作:( \5 |4 l2 b; E' g9 C% v* g
    + B5 i, D7 w; \+ x5 e3 M
    1 W6 @. C, Y$ L  P  g0 a8 ~

    " x3 X% f; A5 c

    1 S( S, g0 D) j, d        但如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。! W; R/ g2 P1 i) |
    , X* i" p% I; P. [0 i2 P

    - i" g. V9 C  ~( }       Spring Task提供两种方式进行配置,注解和配置文件。使用注解虽然简单,不用配置xml,但是相对于修改比较频繁的任务来说,打包编译的过程也是挺麻烦的,建议使用配置文件实现。3 X+ V* g: M9 v3 F
    & @0 ~1 }- W7 W* s+ D2 Q( r6 E
    5 g+ n  b/ ^: R3 K; ]; _4 X
    二、配置" ], Y4 w$ o1 Y" o
    1)xml配置3 a+ A2 W4 \% h/ j  ~
    1. <?xml version="1.0" encoding="UTF-8"?>
      1 P- T8 L2 y  V0 x* d4 S
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      / |/ ]; W- q' c! |* e
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      ! @: X" E1 S. T# s# L( a
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        m8 `& \3 T3 ]
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="6 z% C# {! d6 {
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      ' v' p! x- O& s- y
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      7 v4 U3 @# q3 h3 }& J% D1 [
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd5 n0 L: Q4 N. B7 J% P3 h( @, f
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd/ L" H9 B( b% K4 F! x
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd8 h4 }7 k2 _3 q# V4 [; Z
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      / L) T. \& P% V, `$ p2 A- Q
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      + R" Q6 B. S" S2 e! K: h. @
    13. / U3 ~' I5 A& b& `  R% N+ J
    14.         <description>Spring Configuration</description>
      ' w+ ~! E" s  t$ y1 G& S& i3 P
    15.         6 v) g$ d$ y0 W
    16.         <context:component-scan base-package="task"/>
      & [4 Z3 c+ w2 f+ l8 G4 i
    17.         
      7 i/ {# ]; N( o2 W
    18.         <!-- 配置任务线性池 -->
      2 ^# H/ C4 n- f, |, G
    19.     <task:executor  id="executor" pool-size="10" />
      1 z7 A, r0 @2 X& ~
    20.     <task:scheduler id="scheduler" pool-size="10"/>
      9 r  v- A; D7 J/ ?5 D8 K+ g
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      ' ]4 J' @& s6 z# H  y
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
      / a0 S& Q2 d+ C
    23.    <!--  <task:scheduled-tasks scheduler="scheduler">    H$ F  T2 A& G. I
    24.         <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>  
      # G3 [5 N5 C0 q; V. M9 Y# r
    25.     </task:scheduled-tasks>  -->
      ' N3 |1 d0 p& y1 o
    26. </beans>
    复制代码

    + W* H7 D, t4 |5 V" Y/ ~2)代码实现
    4 K' B3 @7 A. f' l1 v" i& `& m; [$ i
    1. package task;: H; |3 i$ l2 P: h
    2. 5 j8 C: A! I; c- P- [- Z2 n
    3. import org.springframework.scheduling.annotation.Scheduled;1 N; F1 l, w' X/ n9 ^9 y
    4. import org.springframework.stereotype.Component;
      , L- i+ _# _) c% [" @, {6 X) u6 P
    5. 6 y( M# O1 z% E8 X6 y9 H
    6. @Component("TestJob")  
      * d) l4 L1 W& c0 b  Q
    7. public class TestJob {
      . S" b' D9 L; y* l
    8.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次 1 x  N, e' k* C3 O1 w* }4 G
    9.     public void test1()
      3 Y' w+ k# K, C7 |# [, V
    10.     {4 A$ Z' ^9 H: \: e$ k5 P
    11.         System.out.println("job1 开始执行");
      ! h$ j, `! Y1 f) T# O
    12.     } # k/ J. ?& H4 |/ O  L! o' K
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次 / e* \! o3 K# G8 t
    14.     public void test2()# K* a! }# X  X8 I6 ?7 N! X2 n
    15.     {  |2 l8 K& j" e$ g0 Q9 b. Z: q7 w/ y
    16.         System.out.println("job2 开始执行");- d( N! c/ R4 N' p. ^' f
    17.     } ! U+ G; i$ D7 ]! j2 _. t
    18. }
    复制代码

    0 y" P+ B% c3 Z. a( X' `& V8 W3 P项目启动后运行结果:; g9 ]( }/ J6 S, {1 j
    3 Z& q' Q; m1 W* M5 t1 O! H1 h% |' ]

    / K) |( ^0 @- Q
    3 `) P1 t: i- |0 z6 }) k, @/ J
    7 q1 q' `( |0 ~& u' G+ S& ?
    . ^# j- _8 Y( F0 o2 x

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