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