HttpComponents HttpClient连接池(2)-连接的申请

本文深入探讨了HttpClient连接池中连接的申请和释放过程。讲解了如何通过Cpool对象的lease()方法获取连接,涉及RouteSpecificPool、CPoolEntry的管理,包括过期检查、关闭连接及等待超时的处理。最后预告了下一篇文章将讨论http连接的释放。
摘要由CSDN通过智能技术生成

上一篇文章里我们主要介绍了 httpclient 连接池的关键类和数据结构,在这里我们主要介绍http连接的申请和释放。

http连接的申请

http 连接的申请主要调用上一篇文章 Cpool 对象(在父类AbstractConnPool)的 lease() 方法,该方法会返回 Future<CPoolEntry> 对象,该对象调用 get 方法得到 CPoolEntry ,而在 get 方法里又调用 CPool 实例的 getPoolEntryBlocking() 方法,所以该方法是核心,代码如下:

private E getPoolEntryBlocking(final T route, final Object state, final long timeout, final TimeUnit timeUnit, final Future<E> future) throws IOException, InterruptedException, TimeoutException {
        Date deadline = null;
        if (timeout > 0) {
            deadline = new Date (System.currentTimeMillis() + timeUnit.toMillis(timeout));
        }
        this.lock.lock();
        try {
            final RouteSpecificPool<T, C, E> pool = getPool(route);
            E entry;
            for (;;) {
                Asserts.check(!this.isShutDown, "Connection pool shut down");
                for (;;) {
                    entry = pool.getFree(state);
                    if (entry == null) {
                        break;
                    }
                    if (entry.isExpired(System.currentTimeMillis())) {
                        entry.close();
                    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值