2014-06-06 52 views
1
try{ 

     // create new httpPost request with url of his class 
     HttpPost httpPost = new HttpPost("http://192.168.1.229:8080/flightcache/flightcache"); 

     // create params and add it to httpPost 
     List<NameValuePair> paramList = new ArrayList<NameValuePair>(); 
     paramList.add(new BasicNameValuePair("json_req", format)); 
     UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList); 
     httpPost.setEntity(formEntity); 

     // execute request and save response 
     CloseableHttpResponse response = httpclient.execute(httpPost, context); 

     HttpEntity entity = response.getEntity(); 
     for(Header header : response.getAllHeaders()){ 
      System.out.println(header.getName() + ":" + header.getValue()); 
     } 
     resp = entity.getContent().available() > 0; 

     response.close(); 
     httpclient.close(); 
     // return the response 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
} 

我試圖發送多個HttpPost請求併發到我的Servlet,但只有一個線程執行上面的代碼正在接收響應。我檢查了我的Servlet,但響應寫入正確。 httpClient創建如下。Java多線程HttpClient-4.3.3問題

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); 
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build(); 

任何人都可以請幫助/解釋我爲什麼只有一個線程正在接收響應?

預先感謝

public static void main(String[] args) throws Exception{ 
    FileUtil.init(); 

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); 
    cm.setMaxTotal(200); 
    cm.setDefaultMaxPerRoute(200); 
    CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build(); 

    HTTPThread.THREAD_COUNT = 2; 
    HTTPThread.start = new CountDownLatch(HTTPThread.THREAD_COUNT); 

    Thread[] threads = new Thread[ HTTPThread.THREAD_COUNT ]; 

    for(int i = 0; i < HTTPThread.THREAD_COUNT; i++){ 
     threads[ i ] = new Thread(new HTTPThread(httpClient)); 
    } 

    for(Thread thread : threads){ 
     thread.start(); 
    } 

    for(Thread thread : threads){ 
     thread.join(); 
    } 

    httpClient.close(); 

    System.out.println("Average response time: " + calAverage(HTTPThread.times) + " milliseconds."); 
} 

類HTTPThread:

public HTTPThread(CloseableHttpClient httpclient){ 
    this.httpclient = httpclient; 
    context = HttpClientContext.create(); 
} 

public void run(){ 
    String format = randomRequest(); 

    start.countDown(); 

    try{ 
     start.await(); 
    } 
    catch(InterruptedException e){ 
     e.printStackTrace(); 
    } 

    boolean resp = false; 
    long timeMillis = System.currentTimeMillis(); 
    try{ 

     // create new httpPost request with url of his class 
     HttpPost httpPost = new HttpPost("http://192.168.1.229:8080/flightcache/flightcache"); 

     // create params and add it to httpPost 
     List<NameValuePair> paramList = new ArrayList<NameValuePair>(); 
     paramList.add(new BasicNameValuePair("json_req", format)); 
     UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList); 
     httpPost.setEntity(formEntity); 

     // execute request and save response 
     CloseableHttpResponse response = httpclient.execute(httpPost, context); 

     HttpEntity entity = response.getEntity(); 
     resp = entity.getContent().available() > 0; 

     response.close(); 
     httpclient.close(); 
     // return the response 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
    long end = System.currentTimeMillis() - timeMillis; 

    if(!resp){ 
     System.out.println("Response was empty."); 
    } 

    if(end <= 0){ 
     times.add(1L); 
    } 
    else{ 
     times.add(end); 
    } 

} 
+0

你可以顯示你處理線程的代碼嗎? –

+0

我發佈了上面的代碼。這是一個簡單的主要方法,它創建一個定義的線程數並啓動它們。 – aQuip

+1

啊這是一個自定義的可運行 - 抱歉還需要HTTPThread類,在這一點上是主要的罪魁禍首。你可以改變自定義停止等待線程連接? http://docs.oracle.com/javase/tutorial/essential/concurrency/join.html –

回答

2

挖掘低谷代碼,HttpClient的是越來越關閉之前其他線程都得到他們的連接會

,因爲他們的一個機會全部使用相同的客戶端,HttpClient#close()應該在所有線程加入之後