我的日常

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

动态微博

查看: 3284|回复: 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,不然注解会失效(这个坑找了好久)。
    ( _3 X. Q; {; y. r2 V3 s/ [+ p一、说明        
    ' ~" u0 j0 W& R% ]  t" a

    / S5 `9 A0 t; G% j- K. `         以前项目一直使用Quartz的定时任务,虽然其功能强大,但是配置文件极其复杂,并且一个class下只能执行一个方法(貌似是)。定时任务多了以后对于维护xml配置文件时一件极为头疼的事情。" i  X+ H9 ]+ ~1 B1 W6 S

    0 T! d( q6 q1 `' s5 X& [
    ) O  ~( V- T; ^$ g
            前段时间把Quartz整合实例化入数据库了,做了一个任务列表,进行增删查改,的确是简单多了。在项目不重启的情况下可以对任务进行各种你想要的操作。如图所示操作:
    6 N# ?0 r  C; e7 J

    ! x; _; x8 C) E  N, F: j# ]3 w

    / c  G; r- s- z8 l
    3 M0 \8 f* x/ d; {5 [; p
    # }/ r$ r9 f! E* z) |
            但如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。5 y) E. y* A# O( c, y. X, N4 A; q* ^0 a
    3 ]: l+ J, L) u
    ( G: g: d1 _# N
           Spring Task提供两种方式进行配置,注解和配置文件。使用注解虽然简单,不用配置xml,但是相对于修改比较频繁的任务来说,打包编译的过程也是挺麻烦的,建议使用配置文件实现。
    : P; i5 \  b* p3 p5 e6 W( G: ~+ }$ ~, L( V& g

    ! l! A4 N5 M9 E. |6 p+ f二、配置
    3 h$ X4 x/ B  r4 M1)xml配置
    / K/ a) [( T& n8 d) Y; H4 g+ |3 j
    1. <?xml version="1.0" encoding="UTF-8"?>
      ( p4 H/ E  D7 F1 @' L( y7 p
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      . {5 M, b1 Q5 T
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
      ; K# C8 P# d% x/ m) X; W4 g
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"5 [1 B0 V9 R( q
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="' i- H2 K2 I5 `% @+ ^% i* ^
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        k8 B( D3 q3 Q" z8 B3 u& c
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      9 f" t/ N) K8 x
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd# w& f# X; S6 p. S/ c4 P
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      6 I+ E+ {/ Y* X7 j0 T
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd- e+ d: q/ U/ u' @% v
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd9 u+ E% R. K* Q" s* R! k4 ?8 R
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      $ @! N! z+ p, O% f
    13. 1 S0 ~" M3 |: W% f* s5 C- E4 s
    14.         <description>Spring Configuration</description>
      - y* J2 ~4 @8 N7 g
    15.         7 O- k$ R. c" x% {+ W
    16.         <context:component-scan base-package="task"/> + `% D+ G/ b: z; m0 q/ W7 G" M% o* v
    17.         ) E" L$ p* Z- b4 K* f. X% V
    18.         <!-- 配置任务线性池 -->' n& @8 Q' W' w7 Z6 n
    19.     <task:executor  id="executor" pool-size="10" /> / p2 K- m7 M" s" [) V7 x6 m
    20.     <task:scheduler id="scheduler" pool-size="10"/>+ [! N8 V* F. i* q7 @
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
      ) b+ I9 u5 _0 U/ b6 E2 O$ r
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
      4 T% n7 Y1 m4 F9 Y4 |9 i
    23.    <!--  <task:scheduled-tasks scheduler="scheduler">  ' |' Y. U3 k1 t5 F0 I  {1 ~/ ~! x
    24.         <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>  
      3 ~. |1 Y: K9 m& q+ N
    25.     </task:scheduled-tasks>  --> . N4 w" G9 p# A9 H# v
    26. </beans>
    复制代码

    ' \8 ?1 e) w4 j2)代码实现
    & d$ u# P7 @; r8 Y
    1. package task;
      7 ~+ G# Z6 M' d3 @1 p1 Q; w2 s1 n

    2. 5 ^8 ?% U- I# o( a/ P
    3. import org.springframework.scheduling.annotation.Scheduled;
      2 [$ h# K8 T  f* ?
    4. import org.springframework.stereotype.Component;! {: q3 k( a2 o8 R( J

    5. $ u& M8 B2 A  m1 v% n
    6. @Component("TestJob")  ! E+ H: b* L# ?& V
    7. public class TestJob {
      & @! W% A; V9 `# e: L* E
    8.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
      ' h0 c. {+ n7 U/ R' x  R' m" r
    9.     public void test1()
      - {4 y' Z8 T2 u+ y  y
    10.     {( E6 `) ?7 y" r4 s
    11.         System.out.println("job1 开始执行");
      % V+ U' R* i. j
    12.     }   I' R! y- ^) l7 s. U8 ^
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
      ' S. m; h7 G1 ~' E
    14.     public void test2()
      . ^" i8 ^/ s1 `& \% F
    15.     {
      , F0 Z" Y* w" z
    16.         System.out.println("job2 开始执行");
      * ?- F+ O+ I- x' J
    17.     } # y) J* g7 B8 @% }0 h4 @0 p' N
    18. }
    复制代码
    . _% d' r1 _9 l, k
    项目启动后运行结果:% {: d. u0 i$ W; N8 ]2 W
    9 R2 x' x! |8 r- q
    & R: |; Q# v7 n8 D/ S! @* H$ A7 A" q
    : m/ ?0 y1 m+ F" _9 j  s
    . l4 }: X8 f1 \( O- {
    " z" k  F. Z# d# \

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