1.配置 ThreadPoolTaskExecutor bean
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 掃描注解 --> <context:component-scan base-package="com.qi.quartz"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <bean id="taskExecutor" name="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!-- 核心線程數 線程池維護線程的最少數量 --> <property name="corePoolSize" value="10" /> <!-- 線程池維護線程所允許的空閑時間 --> <property name="keepAliveSeconds" value="200" /> <!-- 線程池維護線程的最大數量 --> <property name="maxPoolSize" value="20" /> <!-- 線程池所使用的緩沖隊列 --> <property name="queueCapacity" value="100" /> <!-- 線程池對拒絕任務(無線程可用)的處理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,調用者的線程會執行該任務,如果執行器已關閉,則丟棄. --> <property name="rejectedExecutionHandler"> <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" /> </property> </bean> </beans>
2.controller使用
/**
* This file created at 2018年4月13日 下午3:06:57.
*
* Copyright (c) 2004-2014 AVTrace, Inc. All rights reserved.
*/
package com.avtrace.nlc.nms.listener;
import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author 唐芝泉
* @since 1.0.0
*/
@Controller
@RequestMapping("/test")
public class test implements SessionAwareMessageListener<Message> {
Logger LOG = LoggerFactory.getLogger(test.class);
@Resource(name = "taskExecutor")
private ThreadPoolTaskExecutor taskExecutor;
@RequestMapping("/execute")
@ResponseBody
@Override
public void onMessage(Message message, Session session) throws JMSException {
taskExecutor.execute(new Runnable(){
public void run() {
try {
LOG.info("執行線程任務開始前");
Thread.currentThread().sleep(1000);
if (LOG.isDebugEnabled()) {
LOG.info("執行線程任務結束");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
//設置線程的存活時間
taskExecutor.setKeepAliveSeconds(60);
}
}
3、cmd f: (因為我把ad.exe放到f:\下)
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="4" /> <!-- 並發線程數,想達到真正的並發效果,最好對應CPU的線程數及核心數 -->
<property name="maxPoolSize" value="1000" /> <!-- 最大線程池容量 -->
<property name="queueCapacity" value="2000" /> <!-- 超過最大線程池容量后,允許的線程隊列數 -->
</bean>
這里我是設置4並發數,最大線程池容量1000
F:\>ab -n 10000 -c 1000 http://192.168.40.21:8080/cctv/test/excute
這句命令表示10000次請求,1000個並發數 ,用時:1.206ms
比正常配置 10000*1000-4*1000=9996000
工具:鏈接: https://pan.baidu.com/s/1lg7BB-8OKSoluWOGjz03kw 密碼: dz7i