最近遇到線上機器的日志報錯:error message:[]Error log: thread pool is full
定位原因是httpclient創建連接后沒有及時關閉,
使用httpClient.getConnectionManager().shutdown();只能執行一次,下次執行會報錯:Connection pool shut down
為了關閉連接,特意找到了兩種關閉連接的方法
方法1:關閉CloseableHttpResponse (推薦)
關閉此流並釋放與其關聯的所有系統資源。 如果流已經關閉,則調用此方法無效
public static void release(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient) throws IOException { // 釋放資源 if (httpResponse != null) { httpResponse.close(); } if (httpClient != null) { httpClient.close(); } }
方法2:不能直接close時,可使用response.getEntity().getContent().close()
可關閉此輸入流並釋放與該流關聯的所有系統資源