设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 服务器 > 安全 > 正文

HTTP长连接与短连接使用方法及测试详解(3)

发布时间:2020-05-10 17:48 所属栏目:53 来源:站长网
导读:@Override public void releaseConnection(final HttpClientConnection managedConn,final Object state,final long keepalive, final TimeUnit tunit) {Args.notNull(managedConn, "Managed connection");synchron

@Override public void releaseConnection(final HttpClientConnection managedConn, final Object state,final long keepalive, final TimeUnit tunit) { Args.notNull(managedConn, "Managed connection"); synchronized (managedConn) { final CPoolEntry entry = CPoolProxy.detach(managedConn); if (entry == null) { return; } final ManagedHttpClientConnection conn = entry.getConnection(); try { if (conn.isOpen()) { final TimeUnit effectiveUnit = tunit != null ? tunit : TimeUnit.MILLISECONDS; entry.setState(state); //设置失效时间 entry.updateExpiry(keepalive, effectiveUnit); } } finally { 。。。。。。 } } } }

然后再下一次HTTP操作,从连接池中获取连接时

//org.apache.http.impl.conn.PoolingHttpClientConnectionManager#requestConnection调用org.apache.http.pool.AbstractConnPool#lease, //调用getPoolEntryBlocking,调用org.apache.http.impl.conn.CPoolEntry#isExpired @Override public boolean isExpired(final long now) { final boolean expired = super.isExpired(now); if (expired && this.log.isDebugEnabled()) { //日志中看到的内容 this.log.debug("Connection " + this + " expired @ " + new Date(getExpiry())); } return expired; }

综上,连接的实际有效时间,是根据response的设置来决定的。

其他情况测试

客户端设置Connection: Close

##connection:close请求,kept alive的连接为0 [2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection request: [route: {}->:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200] [2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection leased: [id: 0][route: {}->:80][total kept alive: 0; route allocated: 1 of 32; total allocated: 1 of 200] [2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.execchain.MainClientExec:?) - Opening connection {}->:80 [2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator:?) - Connecting to bizdomain/127.0.0.195:80 ## 建立新连接 [2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator:?) - Connection established 127.0.0.191:49239<->127.0.0.195:80 ## 客户端设置短连接 [2017-04-26 13:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 >> "Connection: Close[\r][\n]" ## 服务端返回的也是短连接 [2017-04-26 13:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 << "Connection: close[\r][\n]" ##请求完之后,关闭连接 [2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.DefaultManagedHttpClientConnection:?) - http-outgoing-0: Close connection [2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.execchain.MainClientExec:?) - Connection discarded [2017-04-26 13:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection released: [id: 0][route: {}->:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200]

如上,当服务端返回Connection: Close时,客户端接收完响应,便会关闭连接。

客户端设置60s超时,服务端设置5s超时

##Keep-Alive: timeout=60 第一次请求,与connection:close无差别 [2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection request: [route: {}->:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200] [2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection leased: [id: 0][route: {}->:80][total kept alive: 0; route allocated: 1 of 32; total allocated: 1 of 200] [2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.execchain.MainClientExec:?) - Opening connection {}->:80 ## 客户端设置超时时间60s [2017-04-26 10:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 >> "Connection: keep-alive[\r][\n]" [2017-04-26 10:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 >> "Keep-Alive: timeout=60[\r][\n]" ## 服务端设置超时时间5s [2017-04-26 10:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 << "Keep-Alive: timeout=5, max=100[\r][\n]" [2017-04-26 10:57:00 DEBUG] (org.apache.http.wire:?) - http-outgoing-0 << "Connection: Keep-Alive[\r][\n]" ## 服务端设置生效,连接可以保持5s [2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.execchain.MainClientExec:?) - Connection can be kept alive for 5000 MILLISECONDS [2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection [id: 0][route: {}->:80] can be kept alive for 5.0 seconds [2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection released: [id: 0][route: {}->:80][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 200] ##Keep-Alive: timeout=60 非第一次请求 [2017-04-26 14:11:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection request: [route: {}->:80][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 200] ## 连接在上一次请求结束后5s失效 [2017-04-26 14:11:00 DEBUG] (org.apache.http.impl.conn.CPool:?) - Connection [id:2][route:{}->:80][state:null] expired @ Wed Apr 26 14:10:05 GMT+08:00 2017

客户端设置失效时间,服务端设置不失效

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读