【问题标题】:Java Multithreading HttpClient-4.3.3 problemsJava 多线程 HttpClient-4.3.3 问题
【发布时间】:2014-06-06 07:54:53
【问题描述】:
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();
}

我正在尝试同时向我的 Servlet 发送多个 HttpPost 请求,但只有一个执行上述代码的线程正在接收响应。我检查了我的 Servlet,但响应写入正确。 httpClient 创建如下。

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

}

【问题讨论】:

  • 你能展示你处理线程的代码吗?
  • 我把上面的代码贴出来了。这是一个简单的主方法,它创建定义的线程数并启动它们。
  • 啊,它是一个自定义的可运行文件 - 抱歉,还需要 HTTPThread 类,此时它是罪魁祸首。您可以使用线程连接更改自定义停止等待吗? docs.oracle.com/javase/tutorial/essential/concurrency/join.html
  • 好的,我发布了更新的主方法并发布了 HTTPThread 类
  • 对不起,它与线程无关 - 你可以在线程加入和测试后移动那个 httpclient.close() 吗?

标签: java multithreading servlets apache-httpclient-4.x


【解决方案1】:

挖掘您的代码,HttpClient 在其他线程有机会建立连接之前已经关闭

因为他们都使用同一个客户端,所以 HttpClient#close() 应该在所有线程加入之后

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多