2014-09-27 142 views
-1

最近我開始研究Java Servlet異步功能。我已經寫了下面顯示的示例代碼來檢查aysnc功能。我在單核處理器上運行它。我從jsp提交了100個請求(圖片請求)。我在代碼中添加了「Request Submmited :::」SOP。它顯示第一個0到6請求的SOP,然後一段時間後顯示SOP爲6到11 ....爲什麼它不顯示所有100個請求的SOP。Java Servlet異步支持

package com.test; 
import java.io.FileInputStream; 
import java.io.IOException; 

import javax.servlet.AsyncContext; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

@WebServlet(urlPatterns={"/Test"}, asyncSupported = true) 
public class Test extends HttpServlet { 
private static final long serialVersionUID = 1L; 

public Test() { 
    super(); 
} 

int counter = 0; 
@Override 
protected void doGet(final HttpServletRequest request, 
     final HttpServletResponse response) throws ServletException, 
     IOException { 
    System.out.println("Request Submmited:::" + counter++); 
    final AsyncContext ctx = request.startAsync(); 
    ctx.start(new Runnable() { 
     public void run() { 
      try { 
       String count = request.getParameter("test"); 
       try { 
        Thread.sleep(10000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       FileInputStream fin = new FileInputStream(
         "D:/TESTImages/8_0_0_NUCLEI" + count + ".jpg"); 
       byte[] data = new byte[fin.available()]; 
       fin.read(data); 
       response.getWriter().print(new String(data)); 
       response.flushBuffer(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      ctx.complete(); 
     } 
    }); 
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
} 

} 

回答

0

似乎默認線程池大小爲,對於procesess在你的servlet容器的異步請求池。嘗試提供init參數,如下所示:

@javax.servlet.annotation.WebServlet(urlPatterns={"/Test"}, asyncSupported = true, 
initParams = { @WebInitParam(name = "threadpoolsize", value = "100") })