|
17 | 17 | import java.io.InputStream; |
18 | 18 | import java.io.PrintWriter; |
19 | 19 | import java.io.StringWriter; |
| 20 | +import java.lang.reflect.Field; |
20 | 21 | import java.net.InetSocketAddress; |
21 | 22 | import java.net.Proxy; |
22 | 23 | import java.net.Socket; |
@@ -497,8 +498,32 @@ public static CloseableHttpClient initHttpClientWithoutDecompression( |
497 | 498 | */ |
498 | 499 | public static CloseableHttpClient initHttpClient(HttpClientSettingsKey key, File ocspCacheFile) { |
499 | 500 | updateRoutePlanner(key); |
500 | | - return httpClient.computeIfAbsent( |
501 | | - key, k -> buildHttpClient(key, ocspCacheFile, key.getGzipDisabled())); |
| 501 | + CloseableHttpClient closeableHttpClient = |
| 502 | + httpClient.computeIfAbsent( |
| 503 | + key, k -> buildHttpClient(key, ocspCacheFile, key.getGzipDisabled())); |
| 504 | + if (detectClosedConnectionManager(closeableHttpClient)) { |
| 505 | + httpClient.remove(key); |
| 506 | + return httpClient.computeIfAbsent( |
| 507 | + key, k -> buildHttpClient(key, ocspCacheFile, key.getGzipDisabled())); |
| 508 | + } |
| 509 | + return closeableHttpClient; |
| 510 | + } |
| 511 | + |
| 512 | + private static boolean detectClosedConnectionManager(CloseableHttpClient closeableHttpClient) |
| 513 | + throws RuntimeException { |
| 514 | + // There is no simple way to detect is the connection manager is closed... |
| 515 | + try { |
| 516 | + Field connManagerField = closeableHttpClient.getClass().getDeclaredField("connManager"); |
| 517 | + connManagerField.setAccessible(true); |
| 518 | + PoolingHttpClientConnectionManager connectionManager = |
| 519 | + (PoolingHttpClientConnectionManager) connManagerField.get(closeableHttpClient); |
| 520 | + Field isShutdownField = connectionManager.getClass().getDeclaredField("isShutDown"); |
| 521 | + isShutdownField.setAccessible(true); |
| 522 | + AtomicBoolean isShutdown = (AtomicBoolean) isShutdownField.get(connectionManager); |
| 523 | + return isShutdown.get(); |
| 524 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 525 | + throw new RuntimeException(e); |
| 526 | + } |
502 | 527 | } |
503 | 528 |
|
504 | 529 | /** |
|
0 commit comments