




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Spring3.2.6 框架多線程序配置【定時(shí)任務(wù)+線程池】費(fèi)勁周折終于把這個(gè)功能配置起來了,在網(wǎng)上找到很多資料,大多數(shù)例子基本都是相互拷貝,前后不搭調(diào),搞得不知所措。因此我把這次配置可以完整運(yùn)行的示例程序整理出來,分享,希望有時(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> <!- 線程池 -> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!- 核心線程數(shù) -> <property name="corePoolSize" value="4" /> <!- 最大線程數(shù) -> <proper
3、ty name="maxPoolSize" value="8" /> <!- 隊(duì)列最大長(zhǎng)度 -> <property name="queueCapacity" value="200" /> <!- 線程池維護(hù)線程所允許的空閑時(shí)間 -> <property name="keepAliveSeconds" value="300" /> <!- 線程池對(duì)拒絕任務(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"> <!- 要啟動(dòng)的主線程 -> <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)的類:MainPro.javapackage com.tz.controller;import
8、 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;/* * 要起動(dòng)的主線程 * 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("主線程分配完畢"); /* * 這個(gè)子線程來就是我們系統(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來源:Spring在schedule這塊支持JDK Timer、concurrent、quartz三種,這三種任務(wù)調(diào)度方案在實(shí)現(xiàn)機(jī)制和調(diào)用方法上都不同,但spring通過對(duì)其包裝,使得基于spring能用統(tǒng)一的配置和編碼風(fēng)格來使用這三種schedule方案??偟脕碚f這三種schedule都是基于scheduler->trigger->job的基本流程,因此spring
12、通過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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 預(yù)付貨款合同范本
- 個(gè)人承運(yùn)勞務(wù)合同范本
- 量具訂貨合同范本
- 夫妻出軌賠償合同范本
- 建設(shè)儲(chǔ)水塘合同范本
- 合辦公司合同范本
- 國內(nèi)旅游攻略
- 廚房出品操作講解
- 面膜的工藝流程
- 2025年中考作文模擬預(yù)測(cè):《成長(zhǎng)中的那束光》寫作指導(dǎo)+佳作展示
- 監(jiān)理平行檢查記錄表(最新全套)電子版本
- 高層建筑核心筒設(shè)計(jì)實(shí)例分析(共67頁)
- 液壓系統(tǒng)清潔度
- 陶瓷磚購銷合同模板直接用
- 說明書cp717應(yīng)用軟件操作手冊(cè)vol
- 多元函數(shù)的概念、極限與連續(xù)
- 手持電動(dòng)工具使用、檢查、維修安全管理制度與手持電動(dòng)工具安全制度
- 供應(yīng)鏈整合培訓(xùn)教材
- 一線員工技能等級(jí)評(píng)定方案
- 非洲豬瘟PCR檢測(cè)實(shí)驗(yàn)室建設(shè)方案參考
評(píng)論
0/150
提交評(píng)論