




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、Spring3.2.6 框架多線程序配置【定時任務(wù)+線程池】費勁周折終于把這個功能配置起來了,在網(wǎng)上找到很多資料,大多數(shù)例子基本都是相互拷貝,前后不搭調(diào),搞得不知所措。因此我把這次配置可以完整運行的示例程序整理出來,分享,希望有時間再進一步優(yōu)化,共同進步。Spring配置文件:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-/SPRING/DTD BEAN/EN" "/dt
2、d/spring-beans.dtd"><beans> <!- 線程池 -> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!- 核心線程數(shù) -> <property name="corePoolSize" value="4" /> <!- 最大線程數(shù) -> <proper
3、ty name="maxPoolSize" value="8" /> <!- 隊列最大長度 -> <property name="queueCapacity" value="200" /> <!- 線程池維護線程所允許的空閑時間 -> <property name="keepAliveSeconds" value="300" /> <!- 線程池對拒絕任務(wù)(無線程可用)的處理策略 -> <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"> <!- 要啟動的主線程 -> <property name="runnable" ref="mainProcess" /> <!- 容器加載運行延遲1秒 ->
6、<property name="delay" value="1000" /> <!- 任務(wù)間隔時間(sleep時間)執(zhí)行完第一組任務(wù),執(zhí)行第二組任務(wù)相隔時間,兩個任務(wù)在時間上不應(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)的類:MainPro.javapackage com.tz.controller;import
8、 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;/* * 要起動的主線程 * 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); /等待池中線程執(zhí)行完 while(taskEx
10、ecutor.getActiveCount()>0) try Thread.sleep(5000); catch (InterruptedException e) e.printStackTrace(); System.out.println("主線程分配完畢"); /* * 這個子線程來就是我們系統(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); 補充知識:1、解析spring schedule來源:Spring在schedule這塊支持JDK Timer、concurrent、quartz三種,這三種任務(wù)調(diào)度方案在實現(xiàn)機制和調(diào)用方法上都不同,但spring通過對其包裝,使得基于spring能用統(tǒng)一的配置和編碼風(fēng)格來使用這三種schedule方案??偟脕碚f這三種schedule都是基于scheduler->trigger->job的基本流程,因此spring
12、通過TimerFactoryBean、ScheduledExecutorFactoryBean和SchedulerFactoryBean分別實現(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. /注冊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. /注冊listener、trigger和job 7. registerListeners(); 8.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 法學(xué)概論考試的整體規(guī)劃與試題及答案
- 2025屆新疆烏魯木齊仟葉學(xué)校七下數(shù)學(xué)期末達標(biāo)檢測模擬試題含解析
- 計算機二級VB考試知名試題及答案
- 財務(wù)業(yè)務(wù)工作目標(biāo)規(guī)劃計劃
- 軟件水平考試經(jīng)典試題及答案解析
- 2024年西安碑林區(qū)友誼小學(xué)招聘筆試真題
- 2024年溫州榕園學(xué)校引進教育人才筆試真題
- 2024年海南省農(nóng)業(yè)農(nóng)村廳下屬事業(yè)單位真題
- 2024年秦皇島事業(yè)單位招聘筆試真題
- 2024年甘肅省應(yīng)急管理廳下屬事業(yè)單位真題
- 2025證券從業(yè)資格考試證券市場基礎(chǔ)知識真題試卷
- 2025年入團基礎(chǔ)知識試題及答案詳解
- 2025-2030年中國軍工行業(yè)市場發(fā)展現(xiàn)狀及發(fā)展趨勢與投資戰(zhàn)略研究報告
- 地震知識課件
- 2025年小學(xué)生科學(xué)知識競賽試題及答案
- 2025年中學(xué)語文教師招聘試題及答案
- 阿片類藥物的不良反應(yīng)和對策
- 《液相色譜-質(zhì)譜聯(lián)用》課件
- 潤滑油購銷合同協(xié)議
- 《醫(yī)療團隊中的護理管理:護士長角色定位》課件
- 2025年電商客服管理試題及答案
評論
0/150
提交評論