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了 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM