Java httpClient連接池支持多線程高并發(fā)的實(shí)現(xiàn)
當(dāng)采用HttpClient httpClient = HttpClients.createDefault() 實(shí)例化的時(shí)候。會(huì)導(dǎo)致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)
采用連接池來(lái)創(chuàng)建httpClient 解決了這個(gè)問(wèn)題,避免資源一直占用不釋放的問(wèn)題。
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 { //類加載的時(shí)候 設(shè)置最大連接數(shù) 和 每個(gè)路由的最大連接數(shù)
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;
}
}
到此這篇關(guān)于Java httpClient連接池支持多線程高并發(fā)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java httpClient連接池多線程高并發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對(duì)Java ArrayList的自動(dòng)擴(kuò)容機(jī)制示例講解
今天小編就為大家分享一篇對(duì)Java ArrayList的自動(dòng)擴(kuò)容機(jī)制示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Java實(shí)現(xiàn)電影院訂票系統(tǒng)代碼
這篇文章主要介紹了Java實(shí)現(xiàn)電影院訂票系統(tǒng)代碼,代碼實(shí)現(xiàn)了界面類登錄注冊(cè)類,用戶類等,具有一定參考價(jià)值,需要的朋友可以參考下。2017-11-11
Java實(shí)現(xiàn)利用圖片或視頻生成GIF并發(fā)送微信
這篇文章主要為大家詳細(xì)介紹了Java語(yǔ)言如何利用圖片或視頻實(shí)現(xiàn)生成GIF并發(fā)送微信的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-11-11
Java基礎(chǔ)類學(xué)習(xí)之String詳解
這篇文章主要為大家詳細(xì)介紹了Java基礎(chǔ)類中String的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2022-12-12
java實(shí)現(xiàn)隨機(jī)輸出300題四則運(yùn)算
本文主要介紹了java實(shí)現(xiàn)隨機(jī)輸出300題四則運(yùn)算實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
在springboot中如何使用filter設(shè)置要排除的URL
這篇文章主要介紹了在springboot中如何使用filter設(shè)置要排除的URL,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

