科帮网

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

动态微博

查看: 3292|回复: 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,不然注解会失效(这个坑找了好久)。
    # m  R6 i. K3 X, R+ B1 a, q一、说明        8 l- Q/ H4 t' l* Y1 _7 r

    7 y: M9 R3 A/ q+ D1 B/ t  f8 Z         以前项目一直使用Quartz的定时任务,虽然其功能强大,但是配置文件极其复杂,并且一个class下只能执行一个方法(貌似是)。定时任务多了以后对于维护xml配置文件时一件极为头疼的事情。
    / n+ U+ e7 [2 {/ C! C4 ~# K/ Z2 `0 [4 Z7 X! n& d
    ' P8 t8 I/ p/ H# s3 Z9 o
            前段时间把Quartz整合实例化入数据库了,做了一个任务列表,进行增删查改,的确是简单多了。在项目不重启的情况下可以对任务进行各种你想要的操作。如图所示操作:( z  R! R4 |: X

    3 ~) b3 B8 [2 l. I, Y' t

    4 `' a9 W  I1 x
    " i$ Q  B9 c+ B3 I/ |# F' z/ Y, T

    7 p: M+ d4 v9 n' ?( [        但如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。
    8 T' c' X% B9 |7 p2 r- Y7 m: x/ v  n. k6 P3 N& s7 y0 U6 ?6 x6 K4 l
    / I/ N9 D4 _7 A$ i/ _9 V
           Spring Task提供两种方式进行配置,注解和配置文件。使用注解虽然简单,不用配置xml,但是相对于修改比较频繁的任务来说,打包编译的过程也是挺麻烦的,建议使用配置文件实现。
    0 M: T: n' w  Q; `( @! }! P# j+ y
    0 t; N5 L5 B& G& ^8 S. f& `- p0 E! I* f- Y/ N3 L
    二、配置- f" g& W$ l4 m: L; e# ]% H
    1)xml配置& G: l2 b& f6 T5 b9 L2 n4 N* H+ Q
    1. <?xml version="1.0" encoding="UTF-8"?>1 F1 m* e: i! Y8 W% I
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; s6 m  Z% ^7 W; l
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  " d: u: W. c9 H  I5 z
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"- C7 R* o6 x/ V2 d. g0 l- F
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="( d2 O& c( E7 L7 y& c5 q7 T* e
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      + }$ n1 P4 M1 M' `2 Y# R3 J8 Q
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      , k) n  [! \4 t. K( T" M3 z" f: q
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd- i( S) Q- {6 A% C* q
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd. X8 a' s* }2 k8 Q
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd2 q/ }8 z" T7 A* Y0 K* p
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      / Y+ U& T# j, v  j$ G! K1 |, j6 S0 \% v
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">+ F' k. X5 y! z5 P. O) O8 Q1 t( W0 \

    13. $ \$ v; [/ H3 p6 w# u* U
    14.         <description>Spring Configuration</description>
      & x1 v: u- s8 P2 b4 c+ s
    15.         ( K" r. O2 r( q! ^. B- \
    16.         <context:component-scan base-package="task"/> 9 r7 ]% Q- A1 G/ w
    17.         $ |0 I8 f( @+ }
    18.         <!-- 配置任务线性池 -->
      # Q$ U3 u: D. S0 L
    19.     <task:executor  id="executor" pool-size="10" />
      % W& h8 R  ^4 s7 a% a/ `2 S7 @
    20.     <task:scheduler id="scheduler" pool-size="10"/>) ~! }& Q7 f& V
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>4 a0 p! y- ]+ K3 J) i
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
      0 W- G$ a, k4 W/ ^
    23.    <!--  <task:scheduled-tasks scheduler="scheduler">  0 f; \0 D7 Z+ W  D5 i8 {8 d9 L+ J; Z
    24.         <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>  9 v) N+ Z& {1 ?# m5 y
    25.     </task:scheduled-tasks>  -->
      6 ~; c6 t& A' g' }9 I
    26. </beans>
    复制代码
    $ g% ?5 O+ [9 i0 v& W
    2)代码实现* x) Q0 r3 U8 Y( B* P  ^1 r% l
    1. package task;
      5 [3 q$ w& s4 R0 b' j# w. H, E

    2. , O$ ~  s" B6 @) i8 e
    3. import org.springframework.scheduling.annotation.Scheduled;; Y8 F1 }/ y. _3 Q9 ~* w0 M
    4. import org.springframework.stereotype.Component;5 P1 @' V& E/ M' S7 ?8 ^
    5. 2 N5 u0 h; c" J. T
    6. @Component("TestJob")  
      $ J; i- b& w$ t: I$ X  R
    7. public class TestJob {
      " `3 y4 z+ n" l$ m7 f! y3 x2 y4 ^
    8.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
      ; ?$ \# j7 d  j1 I
    9.     public void test1()
      8 e: i  I7 i6 J9 Y6 a0 `
    10.     {
      / P; B: |* Q' R* L
    11.         System.out.println("job1 开始执行");
      3 j$ C$ B: B4 @' Y
    12.     }
      ! b8 _! l1 e1 S- Q/ @0 Q2 M% O
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
      * ]" ]( G5 S3 r. b( Z
    14.     public void test2(), b* j7 ]2 i! Y1 B8 |
    15.     {  d  o  y$ ~+ M1 A4 l! N. F: L  }
    16.         System.out.println("job2 开始执行");
      4 e( F9 ~9 F6 Q  I  F; g0 A
    17.     } ) [' ^1 y+ K0 Z& ]- J
    18. }
    复制代码

    & x, o1 [( l* @- l2 j2 G项目启动后运行结果:5 M5 v: J, O2 _5 L
    ; n  r6 L; ~  l- E* F6 _9 Z* g
    1 h7 J9 `/ w$ H9 x+ e+ D

    , q* [! \- `& y! V5 E
    5 O4 e8 y# b) @2 F3 B' ^6 Q8 d& O& e/ j+ s5 m4 j) s$ X8 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群 科帮网手机客户端
    快速回复 返回顶部 返回列表