關(guān)于HttpClient 引發(fā)的線程太多導(dǎo)致FullGc的問題
CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(connectionManager) .setMaxConnTotal(400) .setMaxConnPerRoute(150) .evictExpiredConnections() .build();
evictExpiredConnections 這個(gè)配置作用:
設(shè)置一個(gè)定時(shí)線程,定時(shí)清理閑置連接,可以將這個(gè)定時(shí)時(shí)間設(shè)置為 keep alive timeout 時(shí)間的一半以保證超時(shí)前回收
每個(gè)httpClient 對象都會(huì)有自己獨(dú)立的定時(shí)線程

這樣如果應(yīng)用中httpClient對象很多,就會(huì)導(dǎo)致上圖中線程太多
源碼中,如果設(shè)置了evictExpiredConnections 會(huì)有下面一段邏輯
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;
}
}
});
}
會(huì)出現(xiàn)一個(gè)線程,這個(gè)線程里面就是去關(guān)閉超時(shí)不用的閑置鏈接
到此這篇關(guān)于關(guān)于HttpClient 引發(fā)的線程太多導(dǎo)致FullGc的問題的文章就介紹到這了,更多相關(guān)HttpClient 引發(fā)的線程太多導(dǎo)致FullGc內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot利用Junit動(dòng)態(tài)代理實(shí)現(xiàn)Mock方法
說到Spring Boot 單元測試主要有兩個(gè)主流集成分別是Mockito,Junit,這個(gè)各有特點(diǎn),在實(shí)際開發(fā)中,我想要的測試框架應(yīng)該是這個(gè)框架集成者,本文給大家介紹了SpringBoot利用Junit動(dòng)態(tài)代理實(shí)現(xiàn)Mock方法,需要的朋友可以參考下2024-04-04
在同一個(gè)類中調(diào)用帶有@Transactional注解的方法示例
這篇文章主要為大家介紹了在同一個(gè)類中調(diào)用帶有@Transactional注解的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Java判斷一個(gè)字符串是不是一個(gè)數(shù)字的解決思路
這篇文章主要給大家介紹了關(guān)于Java判斷一個(gè)字符串是不是一個(gè)數(shù)字的解決思路,判斷一個(gè)字符串是否為數(shù)字是Java開發(fā)中很常見的業(yè)務(wù)需求,實(shí)現(xiàn)這個(gè)判斷有很多種方式,需要的朋友可以參考下2023-08-08
Java下http下載文件客戶端和上傳文件客戶端實(shí)例代碼
這篇文章主要介紹了Java下http下載文件客戶端和上傳文件客戶端實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
rabbitmq消息ACK確認(rèn)機(jī)制及發(fā)送失敗處理方式
這篇文章主要介紹了rabbitmq消息ACK確認(rèn)機(jī)制及發(fā)送失敗處理方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Java實(shí)現(xiàn)計(jì)算圖中兩個(gè)頂點(diǎn)的所有路徑
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)計(jì)算圖中兩個(gè)頂點(diǎn)的所有路徑功能,文中通過示例詳細(xì)講解了實(shí)現(xiàn)的方法,需要的可以參考一下2022-10-10

