首页 > 分享 > Spring:任务调度之task:scheduler与task:executor配置的详解

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

最新推荐文章于 2025-12-26 08:04:08 发布

转载 于 2018-11-18 23:40:16 发布 · 3.4k 阅读

· 0

· 1

其实就是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

相关知识

Spring:任务调度之task:scheduler与task:executor配置的详解
Spring任务执行和调度(TaskExecutor,TaskSchedule)
Spark性能调优之调节task并行度
taskscheduler服务无法启动解决方法 爱问知识人
快速上手:前后端分离开发(Vue+Element+Spring Boot+MyBatis+MySQL)
如何部署一个健壮的 apache
使用Plotly绘制交互式图表:从函数实现到应用实例
一个更贴近 android 场景的启动框架
超大规模药物虚拟筛选的实现与应用
django配置全局404和500错误,以及生产环境下(debug=False)下的静态文件失效解决

网址: Spring:任务调度之task:scheduler与task:executor配置的详解 https://m.huajiangbk.com/newsview2566280.html

所属分类:花卉
上一篇: 黄色桔梗花寓意象征着什么?桔梗花
下一篇: 37岁河南小伙伊拉克闪婚,16万