SpringMVC整合TaskExecutor線程池的配置/使用

task.propertiesjava

#核心線程數
task.core_pool_size=5
#最大線程數
task.max_pool_size=50
#隊列最大長度
task.queue_capacity=1000
#線程池維護線程所容許的空閒時間,默認爲60s
task.keep_alive_seconds=60

task.xmlnginx

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:task="http://www.springframework.org/schema/task"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.2.xsd">

    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <!-- 核心線程數 -->
        <property name="corePoolSize" value="${task.core_pool_size}" />
        <!-- 最大線程數 -->
        <property name="maxPoolSize" value="${task.max_pool_size}" />
        <!-- 隊列最大長度 -->
        <property name="queueCapacity" value="${task.queue_capacity}" />
        <!-- 線程池維護線程所容許的空閒時間,默認爲60s -->
        <property name="keepAliveSeconds" value="${task.keep_alive_seconds}" />
    </bean>
    <!-- 註解式 -->
    <task:annotation-driven />
</beans>

Controller.javaspring

@Resource(name = "taskExecutor")
private TaskExecutor taskExecutor;

//使用
taskExecutor.execute(new Runnable() {
            public void run() {
                //這裏編寫處理業務代碼
                try {
                    smsService.sendMsg(telPhone, content);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
相關文章
相關標籤/搜索