HTTP长连接与短连接使用方法及测试详解
//org.apache.http.impl.execchain.MainClientExec#execute ...... //从连接池中lease connection final HttpClientConnectionmanagedConn = connRequest.get(timeout > 0 ? timeout : 0, TimeUnit.MILLISECONDS); ...... //将conenction封装在ConnectionHolder中 final ConnectionHolder connHolder = new ConnectionHolder(this.log, this.connManager, managedConn); ...... // The connection is in or can be brought to a re-usable state. //如果返回值消息头中connection设置为close,则返回false if (reuseStrategy.keepAlive(response, context)) { // Set the idle duration of this connection //取出response消息头中,keep-alive的timeout值 final long duration = keepAliveStrategy.getKeepAliveDuration(response, context); if (this.log.isDebugEnabled()) { final String s; if (duration > 0) { s = "for " + duration + " " + TimeUnit.MILLISECONDS; } else { s = "indefinitely"; } this.log.debug("Connection can be kept alive " + s); } //设置失效时间 connHolder.setValidFor(duration, TimeUnit.MILLISECONDS); connHolder.markReusable(); } else { connHolder.markNonReusable(); } 待读取响应之后,释放连接,即:connHolder.releaseConnection()。调用org.apache.http.impl.conn.PoolingHttpClientConnectionManager#releaseConnection方法。 (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |