HttpClient 로 인 한 스 레 드 가 너무 많아 서 FullGc 에 문제 가 생 겼 습 니 다.


CloseableHttpClient httpClient = HttpClients.custom()
  .setConnectionManager(connectionManager)
  .setMaxConnTotal(400)
  .setMaxConnPerRoute(150)
  .evictExpiredConnections()
  .build();
evictExpiredConnections 이 설정 역할:
정시 스 레 드 를 설정 하고 유 휴 연결 을 정리 합 니 다.이 정시 시간 을 keep alive timeout 시간의 절반 으로 설정 하여 시간 초과 전 회수 할 수 있 습 니 다.
http Client 대상 마다 독립 된 타 이 밍 스 레 드 가 있 습 니 다.

이렇게 하면 http Client 대상 이 많 으 면 위의 그림 에 스 레 드 가 너무 많 습 니 다.
원본 코드 에서 evictExpired Connections 를 설정 하면 다음 논리 가 있 습 니 다.

 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();
    }
 
   });
  }
IdleConnection Evictor 대상 은?

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;
    }
 
   }
  });
 }
스 레 드 가 나타 날 수 있 습 니 다.이 스 레 드 안 에는 시간 이 초과 되 어 사용 하지 않 는 유 휴 링크 를 닫 는 것 입 니 다.
HttpClient 로 인 한 스 레 드 가 너무 많아 서 FullGC 에 문제 가 생 긴 다 는 글 을 소개 합 니 다.HttpClient 로 인 한 스 레 드 가 너무 많아 서 FullGC 내용 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많이 응원 해 주세요!

좋은 웹페이지 즐겨찾기