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

来源:互联网 发布:网络餐饮问题 编辑:程序博客网 时间:2024/06/07 12:35
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=1000defaultMaxPerRoute= 32Http_Default_Timeout= 15000Http_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;    }}


原创粉丝点击