




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Spring3.2.6 框架多线程序配置【定时任务+线程池】费劲周折终于把这个功能配置起来了,在网上找到很多资料,大多数例子基本都是相互拷贝,前后不搭调,搞得不知所措。因此我把这次配置可以完整运行的示例程序整理出来,分享,希望有时间再进一步优化,共同进步。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"> <!- 核心线程数 -> <property name="corePoolSize" value="4" /> <!- 最大线程数 -> <proper
3、ty name="maxPoolSize" value="8" /> <!- 队列最大长度 -> <property name="queueCapacity" value="200" /> <!- 线程池维护线程所允许的空闲时间 -> <property name="keepAliveSeconds" value="300" /> <!- 线程池对拒绝任务(无线程可用)的处理策略 -> <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" /> <!- 任务间隔时间(sleep时间)执行完第一组任务,执行第二组任务相隔时间,两个任务在时间上不应该相交 -> <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>程序对应的类: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); /等待池中线程执行完 while(taskEx
10、ecutor.getActiveCount()>0) try Thread.sleep(5000); catch (InterruptedException e) e.printStackTrace(); System.out.println("主线程分配完毕"); /* * 这个子线程来就是我们系统要处理的任务 * 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三种,这三种任务调度方案在实现机制和调用方法上都不同,但spring通过对其包装,使得基于spring能用统一的配置和编码风格来使用这三种schedule方案。总得来说这三种schedule都是基于scheduler->trigger->job的基本流程,因此spring
12、通过TimerFactoryBean、ScheduledExecutorFactoryBean和SchedulerFactoryBean分别实现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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- Taylor-Couette混凝实验絮凝剂残留铝的影响因素研究
- 黑龙江省农业农村现代化水平评价及优化路径研究
- miR-21-5p通过影响TAMs在肺癌进展中的作用及机制研究
- 我长大了-健康活动
- 腹泻的护理要点
- 小孩子机器人教育培训
- 工厂质量培训课件
- 预防诈骗主题班会课件
- 预防地震知识培训课件
- 火灾预防知识培训
- 酒店卫生管理自查报告和整改措施
- 安全教育培训:实现安全文明施工
- 2025至2030分布式能源行业市场深度调研及发展规划及有效策略与实施路径评估报告
- 反邪教宣讲课件
- 2025年全国统一高考英语Ⅰ卷(含答案)
- 1 感受生活中的法律 课件-道德与法治六年级上册统编版
- 股份代持及员工持股计划协议书范本
- 中医集市活动方案
- 2025年江苏省南京市中考历史试卷(含解析)
- 肿瘤随访登记培训
- 劳动仲裁内部培训
评论
0/150
提交评论