Spring:任务调度之task:scheduler与task:executor配置的详解

其实就是Spring定时器中配置文件中一些配置信息,由于笔者自己是头一次使用,有些配置详细不太明白,随即研究了一番,于是想记录一下,有需要的小伙伴可以参考,也方便日后自己查阅。
首先,创建一个仅仅包含定时器配置的Spring配置文件:spring-timer.xml。以下均为配置信息:
1、在配置文件头部加入定时器的命名空间----------
 

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
</beans>/


2、定时器的详细配置----------
注解方式:

<context:annotation-config />
    <!-- 自动调度需要扫描的包 --> 
    <context:component-scan base-package="com.honest.sspc.timer" ></context:component-scan>
    <!-- 定时器开关 -->
    <task:executor id="executor" pool-size="5"/>
    <task:scheduler id="scheduler" pool-size="10"/>
    <task:annotation-driven executor="executor" scheduler="scheduler"/>
xml配置方式:
<context:annotation-config />
    <!-- 自动调度需要扫描的包 --> 
    <context:component-scan base-package="com.honest.sspc.timer" ></context:component-scan>
    <!-- 定时器开关 -->
    <task:executor id="executor" pool-size="5"/>    
    <task:annotation-driven executor="executor" scheduler="scheduler"/>    
    
    <!-- 配置调度 需要在类名前添加 @Service -->  
    <task:scheduled-tasks>  
        <task:scheduled ref="demoTask" method="myTestWork" cron="0/10 * * * * ?"/>  
    </task:scheduled-tasks> 
    <task:scheduler id="scheduler" pool-size="10"/>
    <!-- 不通过配置调度,需要在类名前 @Component/@Service,在方法名 前添加@Scheduled(cron="0/5 * * * * ? ")、即用注解的方式-->  


3、关于任务调度的说明----------
任务调度器的配置详细参数说明:
task:scheduler/@pool-size:调度线程池的大小,调度线程在被调度任务完成前不会空闲 
task:scheduled/@cron:cron表达式,注意,若上次任务未完成,即使到了下一次调度时间,任务也不会重复调度

<task:scheduled-tasks scheduler="scheduler">  
    <task:scheduled ref="beanID" method="methodName" cron="CronExp" />  
</task:scheduled-tasks>  
<task:scheduler id="scheduler" pool-size="1" />


任务执行器配置详细参数说明:  
task:executor/@pool-size:可以指定执行线程池的初始大小、最大大小 
task:executor/@queue-capacity:等待执行的任务队列的容量 
task:executor/@rejection-policy:当等待队列爆了时的策略,分为丢弃、由任务执行器直接运行等方式

<task:executor id="executor" keep-alive="3600" pool-size="100-200" queue-capacity="500" rejection-policy="CALLER_RUNS" />


 原文参考:https://blog.csdn.net/weixin_37848710/article/details/79635021

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值