spring-对线程池的支持


<!-- 线程的配置文件: corePoolSize: 线程池维护线程的最少数量  keepAliveSeconds  线程池维护线程所允许的空闲时间  maxPoolSize   线程池维护线程的最大数量 queueCapacity 线程池所使用的缓冲队列 -->


<bean id ="taskExecutor"  class ="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
        <property name ="corePoolSize" value ="10" />
        <property name ="maxPoolSize" value ="50" />
        <property name ="keepAliveSeconds" value ="300" /> 
        <property name ="queueCapacity" value ="1000" />
        <property name="rejectedExecutionHandler">
            <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
        </property>
    </bean>



spring 配置异步要点

 一般可以简单的用@Async来配置一个异步方法。例如
复制代码
1  /**
2  * 发送MIME格式的用户修改通知邮件
3  */
4 @Async 
5  public  void sendNotificationMail(Map keyValue,String toAddress,String subJect,String templateName) {
6 
7 String[] toList={toAddress};        sendNotificationMail(keyValue,toList,subJect,templateName) ;
8 }  
复制代码


但是这么做只是简单做法,大概积累3封邮件以后就会堵塞线程。

 

所以要加上配置文件

 

<task:annotation-driven executor="myExecutor" scheduler="myScheduler" />
<task:executor id="myExecutor" pool-size="50" />
<task:scheduler id="myScheduler" pool-size="1000" />  

 

但是只这么做,会报错

 

Caused by: org.xml.sax.SAXParseException: The prefix "task" for element "task:annotation-driven" is not bound. 

 

核心还是在最后。

 

在配置文件的前面加上

 

复制代码
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http: // www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http: // www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http: // www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http: // www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
        http: // www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http: // www.springframework.org/schema/data/jpa  http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http: // www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task.xsd "
         default-lazy-init="true">
复制代码

   

里面的task段落加上就OK了 




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值