您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java httpClient連接池支持多線程高并發的實現方法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Java httpClient連接池支持多線程高并發的實現方法”吧!
當采用HttpClient httpClient = HttpClients.createDefault() 實例化的時候。會導致Address already in use的異常。
信息: I/O exception (java.net.BindException) caught when processing request to {}->http://**.**.**.** Address already in use: connect
十一月 22, 2018 5:02:13 下午 org.apache.http.impl.execchain.RetryExec execute
信息: Retrying request to {}->http://**.**.**.**
java.net.BindException: Address already in use: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
采用連接池來創建httpClient 解決了這個問題,避免資源一直占用不釋放的問題。
import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpRequest; import org.apache.http.NoHttpResponseException; import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.io.InterruptedIOException; import java.net.UnknownHostException; import java.util.HashMap; import java.util.Map; public class WebTools { public static String KEY_STATUS_CODE = "statusCode"; public static String KEY_CONTENT = "content"; private final static PoolingHttpClientConnectionManager poolConnManager = new PoolingHttpClientConnectionManager(); //連接池管理器 private final static HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() { //retry handler public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= 5) { return false; } if (exception instanceof NoHttpResponseException) { return true; } if (exception instanceof InterruptedIOException) { return false; } if (exception instanceof UnknownHostException) { return false; } if (exception instanceof ConnectTimeoutException) { return false; } HttpClientContext clientContext = HttpClientContext .adapt(context); HttpRequest request = clientContext.getRequest(); if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; } }; static { //類加載的時候 設置最大連接數 和 每個路由的最大連接數 poolConnManager.setMaxTotal(2000); poolConnManager.setDefaultMaxPerRoute(1000); } /** * ########################### core code####################### * @return */ private static CloseableHttpClient getCloseableHttpClient() { CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(poolConnManager) .setRetryHandler(httpRequestRetryHandler) .build(); return httpClient; } /** * buildResultMap * * @param response * @param entity * @return * @throws IOException */ private static Map<String, Object> buildResultMap(CloseableHttpResponse response, HttpEntity entity) throws IOException { Map<String, Object> result; result = new HashMap<>(2); result.put(KEY_STATUS_CODE, response.getStatusLine().getStatusCode()); //status code if (entity != null) { result.put(KEY_CONTENT, EntityUtils.toString(entity, "UTF-8")); //message content } return result; } /** * send json by post method * * @param url * @param message * @return * @throws Exception */ public static Map<String, Object> postJson(String url, String message) { Map<String, Object> result = null; CloseableHttpClient httpClient = getCloseableHttpClient(); HttpPost httpPost = new HttpPost(url); CloseableHttpResponse response = null; try { httpPost.setHeader("Accept", "application/json;charset=UTF-8"); httpPost.setHeader("Content-Type", "application/json"); StringEntity stringEntity = new StringEntity(message); stringEntity.setContentType("application/json;charset=UTF-8"); httpPost.setEntity(stringEntity); response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); result = buildResultMap(response, entity); } catch (Exception e) { e.printStackTrace(); } finally { if (response != null) { try { EntityUtils.consume(response.getEntity()); response.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } }
感謝各位的閱讀,以上就是“Java httpClient連接池支持多線程高并發的實現方法”的內容了,經過本文的學習后,相信大家對Java httpClient連接池支持多線程高并發的實現方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。