java使用HttpClient线程池支持大量并发发起http请求

package com.ig.common.util;

import com.ig.common.utils.PropertiesHander;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
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.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * To change this template use File | Settings | File Templates.
 */
public class HttpClientPoolUtil {
    private static Logger logger = LoggerFactory.getLogger(HttpClientPoolUtil.class);

    public static PoolingHttpClientConnectionManager cm = null;

    public static CloseableHttpClient httpClient = null;

    /**
     * 默认content 类型
     */
    private static final String DEFAULT_CONTENT_TYPE = "application/json";

    /**
     * 默认请求超时时间30s
     */
    private static final int DEFAUL_TTIME_OUT = Integer.parseInt(PropertiesHander.getPropertiesValue("Http_Default_Timeout"));

    private static final int count = Integer.parseInt(PropertiesHander.getPropertiesValue("defaultMaxPerRoute"));

    private static final int totalCount = Integer.parseInt(PropertiesHander.getPropertiesValue("maxTotal"));

    private static final int Http_Default_Keep_Time = Integer.parseInt(PropertiesHander.getPropertiesValue("Http_Default_Keep_Time"));

    /**
     * 初始化连接池
     */
    public static synchronized void initPools() {
        if (httpClient == null) {
            cm = new PoolingHttpClientConnectionManager();
            cm.setDefaultMaxPerRoute(count);
            cm.setMaxTotal(totalCount);
            httpClient = HttpClients.custom().setKeepAliveStrategy(defaultStrategy).setConnectionManager(cm).build();
        }
    }

    /**
     * Http connection keepAlive 设置
     */
    public static ConnectionKeepAliveStrategy defaultStrategy = new ConnectionKeepAliveStrategy() {
        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
            int keepTime = Http_Default_Keep_Time;
            while (it.hasNext()) {
                HeaderElement he = it.nextElement();
                String param = he.getName();
                String value = he.getValue();
                if (value != null && param.equalsIgnoreCase("timeout")) {
                    try {
                        return Long.parseLong(value) * 1000;
                    } catch (Exception e) {
                        e.printStackTrace();
                        logger.error("format KeepAlive timeout exception, exception:" + e.toString());
                    }
                }
            }
            return keepTime * 1000;
        }
    };

    public static CloseableHttpClient getHttpClient() {
        return httpClient;
    }

    public static PoolingHttpClientConnectionManager getHttpConnectionManager() {
        return cm;
    }

    /**
     * 执行http post请求 默认采用Content-Typeapplication/jsonAcceptapplication/json
     *
     * @param uri 请求地址
     * @param data  请求数据
     * @return
     */
    public static String execute(String uri, String data) {
        long startTime = System.currentTimeMillis();
        HttpEntity httpEntity = null;
        HttpEntityEnclosingRequestBase method = null;
        String responseBody = "";
        try {
            if (httpClient == null) {
                initPools();
            }
            method = (HttpEntityEnclosingRequestBase) getRequest(uri, HttpPost.METHOD_NAME, DEFAULT_CONTENT_TYPE, 0);
            method.setEntity(new StringEntity(data));
            HttpContext context = HttpClientContext.create();
            CloseableHttpResponse httpResponse = httpClient.execute(method, context);
            httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                responseBody = EntityUtils.toString(httpEntity, "UTF-8");
            }

        } catch (Exception e) {
            if(method != null){
                method.abort();
            }
            e.printStackTrace();
            logger.error(
                    "execute post request exception, url:" + uri + ", exception:" + e.toString() + ", cost time(ms):"
                            + (System.currentTimeMillis() - startTime));
        } finally {
            if (httpEntity != null) {
                try {
                    EntityUtils.consumeQuietly(httpEntity);
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error(
                            "close response exception, url:" + uri + ", exception:" + e.toString() + ", cost time(ms):"
                                    + (System.currentTimeMillis() - startTime));
                }
            }
        }
        return responseBody;
    }

    /**
     * 创建请求
     *
     * @param uri 请求url
     * @param methodName 请求的方法类型
     * @param contentType contentType类型
     * @param timeout 超时时间
     * @return
     */
    public static HttpRequestBase getRequest(String uri, String methodName, String contentType, int timeout) {
        if (httpClient == null) {
            initPools();
        }
        HttpRequestBase method = null;
        if (timeout <= 0) {
            timeout = DEFAUL_TTIME_OUT;
        }
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout * 1000).setConnectTimeout(timeout * 1000)
                .setConnectionRequestTimeout(timeout * 1000).setExpectContinueEnabled(false).build();

        if (HttpPut.METHOD_NAME.equalsIgnoreCase(methodName)) {
            method = new HttpPut(uri);
        } else if (HttpPost.METHOD_NAME.equalsIgnoreCase(methodName)) {
            method = new HttpPost(uri);
        } else if (HttpGet.METHOD_NAME.equalsIgnoreCase(methodName)) {
            method = new HttpGet(uri);
        } else {
            method = new HttpPost(uri);
        }
        if (StringUtils.isBlank(contentType)) {
            contentType = DEFAULT_CONTENT_TYPE;
        }
        method.addHeader("Content-Type", contentType);
        method.addHeader("Accept", contentType);
        method.setConfig(requestConfig);
        return method;
    }

    /**
     * 执行GET 请求
     *
     * @param uri
     * @return
     */
    public static String execute(String uri) {
        long startTime = System.currentTimeMillis();
        HttpEntity httpEntity = null;
        HttpRequestBase method = null;
        String responseBody = "";
        try {
            if (httpClient == null) {
                initPools();
            }
            method = getRequest(uri, HttpGet.METHOD_NAME, DEFAULT_CONTENT_TYPE, 0);
            HttpContext context = HttpClientContext.create();
            CloseableHttpResponse httpResponse = httpClient.execute(method, context);
            httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                responseBody = EntityUtils.toString(httpEntity, "UTF-8");
                logger.info("请求URL: "+uri+"+  返回状态码:"+httpResponse.getStatusLine().getStatusCode());
            }
        } catch (Exception e) {
            if(method != null){
                method.abort();
            }
            e.printStackTrace();
            logger.error("execute get request exception, url:" + uri + ", exception:" + e.toString() + ",cost time(ms):"
                    + (System.currentTimeMillis() - startTime));
        } finally {
            if (httpEntity != null) {
                try {
                    EntityUtils.consumeQuietly(httpEntity);
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("close response exception, url:" + uri + ", exception:" + e.toString() + ",cost time(ms):"
                            + (System.currentTimeMillis() - startTime));
                }
            }
        }
        return responseBody;
    }
}
 
public class PropertiesHander {

    public static Properties properties = null ;

    public static String getPropertiesValue(String key){
        try {
            if(properties==null){
                properties = new Properties();
                String path =  PropertiesHander.class.getResource("/job_task.properties").getFile();
                FileInputStream in = new FileInputStream(new File(path));
                properties.load(in);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties.getProperty(key);
    }

}
job_task.properties
maxTotal=1000
defaultMaxPerRoute= 32
Http_Default_Timeout= 15000
Http_Default_Keep_Time= 15000
import com.alibaba.fastjson.JSON;
import com.ig.common.util.HttpClientPoolUtil;
import com.ig.gapi.result.bbin.BBINResultApi;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.springframework.web.servlet.view.AbstractView.DEFAULT_CONTENT_TYPE;


public class HCoreExecuteBBIN {
    private static final Logger logger = Logger.getLogger(CoreExecuteBBIN.class);


    final static ConcurrentMap<Class,Field[]> fieldsCache = new ConcurrentHashMap<Class,Field[]>();

    //组装参数发起Http请求API接口
    public static <T> T sendGet(Object parameter,Class<T> clazz,String url,int type) {
        try {
            //缓存参数
            Field[] fields = fieldsCache.get(parameter.getClass());

            if(fields == null){
                fields = parameter.getClass().getFields();
                fieldsCache.put(clazz,fields);
            }
            for (int i = 0; i < fields.length; i++) {
                Field filed = fields[i];
                String filedName = filed.getName();
                Object value = null;
                try {
                    value = filed.get(parameter);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

                if(value != null){
                    url = url.replace("${"+filedName+"}", value.toString());
                }
            }
            url = url.replaceAll("&[^\\&]*=\\$\\{.*?\\}", "");
            Pattern pattern = Pattern.compile("\\?.*?\\$\\{.*?\\}");
            Matcher matcher = pattern.matcher(url);
            if(matcher.find()){
                String matchUrl = matcher.group(0);
                url = url.replace(matchUrl.substring(1), "");
            }

            System.out.println("请求url: "+url);

            long startTime = System.currentTimeMillis();
            HttpEntity httpEntity = null;
            HttpEntityEnclosingRequestBase method = null;
            String responseBody = "";
            try {
                method = (HttpEntityEnclosingRequestBase) HttpClientPoolUtil.getRequest(url, HttpPost.METHOD_NAME, DEFAULT_CONTENT_TYPE, 0);
                method.addHeader("accept", "*/*");
                method.addHeader("connection", "Keep-Alive");
                method.addHeader("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");


                HttpContext context = HttpClientContext.create();
                CloseableHttpResponse httpResponse = HttpClientPoolUtil.httpClient.execute(method, context);
                httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    responseBody = EntityUtils.toString(httpEntity, "UTF-8");
                    Map <String,Object> mapp = JSON.parseObject(responseBody);
                    boolean returnFlag = Boolean.valueOf(mapp.get("result")+"");
                    logger.info("接口调用结果:"+returnFlag);
                    if(returnFlag){
                        return JSON.parseObject(responseBody, clazz);
                    }else {
                        mapp.put("result","false");
                        if(type==1){
                            BBINResultApi baseData = JSON.parseObject(responseBody, BBINResultApi.class);
                            mapp.remove("data");
                            mapp.put("baseData",baseData.getData());
                        }
                        return JSON.parseObject(JSON.toJSONString(mapp), clazz);
                    }

                }

            } catch (Exception e) {
                if(method != null){
                    method.abort();
                }
                e.printStackTrace();
                System.out.println("execute post request exception, url:" + url + ", exception:" + e.toString() + ", cost time(ms):" + (System.currentTimeMillis() - startTime));
            } finally {
                if (httpEntity != null) {
                    try {
                        EntityUtils.consumeQuietly(httpEntity);
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("close response exception, url:" + url + ", exception:" + e.toString() + ", cost time(ms):"+(System.currentTimeMillis() - startTime));
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("请求URL异常" + e);
            e.printStackTrace();
        }
        return null;
    }

    //BBIN游戏调用封装请求连接方法
    public static String  getPlayGameUrl(Object parameter,String url) {
        Field[] fields = fieldsCache.get(parameter.getClass());

        if (fields == null) {
            fields = parameter.getClass().getFields();
        }
        for (int i = 0; i < fields.length; i++) {
            Field filed = fields[i];
            String filedName = filed.getName();
            Object value = null;
            try {
                value = filed.get(parameter);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            if (value != null) {
                url = url.replace("${" + filedName + "}", value.toString());
            }
        }
        url = url.replaceAll("&[^\\&]*=\\$\\{.*?\\}", "");
        Pattern pattern = Pattern.compile("\\?.*?\\$\\{.*?\\}");
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            String matchUrl = matcher.group(0);
            url = url.replace(matchUrl.substring(1), "");
        }
        return url;
    }
}

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Java 生成 HttpClient并发请求第三方接口时,你可以使用 Apache HttpClient 库。以下是一个简单的示例代码: ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ConcurrentHttpClientExample { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); ExecutorService executorService = Executors.newFixedThreadPool(10); // 并发线程池 // 发起并发请求 for (int i = 0; i < 100; i++) { executorService.execute(() -> { HttpGet httpGet = new HttpGet("https://api.example.com/endpoint"); // 替换为实际的接口地址 try { HttpResponse response = httpClient.execute(httpGet); BufferedReader reader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); String line; StringBuilder responseContent = new StringBuilder(); while ((line = reader.readLine()) != null) { responseContent.append(line); } System.out.println("Response: " + responseContent.toString()); } catch (IOException e) { e.printStackTrace(); } }); } executorService.shutdown(); // 关闭线程池 } } ``` 在上述示例中,我们首先创建了一个 HttpClient 实例,并创建了一个具有固定线程数的线程池。然后使用线程池发起并发请求,每个线程执行一个 HttpGet 请求,并处理响应结果。 请注意,在实际使用中,你可能需要根据第三方接口的要求进行设置,如请求头、请求参数等。此外,还需要适当处理异常、重试机制和流量控制等情况,以保证请求的稳定性和性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值