paulwong

          My Links

          Blog Stats

          常用鏈接

          留言簿(66)

          隨筆分類(1387)

          隨筆檔案(1145)

          文章分類(7)

          文章檔案(10)

          相冊

          收藏夾(2)

          AI

          Develop

          E-BOOK

          Other

          養(yǎng)生

          微服務(wù)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          http connection存活期

          HTTP1.1的鏈接,默認是長鏈接,不會主動關(guān)閉。
          LINUX會默認保留鏈接5天再關(guān)閉。
          建立HTTP鏈接其實也是調(diào)用TCL的協(xié)議去建立,包括開始的時候有三次握手,關(guān)閉的時候有四次握手。關(guān)閉鏈接雙方都可以發(fā)起。
          但這些鏈接可能會被防火墻關(guān)掉而不通知建立鏈接的雙方,因此設(shè)置需設(shè)置鏈接的存活期。
          使用httpClient的鏈接池時,要設(shè)置池中的鏈接存活期或設(shè)置存活策略。
          檢測存活期只在每次發(fā)送數(shù)據(jù)時,才檢測取出的鏈接是否超過存活期,如超過則關(guān)閉。
          設(shè)置存活期的策略:

          import java.util.concurrent.TimeUnit;

          import org.apache.http.HeaderElement;
          import org.apache.http.HeaderElementIterator;
          import org.apache.http.HttpResponse;
          import org.apache.http.conn.ConnectionKeepAliveStrategy;
          import org.apache.http.message.BasicHeaderElementIterator;
          import org.apache.http.protocol.HTTP;
          import org.apache.http.protocol.HttpContext;
          import org.apache.http.util.Args;

          public class MyConnectionKeepAliveStrategy implements ConnectionKeepAliveStrategy{
              
              
              private int timeToLive;
              private TimeUnit timeUnit;

              public MyConnectionKeepAliveStrategy(int timeToLive, TimeUnit timeUnit) {
                  this.timeToLive = timeToLive;
                  this.timeUnit = timeUnit;
              }

              @Override
              public long getKeepAliveDuration(final HttpResponse response, final HttpContext context) {
                  Args.notNull(response, "HTTP response");
                  final HeaderElementIterator it = new BasicHeaderElementIterator(
                          response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                  while (it.hasNext()) {
                      final HeaderElement he = it.nextElement();
                      final String param = he.getName();
                      final String value = he.getValue();
                      if (value != null && param.equalsIgnoreCase("timeout")) {
                          try {
                              return Long.parseLong(value) * 1000;
                          } catch(final NumberFormatException ignore) {
                              //do nothing
                          }
                      }
                  }
                  return timeUnit.toMillis(timeToLive);
              }

          }

          《HttpClient官方文檔》2.6 連接維持存活策略
          http://ifeve.com/httpclient-2-6/

          httpclient連接池管理,你用對了?
          http://ifeve.com/http-connection-pool/

          HttpClient連接池的一些思考
          https://zhuanlan.zhihu.com/p/85524697

          HTTP協(xié)議的Keep-Alive 模式
          https://www.jianshu.com/p/49551bda6619

          posted on 2020-12-31 15:08 paulwong 閱讀(357) 評論(0)  編輯  收藏 所屬分類: HTTPCLIENT

          主站蜘蛛池模板: 焉耆| 商城县| 无棣县| 醴陵市| 安化县| 闽清县| 同心县| 保康县| 沙田区| 响水县| 伊吾县| 永春县| 扶绥县| 安龙县| 绥中县| 双辽市| 台州市| 陈巴尔虎旗| 嵩明县| 德格县| 太康县| 磐安县| 繁峙县| 北辰区| 青龙| 修文县| 兴宁市| 磐安县| 阿坝| 沅陵县| 涞水县| 凯里市| 开原市| 梅州市| 敖汉旗| 大英县| 新疆| 吉首市| 崇礼县| 象山县| 陆河县|