httpclient httpclient连接回收

httpclient连接释放

 httpClient必须releaseConnection,但不是abort。因为releaseconnection是归还连接到到连接池,而abort是直接抛弃这个连接,
而且占用连接池的数目。
HttpGet httpget = new HttpGet(url);
httpGet.releaseConnection();

连接回收策略

   经典的IO阻塞模式有一个主要的缺点,就是当IO操作被阻塞的时候,网络socket只对I/O事件影响。
当一个连接释放回管理器,它会保持活跃,然后它不会监听socket的状态和任何IO事件。如果这个连接在服务器端被关闭,客户端的连接在连接状态下不会检测出这个改变。
HttpClient通过测试连接是否为"陈腐的"而尝试去缓解这个问题,"陈腐的"是指不再是有效的,因为它会被服务器端关闭掉。并会在这之前为了正执行中的HTTP请求去使用连接。
"陈腐的"连接检测不是百分之百有效的,并且会给每个请求执行增加10毫秒,为了空闲连接,唯一有效的解决方法是在每个socket模型里不包含一个线程,有一个专门的监听线程是被用来驱逐已经过期不活跃的长连接的。这个监听线程会周期中的调用ClientConnectionManager #closeExpiredConnections()方法去关闭所有已经过期的连接并从连接池里驱逐已经关闭的连接。在超过指定的过期后,也可以随意地调用ClientConnectionManager#closeIdleConnections()方法来关闭所有连接。
public static class IdelConnectionMonitorThread extends Thread{
  private final HttpClientConnectionManager connMgr;
  private volatile boolean shutdown;
  
  public IdleConnectionMonitorThread(HttpClientManager connMgr){
    super();
    this.connMgr = connMgr; 
  }
​
  public void run(){
    try{
       while(!shutdown){
          synchronized(this){
             wait(5000);
             //Close expired connections
             connMgr.closeExpiredConnections();
             //Optionally close connections that have bean idle longer than 30 sec
             connMgr.closeIdelConnections(30,TimeUnit.SECONDS);
          }
       }
    }catche(Exception e){
       //terminate
    }
  }
​
  public void shutdown(){
     shutdown=true;
     synchronized(this){
      notifyAll();
     }
  }
}

原文地址:https://www.cnblogs.com/ssgao/p/8829289.html