




下載本文檔
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Spring3.2.6 框架多線(xiàn)程序配置【定時(shí)任務(wù)+線(xiàn)程池】費(fèi)勁周折終于把這個(gè)功能配置起來(lái)了,在網(wǎng)上找到很多資料,大多數(shù)例子基本都是相互拷貝,前后不搭調(diào),搞得不知所措。因此我把這次配置可以完整運(yùn)行的示例程序整理出來(lái),分享,希望有時(shí)間再進(jìn)一步優(yōu)化,共同進(jìn)步。Spring配置文件:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-/SPRING/DTD BEAN/EN" "/dt
2、d/spring-beans.dtd"><beans> <!- 線(xiàn)程池 -> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!- 核心線(xiàn)程數(shù) -> <property name="corePoolSize" value="4" /> <!- 最大線(xiàn)程數(shù) -> <proper
3、ty name="maxPoolSize" value="8" /> <!- 隊(duì)列最大長(zhǎng)度 -> <property name="queueCapacity" value="200" /> <!- 線(xiàn)程池維護(hù)線(xiàn)程所允許的空閑時(shí)間 -> <property name="keepAliveSeconds" value="300" /> <!- 線(xiàn)程池對(duì)拒絕任務(wù)(無(wú)線(xiàn)程可用)的處理策略 -> <property
4、 name="rejectedExecutionHandler"> <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" /> </property> </bean> <bean id="mainProcess" class="com.tz.controller.MainPro"> <property name="taskExecutor" ref=
5、"taskExecutor" /> </bean> <bean id="springScheduleExecutorTask" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask"> <!- 要啟動(dòng)的主線(xiàn)程 -> <property name="runnable" ref="mainProcess" /> <!- 容器加載運(yùn)行延遲1秒 ->
6、<property name="delay" value="1000" /> <!- 任務(wù)間隔時(shí)間(sleep時(shí)間)執(zhí)行完第一組任務(wù),執(zhí)行第二組任務(wù)相隔時(shí)間,兩個(gè)任務(wù)在時(shí)間上不應(yīng)該相交 -> <property name="period" value="10000" /> </bean> <bean id="springScheduledExecutorFactoryBean" class="org.springframework.
7、scheduling.concurrent.ScheduledExecutorFactoryBean"> <property name="scheduledExecutorTasks"> <list> <ref bean="springScheduleExecutorTask" /> </list> </property> </bean> </beans>程序?qū)?yīng)的類(lèi):MainPro.javapackage com.tz.controller;import
8、 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;/* * 要起動(dòng)的主線(xiàn)程 * author wyg20151013 */public class MainPro implements Runnable private ThreadPoolTaskExecutor taskExecutor; public MainPro()public MainPro(ThreadPoolTaskExecutor taskExecutor)this.taskExecutor = taskExecutor; public Thre
9、adPoolTaskExecutor getTaskExecutor() return taskExecutor;public void setTaskExecutor(ThreadPoolTaskExecutor taskExecutor) this.taskExecutor = taskExecutor; public void run() for(int i = 0; i < 25; i+) taskExecutor.execute(new MessagePrinterTask("Message = " + i); /等待池中線(xiàn)程執(zhí)行完 while(taskEx
10、ecutor.getActiveCount()>0) try Thread.sleep(5000); catch (InterruptedException e) e.printStackTrace(); System.out.println("主線(xiàn)程分配完畢"); /* * 這個(gè)子線(xiàn)程來(lái)就是我們系統(tǒng)要處理的任務(wù) * author wyg20151013 */ private class MessagePrinterTask implements Runnable private String message; public MessagePrinterTask(St
11、ring message) this.message = message; public void run() System.out.println(message); 補(bǔ)充知識(shí):1、解析spring schedule來(lái)源:Spring在schedule這塊支持JDK Timer、concurrent、quartz三種,這三種任務(wù)調(diào)度方案在實(shí)現(xiàn)機(jī)制和調(diào)用方法上都不同,但spring通過(guò)對(duì)其包裝,使得基于spring能用統(tǒng)一的配置和編碼風(fēng)格來(lái)使用這三種schedule方案。總得來(lái)說(shuō)這三種schedule都是基于scheduler->trigger->job的基本流程,因此spring
12、通過(guò)TimerFactoryBean、ScheduledExecutorFactoryBean和SchedulerFactoryBean分別實(shí)現(xiàn)JDK Timer、concurrent和quartz的基本流程。主要邏輯如下代碼所示:TimerFactoryBeanjava view plaincopy1. public void afterPropertiesSet() 2. ("Initializing
13、 Timer"); 3. this.timer = createTimer(this.beanName, this.daemon); 4. / Register specified ScheduledTimerTasks, if necessary. &
14、#160;5. if (!ObjectUtils.isEmpty(this.scheduledTimerTasks) 6. /注冊(cè)task和timer registerTasks(this.scheduledTimerTasks, this.timer); 7.
15、; 8. ScheduledExecutorFactoryBeanc-sharp view plaincopy1. public void afterPropertiesSet() 2. if (logger.isInfoEnabled() 3.
16、60; ("Initializing ScheduledExecutorService" + 4. (this.beanName !=&
17、#160;null ? " '" + this.beanName + "'" : ""); 5. 6. ScheduledExecutorService executor =&
18、#160; 7. createExecutor(this.poolSize, this.threadFactory, this.rejectedExecutionHandler); 8. / Register specified
19、ScheduledExecutorTasks, if necessary. 9. if (!ObjectUtils.isEmpty(this.scheduledExecutorTasks) 10. registerTasks(this.scheduledExecutorTa
20、sks, executor); 11. 12. / Wrap executor with an unconfigurable decorator. 13. this.exec
21、utor = (this.exposeUnconfigurableExecutor ? 14. Executors.unconfigurableScheduledExecutorService(executor) : executor); 15.
22、60;SchedulerFactoryBeanc-sharp view plaincopy1. public void afterPropertiesSet() throws Exception 2. . 3. / 初始化sceduler 4.
23、 initSchedulerFactory(schedulerFactory); 5. this.scheduler = createScheduler(schedulerFactory, this.schedulerName); 6. /注冊(cè)listener、trigger和job 7. registerListeners(); 8.
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 個(gè)人入股工廠合同樣本
- 江蘇省連云港市海州區(qū)2024-2025學(xué)年度九年級(jí)歷史下學(xué)期期中考試題(中國(guó)史-世界史)(含答案)
- 量化對(duì)沖交易對(duì)A股的影響分析及展望
- 2023年5月7日江西事業(yè)單位考試A類(lèi)職業(yè)能力傾向測(cè)驗(yàn)試題及答案
- 拉丁美洲跨境供應(yīng)鏈金融風(fēng)險(xiǎn)防范與技術(shù)支持合同
- 工業(yè)氣體供應(yīng)與安全應(yīng)急預(yù)案編制合同
- 教育培訓(xùn)機(jī)構(gòu)股權(quán)證書(shū)及師資力量整合交接協(xié)議
- 兒童早期教育機(jī)構(gòu)加盟合作合同
- 群眾演員參演電視劇拍攝合同書(shū)
- 特色烘焙品牌形象保護(hù)及合作協(xié)議補(bǔ)充條款
- 血常規(guī)教育課件
- 普通飲片車(chē)間共線(xiàn)生產(chǎn)風(fēng)險(xiǎn)評(píng)估報(bào)告
- 建筑總工程師招聘面試題與參考回答(某大型央企)2024年
- 糖尿病視網(wǎng)膜病變護(hù)理
- 解讀智能測(cè)試用例生成
- 【工程法規(guī)】王欣 教材精講班課件 35-第6章-6.1-建設(shè)單位和相關(guān)單位的安全責(zé)任制度
- 獸藥GSP質(zhì)量管理制度匯編
- 【基于單片機(jī)的智能送餐配送車(chē)設(shè)計(jì)與實(shí)現(xiàn)(論文)11000字】
- 2024年供電營(yíng)業(yè)規(guī)則復(fù)習(xí)題庫(kù)含答案解析
- GB/T 18457-2024制造醫(yī)療器械用不銹鋼針管要求和試驗(yàn)方法
評(píng)論
0/150
提交評(píng)論