转载于
http://www.httpclient.cn/archives/78.html
public class App
{
private static CloseableHttpClient httpClient = null;
private static void initHttpClient()
{
final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
// 总连接池数量
connectionManager.setMaxTotal(5);
// 可为每个域名设置单独的连接池数量
connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost("www.httpclient.cn")), 1);
// setConnectTimeout表示设置建立连接的超时时间
// setConnectionRequestTimeout表示从连接池中拿连接的等待超时时间
// setSocketTimeout表示发出请求后等待对端应答的超时时间
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000).setConnectionRequestTimeout(2000)
.setSocketTimeout(3000).build();
// 重试处理器,StandardHttpRequestRetryHandler这个是官方提供的,不过很多错误不能重试,可自己实现HttpRequestRetryHandler接口去做
HttpRequestRetryHandler retryHandler = new StandardHttpRequestRetryHandler();
httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig)
.setRetryHandler(retryHandler).build();
// 服务端假设关闭了连接,对客户端是不透明的,HttpClient为了缓解这一问题,在某个连接使用前会检测这个连接是否过时,如果过时则连接失效,但是这种做法会为每个请求
// 增加一定额外开销,因此有一个定时任务专门回收长时间不活动而被判定为失效的连接,可以某种程度上解决这个问题
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run()
{
try
{
// 关闭失效连接并从连接池中移除
connectionManager.closeExpiredConnections();
// 关闭30秒钟内不活动的连接并从连接池中移除,空闲时间从交还给连接管理器时开始
connectionManager.closeIdleConnections(30, TimeUnit.SECONDS);
}
catch (Throwable t)
{
t.printStackTrace();
}
}
}, 0, 1000 * 2);
}
public static void main(String[] args) throws IOException
{
initHttpClient();
HttpGet httpGet = new HttpGet("www.httpclient.cn");
try
{
CloseableHttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
}
catch (Exception e)
{
}
finally
{
httpClient.close();
}
}
}