我采用单实例方式使用HttpClient和MultiThreadedHttpConnectionManager对象,在使用时将MultiThreadedHttpConnectionManager.maxHostConnections设置为10,将MultiThreadedHttpConnectionManager.maxTotalConnections设置为50。每秒钟发送10个请求,但从被访问主机的连接数看并不是10个连接数,而是大大超过10个,随着响应的时间越长,连接数还会增加。我测试过HttpClient 3.1版本和HttpClient 2.0均存在同样的问题。   被访问主机的连接数通过JBOSS的连接数疯长,netstat命令检查出客户端有很多FIN_WAIT_2状态,服务端有很多CLOSE_WAIT状态。
   客户端程序需要控制与主机的连接数,以防止主机承受压力过大,请问怎样才能控制主机的连接数?如何避免FIN_WAIT_2/CLOSE_WAIT状态?
   部分源码:
public class HttpSendClient
{
    private final HttpClient client = HttpClient(new MultiThreadedHttpConnectionManager());    protected HttpSendClient()
    {
        super();        client.setTimeout(5000);
        client.setConnectionTimeout(5000);
        client.setHttpConnectionFactoryTimeout(5000);
        final MultiThreadedHttpConnectionManager manager =
            (MultiThreadedHttpConnectionManager)client.getHttpConnectionManager();
        manager.setMaxConnectionsPerHost(10);
        manager.setMaxTotalConnections(20);
    }
.........
    private HttpResponse send(final HttpMethodBase method)
    {
        final HttpResponse response = new HttpResponse();
        try
        {
            client.executeMethod(method);
            response.setStatusCode(method.getStatusCode());
            response.setResponseAsString(method.getResponseBodyAsString().trim());
        }
        catch (Exception exp)
        {
            response.setStatusCode(XXX);
            response.setExcepMessage(httpExp.getMessage());
        }
         finally
        {
            method.releaseConnection();
        }
        return response;
    }
}

解决方案 »

  1.   

    被访问主机的连接数通过JBOSS的连接数获知。如下:
    ====================================================================================================================================
    Max threads: 256 Min spare threads: 64 Max spare threads: 256 Current thread count: 128 Current thread busy: 110
    Max processing time: 56631 ms Processing time: 6626 s Request count: 230 Error count: 27 Bytes received: 0.00 MB Bytes sent: 0.26 MBStage Time B Sent B Recv Client VHost Request
    R ? ? ? ? ? ?
    R ? ? ? ? ? ?
    S 35020 ms 0 KB 0 KB 127.0.0.1 localhost GET /testcase/UpSMResp.jsp?13651800016 HTTP/1.1
    S 25097 ms 0 KB 0 KB 127.0.0.1 localhost GET /testcase/UpSMResp.jsp?13651800018 HTTP/1.1
    R ? ? ? ? ? ?
    S 25065 ms 0 KB 0 KB 127.0.0.1 localhost GET /testcase/UpSMResp.jsp?13651800018 HTTP/1.1
    R ? ? ? ? ? ?
    R ? ? ? ? ? ?
    R ? ? ? ? ? ?
    S 25112 ms 0 KB 0 KB 127.0.0.1 localhost GET /testcase/UpSMResp.jsp?13651800018 HTTP/1.1
    R ? ? ? ? ? ?
    R ? ? ? ? ? ?
    R ? ? ? ? ? ?
    S 45177 ms 0 KB 0 KB 127.0.0.1 localhost GET /testcase/UpSMResp.jsp?13651800014 HTTP/1.1
    S 50037 ms 0 KB 0 KB 127.0.0.1 localhost GET /testcase/UpSMResp.jsp?13651800013 HTTP/1.1
    S 50053 ms 0 KB 0 KB 127.0.0.1 localhost GET /testcase/UpSMResp.jsp?13651800013 HTTP/1.1
    ...
    ...
      

  2.   

      有使用HttpClient的XDJM吗?知道的指导指导,谢谢啦。
      

  3.   

    Up!!补充:netstat命令检查出客户端有很多FIN_WAIT_2状态,服务端有很多CLOSE_WAIT状态.
      

  4.   

    private final HttpClient client = HttpClient(new MultiThreadedHttpConnectionManager()); final MultiThreadedHttpConnectionManager manager =
                (MultiThreadedHttpConnectionManager)client.getHttpConnectionManager();
            manager.setMaxConnectionsPerHost(10);
            manager.setMaxTotalConnections(20); 定义两个manager,你想用那个?