欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

关于HttpClient 引发的线程太多导致FullGc的问题

时间:2021-03-20 10:00:15|栏目:JAVA代码|点击:
CloseableHttpClient httpClient = HttpClients.custom()
  .setConnectionManager(connectionManager)
  .setMaxConnTotal(400)
  .setMaxConnPerRoute(150)
  .evictExpiredConnections()
  .build();

evictExpiredConnections 这个配置作用:

设置一个定时线程,定时清理闲置连接,可以将这个定时时间设置为 keep alive timeout 时间的一半以保证超时前回收

每个httpClient 对象都会有自己独立的定时线程

这样如果应用中httpClient对象很多,就会导致上图中线程太多

源码中,如果设置了evictExpiredConnections 会有下面一段逻辑

 List<Closeable> closeablesCopy = closeables != null ? new ArrayList<Closeable>(closeables) : null;
  if (!this.connManagerShared) {
   if (closeablesCopy == null) {
    closeablesCopy = new ArrayList<Closeable>(1);
   }
   final HttpClientConnectionManager cm = connManagerCopy;
 
   if (evictExpiredConnections || evictIdleConnections) {
    final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor(cm,
      maxIdleTime > 0 ? maxIdleTime : 10, maxIdleTimeUnit != null ? maxIdleTimeUnit : TimeUnit.SECONDS,
      maxIdleTime, maxIdleTimeUnit);
    closeablesCopy.add(new Closeable() {
 
     @Override
     public void close() throws IOException {
      connectionEvictor.shutdown();
      try {
       connectionEvictor.awaitTermination(1L, TimeUnit.SECONDS);
      } catch (final InterruptedException interrupted) {
       Thread.currentThread().interrupt();
      }
     }
 
    });
    connectionEvictor.start();
   }
   closeablesCopy.add(new Closeable() {
 
    @Override
    public void close() throws IOException {
     cm.shutdown();
    }
 
   });
  }

IdleConnectionEvictor 对象是

public final class IdleConnectionEvictor {
 
 private final HttpClientConnectionManager connectionManager;
 private final ThreadFactory threadFactory;
 private final Thread thread;
 private final long sleepTimeMs;
 private final long maxIdleTimeMs;
 
 private volatile Exception exception;
 
 public IdleConnectionEvictor(
   final HttpClientConnectionManager connectionManager,
   final ThreadFactory threadFactory,
   final long sleepTime, final TimeUnit sleepTimeUnit,
   final long maxIdleTime, final TimeUnit maxIdleTimeUnit) {
  this.connectionManager = Args.notNull(connectionManager, "Connection manager");
  this.threadFactory = threadFactory != null ? threadFactory : new DefaultThreadFactory();
  this.sleepTimeMs = sleepTimeUnit != null ? sleepTimeUnit.toMillis(sleepTime) : sleepTime;
  this.maxIdleTimeMs = maxIdleTimeUnit != null ? maxIdleTimeUnit.toMillis(maxIdleTime) : maxIdleTime;
  this.thread = this.threadFactory.newThread(new Runnable() {
   @Override
   public void run() {
    try {
     while (!Thread.currentThread().isInterrupted()) {
      Thread.sleep(sleepTimeMs);
      connectionManager.closeExpiredConnections();
      if (maxIdleTimeMs > 0) {
       connectionManager.closeIdleConnections(maxIdleTimeMs, TimeUnit.MILLISECONDS);
      }
     }
    } catch (final Exception ex) {
     exception = ex;
    }
 
   }
  });
 }

会出现一个线程,这个线程里面就是去关闭超时不用的闲置链接

上一篇:Jmeter如何添加循环控制器

栏    目:JAVA代码

下一篇:idea springboot 修改css,jsp不重启实现页面更新的问题

本文标题:关于HttpClient 引发的线程太多导致FullGc的问题

本文地址:http://www.codeinn.net/misctech/84216.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有