Java httpClient连接池支持多线程高并发的实现

作者:qzqanlhy1314 时间:2022-09-14 04:13:15 

当采用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;
   }
}

来源:https://blog.csdn.net/qzqanzc/article/details/84344655

标签:Java,多线程,高并发
0
投稿

猜你喜欢

  • SpringBoot 集成 activiti的示例代码

    2023-01-22 10:22:03
  • SpringBoot异步与事务一起使用的问题解决

    2023-03-17 07:11:08
  • 在 Ubuntu Linux 上安装 Oracle Java 14的方法

    2022-01-01 15:13:52
  • Java日期与时间类原理解析

    2021-07-20 14:00:36
  • C#警惕匿名方法造成的变量共享实例分析

    2021-08-26 19:35:22
  • java实现新浪微博Oauth接口发送图片和文字的方法

    2023-11-29 01:43:04
  • Java中对象与C++中对象的放置安排的对比

    2022-05-31 15:07:18
  • Java使用Apache POI库读取Excel表格文档的示例

    2021-06-02 04:32:49
  • java多线程中执行多个程序的实例分析

    2023-03-11 18:21:58
  • Java输入年份和月份判断多少天实例代码

    2023-12-23 10:43:11
  • Android Studio使用小技巧:提取方法代码片段

    2023-11-14 19:35:20
  • C#实现希尔排序

    2023-11-02 08:15:04
  • DevExpress实现自定义TreeListNode的Tooltip的方法

    2022-08-19 05:39:47
  • Java实现对两个List快速去重并排序操作示例

    2023-05-02 03:40:14
  • C#中ExecuteNonQuery()返回值注意点分析

    2023-04-05 03:40:14
  • Java动态获取实现某个接口下所有的实现类对象集合

    2023-04-01 14:43:20
  • Android 在viewPager中双指缩放图片双击缩放图片单指拖拽图片的实现思路

    2021-12-09 14:04:22
  • Spring如何处理注解的深入理解

    2023-09-09 14:35:49
  • Java基础知识之ByteArrayInputStream流的使用

    2023-10-27 14:37:53
  • Java如何把数组转换为ArrayList

    2021-08-14 19:59:45
  • asp之家 软件编程 m.aspxhome.com