科帮网-Java论坛、Java社区、JavaWeb毕业设计

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

动态微博

查看: 3010|回复: 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& S: k& m0 p0 [: e7 m) D3 b- _一、说明        
    0 l3 Y6 @4 X4 m& m; `1 g' Z

    5 k. ^6 m" j5 k3 _! Q' Y9 Z8 W         以前项目一直使用Quartz的定时任务,虽然其功能强大,但是配置文件极其复杂,并且一个class下只能执行一个方法(貌似是)。定时任务多了以后对于维护xml配置文件时一件极为头疼的事情。
    2 R; M! A: Z4 H) g* q3 {& N7 O
    2 d# t  g% U, ~. I2 u

    2 T$ D/ F# X6 x# f6 @# v. \6 b3 d        前段时间把Quartz整合实例化入数据库了,做了一个任务列表,进行增删查改,的确是简单多了。在项目不重启的情况下可以对任务进行各种你想要的操作。如图所示操作:
    " Q. R; t) H8 i2 q: U7 F& _, B
    1 P2 z, ~" L: f; Y5 V: b# I" F

    4 V+ Z4 n1 _/ h3 _: u8 K( ^8 B" Q" ^. e- W) F( g" _; }6 Z
    2 K; b2 z6 L  [& [9 V
            但如果只是简单的跑个任务其实spring升级到3后已经自带任务调度器了,相比之下Spring task无论是理解还是使用都简单很多。但是Quartz有线程和线程管理以及集群等高级特性,所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。& U4 \# o  T6 ?) f  e5 c) i+ P
    ; C/ w( G- R% e0 K+ ~1 O

    ' z: L; j$ p7 m' R) F       Spring Task提供两种方式进行配置,注解和配置文件。使用注解虽然简单,不用配置xml,但是相对于修改比较频繁的任务来说,打包编译的过程也是挺麻烦的,建议使用配置文件实现。
    9 b# ]6 [6 K. w/ R- C* Z1 F2 \. j' U; |2 `/ {! Y

    3 V6 Z( l: s& q$ i+ W0 B二、配置
    8 I; B( o% N. U# t- F5 q' l1 D1)xml配置5 x/ Z& A$ _8 i! Z+ b, s# _/ U
    1. <?xml version="1.0" encoding="UTF-8"?>  U9 `3 P# G: a' g
    2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"8 X9 E/ G2 e1 K; d$ N+ e# K# {9 h. l
    3.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"    W, z. Y6 N( s% }/ {* u
    4.         xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      - I( t7 ]/ T# n
    5.     xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
      1 P+ n4 v. o1 f( a( I
    6.                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      9 {5 @& Y0 G  i: R
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd3 W. J9 t$ E. E
    8.                 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
      6 K8 Q- D/ L( d% H( G0 Q
    9.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      8 o" S+ n& P$ O. [1 I, v
    10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      " A3 L6 _6 i% M8 {
    11.                 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      3 c1 T+ V8 D1 O) F
    12.                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
      " ~! a7 S; m. f( r* H( N6 V

    13. ) }1 p7 d% o. A
    14.         <description>Spring Configuration</description>) b7 O% x% k5 Y' h- Q
    15.         
      ; K) o2 a' r' v1 \
    16.         <context:component-scan base-package="task"/>
      ; ~, l. F0 e% X0 m
    17.         + @8 z1 o$ g1 X
    18.         <!-- 配置任务线性池 -->
      ; \) n8 n$ u( J0 T( ^: ~* Q; i
    19.     <task:executor  id="executor" pool-size="10" />
      ( s/ n4 K8 Z- `, Q: _+ M  V( g$ W
    20.     <task:scheduler id="scheduler" pool-size="10"/>% N1 C1 w: D+ c5 W' D
    21.     <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>+ J1 U1 t* f6 z8 i/ S: E( B
    22.     <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
      7 c, s7 v+ M( Z0 F0 A  U- r
    23.    <!--  <task:scheduled-tasks scheduler="scheduler">  + R; d( y6 L- a; H+ }2 g
    24.         <task:scheduled ref="TestJob" method="test" cron="0/1 * * * * ?"/>  : u3 E% u$ w" z+ q6 k
    25.     </task:scheduled-tasks>  --> $ u7 s* D( O$ r' A; Y7 }
    26. </beans>
    复制代码
    - w1 ?! \7 {8 s8 s
    2)代码实现
    5 ]7 F* C1 ~( ~% L9 Y  W- ]
    1. package task;; s( P3 b- x/ ^8 r1 o6 w* L- D( x

    2. 3 _; J# I9 z/ U6 s: l" X
    3. import org.springframework.scheduling.annotation.Scheduled;: b) P7 V& {4 E7 a1 Z  L* ~) C6 Z" ^
    4. import org.springframework.stereotype.Component;' m6 u6 S3 D# G9 f+ x* C9 {) N

    5. 6 Z, A& }0 q9 M& u6 _' A
    6. @Component("TestJob")  . k6 B7 n7 W7 b6 N' v* v
    7. public class TestJob {
      4 I4 i: V" x, _" ]6 @, ?
    8.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次 ) ?& s9 R2 s( b# `) S
    9.     public void test1()
      ' y( h+ g. {; R! `8 H* O; Y: }; V9 E6 |& c
    10.     {
      " E9 x- u" ~+ p+ V  Q0 C
    11.         System.out.println("job1 开始执行");/ ~" v0 ^% \1 z! P8 ~: m
    12.     }
      4 P& q* h" l+ [* h5 b
    13.         @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
      " B6 w8 w- i% b5 P! ]& K
    14.     public void test2()
      5 [  U& y+ N- m, J
    15.     {
      0 G0 e0 Q5 H6 E* ^$ ]
    16.         System.out.println("job2 开始执行");
      8 f& D( C3 d; |$ f. G- D
    17.     }
      6 A: i9 d2 i* Z2 x# ~
    18. }
    复制代码
    ' }1 S: c2 b3 ]$ ~5 Z
    项目启动后运行结果:
    , v9 V6 j, m" \, F' K$ b
    7 u8 n0 s; d% r
    # y& K0 L  o- g* r  S- T
    : T  s' q1 y9 |, X# W, w1 @5 J5 T

    2 v% V6 A$ t' K: H& ^$ K/ R. ^3 q

    CSDN下载:http://download.csdn.net/detail/zhulin2012/9576741

    百度云下载:http://pan.baidu.com/s/1boW6WP5


    科帮网-Java论坛、Java社区、JavaWeb毕业设计 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与科帮网-Java论坛、Java社区、JavaWeb毕业设计享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网-Java论坛、Java社区、JavaWeb毕业设计的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、科帮网-Java论坛、Java社区、JavaWeb毕业设计管理员和版主有权不事先通知发贴者而删除本文


    JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

    0

    主题

    0

    听众

    104

    金钱

    三袋弟子

    该用户从未签到

    沙发
    发表于 2017-12-20 18:33:33 |只看该作者
    额1111111111
    回复

    使用道具 举报

    快速回复
    您需要登录后才可以回帖 登录 | 立即注册

       

    发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
    快速回复 返回顶部 返回列表