public class PoolUtil {
private static CloseableHttpClient httpClient;
private static PoolingHttpClientConnectionManager poolingHttpClientConnectionManager;
private static final int MAX_CONN_TOTAL = 10;
private static final int MAX_CONN_PERROUTE = 10;
static {
poolingHttpClientConnectionManager = initPool();
}
public static HttpClient getHttpClient(){
if(null != httpClient){
return httpClient;
}
synchronized (PoolUtil.class){
if(null == httpClient){
httpClient = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
return httpClient;
}
}
return httpClient;
}
private static PoolingHttpClientConnectionManager initPool(){
PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
pool.setMaxTotal(MAX_CONN_
HttpClient连接池的使用示例
最新推荐文章于 2024-08-27 19:45:00 发布
本文详细介绍了Apache HttpClient如何配置和使用连接池,通过实例代码展示了如何优化HTTP请求,提高并发性能,减少网络延迟,深入理解连接池的工作原理及其在HttpClient中的应用。
摘要由CSDN通过智能技术生成