Spring3.2.6定时任务+线程池_第1页
Spring3.2.6定时任务+线程池_第2页
Spring3.2.6定时任务+线程池_第3页
Spring3.2.6定时任务+线程池_第4页
Spring3.2.6定时任务+线程池_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论