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();
}

I'm trying to send multiple HttpPost request concurrently to my Servlet but only one Thread executing the code above is receiving a response. I checked my Servlet but the responses are written correctly. httpClient was created as following.

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

Can anyone please help/explain me why only one Thread is receiving a response?

Thank in advance

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." );
}

Class 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 );
    }

}
6
  • can you show the code where you handle the threading? Commented Jun 6, 2014 at 8:37
  • I posted the code above. It is a simple main-method which creates a defined count of thread and starts them. Commented Jun 6, 2014 at 8:42
  • 1
    ah it is a custom runnable - sorry will need also the HTTPThread class which at this point is the main culprit. can you change the custom stop await with thread joins? docs.oracle.com/javase/tutorial/essential/concurrency/join.html Commented Jun 6, 2014 at 9:16
  • okay I posted the updated main-method and postet the class HTTPThread Commented Jun 6, 2014 at 9:37
  • 1
    sorry it was unrelated to the threading - can you move that httpclient.close() after the thread joins and test? Commented Jun 6, 2014 at 9:54

1 Answer 1

2

Digging trough your code, HttpClient is getting closed before other threads have a chance of getting their connection going

as they all use the same client, HttpClient#close() should be after all the threads are joined

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.