简体   繁体   English

Java Http连接池中的连接收回策略

[英]Connection Eviction strategy in Http Connection Pooling in Java

I am trying to implement a http connection pooling in java for a web service. 我正在尝试为Web服务在Java中实现http连接池。 The service will receive a request and then call other http services. 该服务将收到一个请求,然后调用其他http服务。

public final class HttpClientPool {
 private static HttpClientPool instance = null;
 private PoolingHttpClientConnectionManager manager;
 private IdleConnectionMonitorThread monitorThread;
 private final CloseableHttpClient client;

 public static HttpClientPool getInstance() {
  if (instance == null) {
   synchronized(HttpClientPool.class) {
    if (instance == null) {
     instance = new HttpClientPool();
    }
   }
  }
  return instance;
 }

 private HttpClientPool() {
  manager = new PoolingHttpClientConnectionManager();
  client = HttpClients.custom().setConnectionManager(manager).build();
  monitorThread = new IdleConnectionMonitorThread(manager);
  monitorThread.setDaemon(true);
  monitorThread.start();
 }

 public CloseableHttpClient getClient() {
  return client;
 }
}


class IdleConnectionMonitorThread extends Thread {
 private final HttpClientConnectionManager connMgr;
 private volatile boolean shutdown;

 IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
  super();
  this.connMgr = connMgr;
 }

 @Override
 public void run() {
  try {
   while (!shutdown) {
    synchronized(this) {
     wait(5000);
     // Close expired connections
     connMgr.closeExpiredConnections();
     // Optionally, close connections
     // that have been idle longer than 30 sec
     connMgr.closeIdleConnections(60, TimeUnit.SECONDS);
    }
   }
  } catch (InterruptedException ex) {
   //
  }
 }

 void shutdown() {
  shutdown = true;
  synchronized(this) {
   notifyAll();
  }
 }
}
  1. As mentioned in Connection Management doc for Connection Eviction strategy instead of using a IdleConnectionMonitorThread what if I use manager.setValidateAfterInactivity . 连接管理策略的连接管理文档中所述,而不是使用IdleConnectionMonitorThread ,如果我使用manager.setValidateAfterInactivity怎样? What are the pros & cons of the above two approach? 以上两种方法的优缺点是什么?

  2. Is the above Http Connection Pool implementation correct? 上面的Http连接池实现正确吗?

With #setValidateAfterInactivity set to a positive value persistent connections will get validated upon lease request. #setValidateAfterInactivity设置为正值,则持久连接将根据租赁请求进行验证。 That is, stale and non-reusable connections will not get automatically evicted from the pool until an attempt is made to re-use them. 也就是说,在尝试重新使用陈旧且不可重用的连接之前,它们不会从池中自动退出。

Running a dedicated thread that iterates over persistent connections at the specified time interval and removes expired or idle connections from the pool ensures proactive connection eviction at the cost of an extra thread and slightly higher pool lock contention. 运行在指定的时间间隔上遍历持久性连接并从池中删除过期或空闲连接的专用线程可确保主动驱逐连接,而这需要额外的线程和稍高的池锁争用。

In HttpClient 4.5.3, manager.setValidateAfterInactivity has a defult value of 2000 which is 2 seconds. 在HttpClient 4.5.3中, manager.setValidateAfterInactivity的默认值为2000,即2秒。 So I would suggest not to use a IdleConnectionMonitorThread unless you want the application to validate inactive connections and clean up at the same time. 因此,我建议不要使用IdleConnectionMonitorThread除非您希望应用程序验证不活动的连接并同时进行清理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM