zoukankan      html  css  js  c++  java
  • Spring任务调度器之Task的使用

    Spring Task提供两种方式进行配置,正如大家所想吧,还是一种是annotation(标注),而另外一种就是XML配置了。但其实这里我觉得比较尴尬,因为任务调度这样的需求,通常改动都是比较多的,如果用annotation的方式的话,改动就变得麻烦了,必须去重新编译。所以,我只是选择用XML配置的方式,不过我还是习惯性地启用着标注方式,就如AOP配置那样。annotation方式请自行查找@Scheduled

    具体配置参考如下即可

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"  
    4.     xsi:schemaLocation="  
    5.           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    6.           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">  
    7.   
    8.     <bean id="reminderProcessor" class="com.foo.task.ReminderProcessor">  
    9.         <property name="workers">  
    10.             <array value-type="com.foo.task.Worker">  
    11.                 <ref bean="projectScheduleRemindWorker" />  
    12.             </array>  
    13.         </property>  
    14.     </bean>  
    15.   
    16.     <!-- 配置任务线性池 -->  
    17.     <task:executor id="executor" pool-size="3" />  
    18.     <task:scheduler id="scheduler" pool-size="3" />  
    19.     <!-- 启用annotation方式 -->  
    20.     <task:annotation-driven scheduler="scheduler"  
    21.         executor="executor" proxy-target-class="true" />  
    22.   
    23.     <task:scheduled-tasks scheduler="scheduler">  
    24.         <task:scheduled ref="reminderProcessor" method="process"  
    25.             cron="0 0 12 * * ?" />  
    26.     </task:scheduled-tasks>  
    27. </beans>  

    核心部分见

    Xml代码  收藏代码
    1. <task:scheduled-tasks scheduler="scheduler">  
    2. <task:scheduled ref="reminderProcessor" method="process"  
    3. cron="0 0 12 * * ?" />  
    4. </task:scheduled-tasks>  

     

    意思就是每天的12点执行reminderProcessor这个Bean中的process方法。cron的配置表达式跟Quartz基本一致,但实测不支持一些特殊字符,如配置天的时候的L,W和Z,因为遇到要每个月倒数第三天执行任务调度的需求,但我一配置SpringTask报非法字符。

    所以,Quartz和SpringTask间的差距也显而易见的。SpringTask用起来十分简单,毕竟是Spring自家的,虽然跟Quartz也可以实现结合,但没那么简单。而SpringTask功能也没Quartz强大,Quartz的集群和高级特性多的去了。所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。

    附上我这个例子的详细UML说明

     

  • 相关阅读:
    使用 Apachetop 实时监测web服务器运行状况
    Idea连接服务器docker并部署代码到docker实现一键启动
    win10上修改docker的镜像文件存储位置
    docker无法删除镜像,Error: No such container,附docker常用命令
    docker--docker基本命令使用及发布镜像
    Docker for windows pull镜像文件的安装位置改变方法
    k8s 超详细总结,面试必问
    Java大数类BigDecimal及八种舍入模式的介绍
    BigDecimal创建初始化值类型对比
    BigDecimal 小数 浮点数 精度 财务计算
  • 原文地址:https://www.cnblogs.com/yhtboke/p/5856424.html
Copyright © 2011-2022 走看看