聊聊HttpClient的close

本文主要研究一下HttpClient的close

CloseableHttpClient

org/apache/http/impl/client/CloseableHttpClient.java

@Contract(threading = ThreadingBehavior.SAFE)
public abstract class CloseableHttpClient implements HttpClient, Closeable {

    @Override
    public <T> T execute(final HttpHost target, final HttpRequest request,
            final ResponseHandler<? extends T> responseHandler, final HttpContext context)
            throws IOException, ClientProtocolException {
        Args.notNull(responseHandler, "Response handler");

        final CloseableHttpResponse response = execute(target, request, context);
        try {
            final T result = responseHandler.handleResponse(response);
            final HttpEntity entity = response.getEntity();
            EntityUtils.consume(entity);
            return result;
        } catch (final ClientProtocolException t) {
            // Try to salvage the underlying connection in case of a protocol exception
            final HttpEntity entity = response.getEntity();
            try {
                EntityUtils.consume(entity);
            } catch (final Exception t2) {
                // Log this exception. The original exception is more
                // important and will be thrown to the caller.
                this.log.warn("Error consuming content after an exception.", t2);
            }
            throw t;
        } finally {
            response.close();
        }
    }

    //......
}

CloseableHttpClient声明实现HttpClient, Closeable接口

InternalHttpClient

org/apache/http/impl/client/InternalHttpClient.java

@Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
@SuppressWarnings("deprecation")
class InternalHttpClient extends CloseableHttpClient implements Configurable {

    private final Log log = LogFactory.getLog(getClass());

    private final ClientExecChain execChain;
    private final HttpClientConnectionManager connManager;
    private final HttpRoutePlanner routePlanner;
    private final Lookup<CookieSpecProvider> cookieSpecRegistry;
    private final Lookup<AuthSchemeProvider> authSchemeRegistry;
    private final CookieStore cookieStore;
    private final CredentialsProvider credentialsProvider;
    private final RequestConfig defaultConfig;
    private final List<Closeable> closeables;

    public InternalHttpClient(
            final ClientExecChain execChain,
            final HttpClientConnectionManager connManager,
            final HttpRoutePlanner routePlanner,
            final Lookup<CookieSpecProvider> cookieSpecRegistry,
            final Lookup<AuthSchemeProvider> authSchemeRegistry,
            final CookieStore cookieStore,
            final CredentialsProvider credentialsProvider,
            final RequestConfig defaultConfig,
            final List<Closeable> closeables) {
        super();
        Args.notNull(execChain, "HTTP client exec chain");
        Args.notNull(connManager, "HTTP connection manager");
        Args.notNull(routePlanner, "HTTP route planner");
        this.execChain = execChain;
        this.connManager = connManager;
        this.routePlanner = routePlanner;
        this.cookieSpecRegistry = cookieSpecRegistry;
        this.authSchemeRegistry = authSchemeRegistry;
        this.cookieStore = cookieStore;
        this.credentialsProvider = credentialsProvider;
        this.defaultConfig = defaultConfig;
        this.closeables = closeables;
    }

    //......

    @Override
    public void close() {
        if (this.closeables != null) {
            for (final Closeable closeable: this.closeables) {
                try {
                    closeable.close();
                } catch (final IOException ex) {
                    this.log.error(ex.getMessage(), ex);
                }
            }
        }
    }    
}    

InternalHttpClient继承了CloseableHttpClient,其构造器要求传入closeables,它实现了close方法,它主要是遍历closeables,挨个执行close

HttpClientBuilder

org/apache/http/impl/client/HttpClientBuilder.java

public class HttpClientBuilder {

	private List<Closeable> closeables;

	private boolean connManagerShared;

	//......

    /**
     * For internal use.
     */
    protected void addCloseable(final Closeable closeable) {
        if (closeable == null) {
            return;
        }
        if (closeables == null) {
            closeables = new ArrayList<Closeable>();
        }
        closeables.add(closeable);
    }

    public CloseableHttpClient build() {

    	//......

        List<Closeable> closeablesCopy = closeables != null ? new ArrayList<Closeable>(closeables) : null;
        if (!this.connManagerShared) {
            if (closeablesCopy == null) {
                closeablesCopy = new ArrayList<Closeable>(1);
            }
            final HttpClientConnectionManager cm = connManagerCopy;

            if (evictExpiredConnections || evictIdleConnections) {
                final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor(cm,
                        maxIdleTime > 0 ? maxIdleTime : 10, maxIdleTimeUnit != null ? maxIdleTimeUnit : TimeUnit.SECONDS,
                        maxIdleTime, maxIdleTimeUnit);
                closeablesCopy.add(new Closeable() {

                    @Override
                    public void close() throws IOException {
                        connectionEvictor.shutdown();
                        try {
                            connectionEvictor.awaitTermination(1L, TimeUnit.SECONDS);
                        } catch (final InterruptedException interrupted) {
                            Thread.currentThread().interrupt();
                        }
                    }

                });
                connectionEvictor.start();
            }
            closeablesCopy.add(new Closeable() {

                @Override
                public void close() throws IOException {
                    cm.shutdown();
                }

            });
        }

        //......

        return new InternalHttpClient(
                execChain,
                connManagerCopy,
                routePlannerCopy,
                cookieSpecRegistryCopy,
                authSchemeRegistryCopy,
                defaultCookieStore,
                defaultCredentialsProvider,
                defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT,
                closeablesCopy);
    }
}    

HttpClientBuilder定义了addCloseable方法用于添加closeable,不过是protected;而build方法在connManagerShared参数为false的时候(默认)会创建closeablesCopy,创建Closeable去关闭HttpClientConnectionManager并添加到closeablesCopy中;

在开启evictExpiredConnections或者evictIdleConnections的时候会创建IdleConnectionEvictor,然后创建关闭connectionEvictor的Closeable添加到closeablesCopy中

最后将这些closeablesCopy传递给InternalHttpClient的构造器

小结

HttpClient(CloseableHttpClient)的close方法会关闭一系列的Closeable,这些Closeable在HttpClientBuilder的build方法会构建好然后传递给InternalHttpClient;默认情况下这些closeable包括HttpClientConnectionManager的关闭、IdleConnectionEvictor的关闭。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpClient close_wait不释放是指当HttpClient执行完请求后,关闭连接后却不能及时释放连接,出现了close_wait状态,可能会导致资源的浪费和程序性能的降低。这种情况通常发现在服务器的TCP连接数一直在增加,却一直处于close_wait状态,并且ClientSocketImpl的状态一直是CLOSED_WAIT,造成资源的浪费和服务器处理能力的下降。 造成HttpClient close_wait不释放的原因可能有以下几点: 1. 连接池没有及时释放连接。HttpClient有一个默认的连接池管理器,如果连接池同时被多个线程调用,可能会出现连接池没有及时释放连接的情况,导致close_wait状态的产生。 2. Http客户端没有及时关闭连接。有些Http客户端请求完成后不主动释放连接,导致连接一直处于close_wait状态。 3. 服务器没有及时关闭连接。如果服务器没有在超时时间内关闭连接,那么连接就会一直处于close_wait状态。 解决HttpClient close_wait不释放的方法有以下几点: 1. 使用HttpClient连接池管理器时,需要设置合理的连接超时时间和请求超时时间,及时关闭闲置连接或者过期的连接。 2. 在Http客户端请求完成后,及时关闭连接,避免连接一直处于close_wait状态。 3. 调整服务器的超时时间,及时关闭连接。 4. 调整服务器的TCP/IP协议参数,设置合理的TIME_WAIT时间。 总之,为了避免出现close_wait状态的连接,需要在使用HttpClient连接池管理器时,合理调整连接超时时间和请求超时时间,并在请求完成后及时释放连接,避免资源的浪费和程序性能的降低。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值