欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

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

发布时间:2025/3/12 javascript 32 豆豆
生活随笔 收集整理的这篇文章主要介绍了 关于Spring 任务调度之task:scheduler与task:executor配置的详解 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

关于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" />

总结

以上是生活随笔为你收集整理的关于Spring 任务调度之task:scheduler与task:executor配置的详解的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。