spring 集成 threadpool 测试demo

来源:互联网 发布:利用淘宝店铺做淘宝客 编辑:程序博客网 时间:2024/06/04 00:37
实现多线程的方法,集成Thread类,实现runnable接口
多线程测试spring threadpool 直接上代码
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

  <display-name>Spring3MVC</display-name>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.xml</param-value>
  </context-param>
  <context-param>
    <param-name>controller</param-name>
    <param-value>controller-log</param-value>
  </context-param>
  <context-param>
    <param-name>loggingLevel</param-name>
    <param-value>info</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
</web-app>

spring-servlet中引入 spring-threadpool.xml 线程池配置文件

<?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:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
     <mvc:annotation-driven />
     <import resource="spring-threadpool.xml" />
     <context:component-scan base-package="net.spring.*" />
     <bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
           <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
           <property name="prefix" value="/WEB-INF/jsp/" />
           <property name="suffix" value=".jsp" />
     </bean>
</beans>


线程池配置文件
<?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-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"
     default-autowire="byName">
     <bean  id="threadPool"
          class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
           <!-- 核心线程数 -->
           <property name="corePoolSize" value="10" />
           <!-- 最大线程数 -->
           <property name="maxPoolSize" value="10" />
           <!-- 队列最大长度 >=mainExecutor.maxSize -->
           <property name="queueCapacity" value="500" />
           <!-- 线程池维护线程所允许的空闲时间(秒) -->
           <property name="keepAliveSeconds" value="60" />
           <!-- 线程池对拒绝任务(无线程可用)的处理策略,使用默认的AbortPolicy -->
           <property name="rejectedExecutionHandler">
                <bean class="java.util.concurrent.ThreadPoolExecutor$AbortPolicy" />
           </property>
     </bean>
</beans>


controller 类:
@Component
@RequestMapping("/wallet")
public class RestTestController {
     @POST
     @RequestMapping(value = "/v3", method = RequestMethod.POST)
     public @ResponseBody ResponseEntitys callable() throws InterruptedException, ExecutionException {
           Future<ResponseEntitys> future=threadPool.submit(jerClient);
           ResponseEntitys rs=future.get();
           System.out.println("111111");
           return rs;
     }
}

测试方法: 使用多线程httpclient测试访问本地项目地址

public class Test extends Thread{
     public void run() {
           CloseableHttpClient httpclient = HttpClients.createDefault();
           String url = "http://localhost:8080/SpringMvcVsRest/rest/wallet/v3";
           HttpPost post = new HttpPost(url);
           CloseableHttpResponse httpResponse = null;
           // 发送get请求
           try {
                httpResponse = httpclient.execute(post);
                if (httpResponse.getStatusLine().getStatusCode() == 200) {
                     HttpEntity entity = httpResponse.getEntity();
                     try {
                           BufferedReader br = new BufferedReader(
                                     new InputStreamReader(entity.getContent()));
                           System.out.println(br.readLine());
                           // File f= new File("d:" + File.separator + "test.txt");
                           // OutputStream output=new FileOutputStream(f);
                           // //OutputStream output=System.out;
                           // entity.writeTo(output);
                           /*
                            * InputStream brins=entity.getContent();
                            * BufferedOutputStream fos = new BufferedOutputStream( new
                            * FileOutputStream("D:\\tmp\\yzm\\"+new
                            * Random().nextInt(100)+".jpg")); byte[] buf = new
                            * byte[1024]; int i = 0; while ((i = brins.read(buf)) !=
                            * -1) { fos.write(buf, 0, i); } brins.close(); fos.close();
                            */
                     } catch (ParseException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                     } catch (IOException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                     }
                }
           } catch (HttpHostConnectException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           } catch (ClientProtocolException e) {

                // TODO Auto-generated catch block
                e.printStackTrace();
           } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           }

     }
     public static void main(String[] args) throws ClientProtocolException,
                IOException {
           for(int i = 0; i < 13; i++){
                new Test().start();
                System.out.println(new Date().getTime());
           }
     }
}

测试发现 spring threadpool  并发量大于线程池等待数目时 线程池会拒绝多余的访问
原创粉丝点击