1 import lombok.extern.slf4j.Slf4j;
2 import org.apache.http.HttpEntity;
3 import org.apache.http.HttpResponse;
4 import org.apache.http.HttpStatus;
5 import org.apache.http.NameValuePair;
6 import org.apache.http.client.HttpRequestRetryHandler;
7 import org.apache.http.client.config.RequestConfig;
8 import org.apache.http.client.entity.UrlEncodedFormEntity;
9 import org.apache.http.client.methods.CloseableHttpResponse;
10 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
11 import org.apache.http.client.methods.HttpPost;
12 import org.apache.http.client.methods.HttpRequestBase;
13 import org.apache.http.config.Registry;
14 import org.apache.http.config.RegistryBuilder;
15 import org.apache.http.conn.socket.ConnectionSocketFactory;
16 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
17 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
18 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
19 import org.apache.http.entity.StringEntity;
20 import org.apache.http.impl.client.CloseableHttpClient;
21 import org.apache.http.impl.client.HttpClients;
22 import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
23 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
24 import org.apache.http.message.BasicNameValuePair;
25 import org.apache.http.ssl.SSLContextBuilder;
26 import org.apache.http.util.EntityUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.io.IOException;
31 import java.io.UnsupportedEncodingException;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.concurrent.ScheduledExecutorService;
37 import java.util.concurrent.ScheduledThreadPoolExecutor;
38 import java.util.concurrent.TimeUnit;
39
40 /**
41 * 可为每个域名设置单独的连接池数量
42 * connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost("www.baidu.com")), 1);
43 * setConnectTimeout表示设置建立连接的超时时间
44 * setConnectionRequestTimeout表示从连接池中拿连接的等待超时时间
45 * setSocketTimeout表示发出请求后等待对端应答的超时时间
46 */
47
48 @Slf4j
49 public class HttpConnectionPoolUtils {
50
51 private static final Logger LOGGER = LoggerFactory.getLogger(HttpConnectionPoolUtils.class);
52
53 private static final String ENCODING = "UTF-8";
54
55 private static PoolingHttpClientConnectionManager connectionManager = null;
56
57 private static CloseableHttpClient httpClient;
58
59 static {
60 LOGGER.info("初始化http connection 连接池 ...");
61 try {
62 // 配置同时支持 HTTP 和 HTPPS
63 SSLContextBuilder builder = new SSLContextBuilder();
64 builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
65 SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(builder.build());
66 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslConnectionSocketFactory).build();
67 connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
68 }catch (Exception e){
69 LOGGER.error("初始化http 连接池异常",e);
70 connectionManager = new PoolingHttpClientConnectionManager();
71 }
72 // 总连接池数量
73 connectionManager.setMaxTotal(20);
74 connectionManager.setDefaultMaxPerRoute(100);
75 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000).setConnectionRequestTimeout(2000).setSocketTimeout(3000).build();
76 HttpRequestRetryHandler retryHandler = new StandardHttpRequestRetryHandler();
77
78 httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).setRetryHandler(retryHandler).build();
79
80 ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1);
81 scheduledExecutorService.scheduleWithFixedDelay(() -> {
82 connectionManager.closeExpiredConnections();
83 connectionManager.closeIdleConnections(20,TimeUnit.SECONDS);
84 LOGGER.info("回收过期的http连接完成 status:{}",connectionManager.getTotalStats());
85 },30,120,TimeUnit.SECONDS);
86
87 Runtime.getRuntime().addShutdownHook(new Thread(() -> {
88 log.info("关闭 httpClient 连接");
89 try {
90 if(httpClient != null){
91 httpClient.close();
92 }
93 } catch (IOException e) {
94 log.error("关闭 httpClient 异常",e);
95 }
96 }));
97 }
98
99 public static HttpClientResult doPost(String url, Map<String, String> params) throws IOException {
100 return doPost(url, null, params);
101 }
102
103 public static HttpClientResult doPost(String url, Map<String, String> headers, Map<String, String> params) throws IOException {
104
105 log.info("http post 请求:url={}", url);
106 log.info("http post 请求:params = {}", params);
107 // 创建httpClient对象
108 HttpPost httpPost = new HttpPost(url);
109 packageHeader(headers, httpPost);
110 // 封装请求参数
111 try {
112 packageParam(params, httpPost);
113 } catch (UnsupportedEncodingException e) {
114 throw e;
115 }
116
117 // 创建httpResponse对象
118 CloseableHttpResponse httpResponse = null;
119
120 try {
121 // 执行请求并获得响应结果
122 long startTime = System.currentTimeMillis();
123 HttpClientResult httpClientResult = getHttpClientResult(httpResponse, httpClient, httpPost);
124 long endTime = System.currentTimeMillis();
125 log.info("http post 请求相应时间:{}", (endTime-startTime));
126 log.info("http post 请求返回结果:{}", httpClientResult);
127 return httpClientResult;
128 } catch (Exception e) {
129 throw e;
130 } finally {
131 // 释放资源
132 if (httpResponse != null) {
133 httpResponse.close();
134 }
135 }
136 }
137
138 public static void packageHeader(Map<String, String> params, HttpRequestBase httpMethod) {
139 // 封装请求头
140 if (params != null) {
141 Set<Map.Entry<String, String>> entrySet = params.entrySet();
142 for (Map.Entry<String, String> entry : entrySet) {
143 // 设置到请求头到HttpRequestBase对象中
144 httpMethod.setHeader(entry.getKey(), entry.getValue());
145 }
146 }
147 }
148
149 public static void packageParam(Map<String, String> params, HttpEntityEnclosingRequestBase httpMethod)
150 throws UnsupportedEncodingException {
151 // 封装请求参数
152 if (params != null) {
153 List<NameValuePair> nvps = new ArrayList<NameValuePair>();
154 Set<Map.Entry<String, String>> entrySet = params.entrySet();
155 for (Map.Entry<String, String> entry : entrySet) {
156 nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
157 }
158
159 // 设置到请求的http对象中
160 httpMethod.setEntity(new UrlEncodedFormEntity(nvps, ENCODING));
161 }
162 }
163
164 public static HttpClientResult getHttpClientResult(CloseableHttpResponse httpResponse,
165 CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws IOException {
166 // 执行请求
167 httpResponse = httpClient.execute(httpMethod);
168
169 // 获取返回结果
170 if (httpResponse != null && httpResponse.getStatusLine() != null) {
171 String content = "";
172 if (httpResponse.getEntity() != null) {
173 content = EntityUtils.toString(httpResponse.getEntity(), ENCODING);
174 }
175 return new HttpClientResult(httpResponse.getStatusLine().getStatusCode(), content);
176 }
177 return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR);
178 }
179
180 public static String doPost(String url,String postBody) throws IOException {
181 HttpPost httpPost = new HttpPost(url);
182 StringEntity entity = new StringEntity(postBody,"utf-8");
183 entity.setContentEncoding("UTF-8");
184 entity.setContentType("application/json");
185 httpPost.setEntity(entity);
186 HttpResponse resp = httpClient.execute(httpPost);
187
188 String respContent = "";
189 if(resp.getStatusLine().getStatusCode() == 200) {
190 HttpEntity he = resp.getEntity();
191 respContent = EntityUtils.toString(he,"UTF-8");
192 }
193 return respContent;
194 }
195 }
196

池化HttpClient,拿去就能用的更多相关文章

  1. Deep Learning 学习随记(七)Convolution and Pooling --卷积和池化

    图像大小与参数个数: 前面几章都是针对小图像块处理的,这一章则是针对大图像进行处理的.两者在这的区别还是很明显的,小图像(如8*8,MINIST的28*28)可以采用全连接的方式(即输入层和隐含层直接 ...

  2. 对象池化技术 org.apache.commons.pool

    恰当地使用对象池化技术,可以有效地减少对象生成和初始化时的消耗,提高系统的运行效率.Jakarta Commons Pool组件提供了一整套用于实现对象池化的框架,以及若干种各具特色的对象池实现,可以 ...

  3. 第十三节,使用带有全局平均池化层的CNN对CIFAR10数据集分类

    这里使用的数据集仍然是CIFAR-10,由于之前写过一篇使用AlexNet对CIFAR数据集进行分类的文章,已经详细介绍了这个数据集,当时我们是直接把这些图片的数据文件下载下来,然后使用pickle进 ...

  4. TensorFlow池化层-函数

    池化层的作用如下-引用<TensorFlow实践>: 池化层的作用是减少过拟合,并通过减小输入的尺寸来提高性能.他们可以用来对输入进行降采样,但会为后续层保留重要的信息.只使用tf.nn. ...

  5. 【TensorFlow】tf.nn.max_pool实现池化操作

    max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似 有些地方可以从卷积去参考[TensorFlow]tf.nn.conv2d是怎样实现卷积的? tf.nn.max_pool(va ...

  6. tf入门-池化函数 tf.nn.max_pool 的介绍

    转载自此大神 http://blog.csdn.net/mao_xiao_feng/article/details/53453926 max pooling是CNN当中的最大值池化操作,其实用法和卷积 ...

  7. 基于深度学习和迁移学习的识花实践——利用 VGG16 的深度网络结构中的五轮卷积网络层和池化层,对每张图片得到一个 4096 维的特征向量,然后我们直接用这个特征向量替代原来的图片,再加若干层全连接的神经网络,对花朵数据集进行训练(属于模型迁移)

    基于深度学习和迁移学习的识花实践(转)   深度学习是人工智能领域近年来最火热的话题之一,但是对于个人来说,以往想要玩转深度学习除了要具备高超的编程技巧,还需要有海量的数据和强劲的硬件.不过 Tens ...

  8. 卷积和池化的区别、图像的上采样(upsampling)与下采样(subsampled)

    1.卷积 当从一个大尺寸图像中随机选取一小块,比如说 8x8 作为样本,并且从这个小块样本中学习到了一些特征,这时我们可以把从这个 8x8 样本中学习到的特征作为探测器,应用到这个图像的任意地方中去. ...

  9. 深入解析CNN pooling 池化层原理及其作用

    原文地址:https://blog.csdn.net/CVSvsvsvsvs/article/details/90477062 池化层作用机理我们以最简单的最常用的max pooling最大池化层为例 ...

随机推荐

  1. 关于爬取babycenter.com A-Z为顺序的所有英文名及其详细属性

     这一次爬取的内容已经在标题里提到了,下面是详细要求及其图示: 1.首先以A-Z的顺序获取所有英文名,最后爬取该英文名的详细信息. 2.CSV的header以3中的单词为准,请别拼错.如果没有对应的数 ...

  2. 下载Abook 高等教育出版社网站资料

    一.背景 又快到了期末复习周,这个学期学了一门操作系统,老师没有给课本习题的答案,说是配套网站上有,我看了一下,确实有,是高等教育出版社的数字课程网站Abookl http://abook.hep.c ...

  3. 终极CURD-4-java8新特性

    目录 1 概述 2 lambda表达式 2.1 lambda重要知识点总结 2.2 java内置函数接口 2.3 方法引用 2.4 构造器引用 2.5 数组引用 2.6 lambda表达式的陷阱 3 ...

  4. 【活动】美团技术沙龙第49期:AI在外卖场景中的最佳实践

    美团技术沙龙第49期开始啦! 本次沙龙,美团外卖技术部专家会深入介绍AI在对话系统.图像处理.个性化推荐.智能营销等方向在外卖业务中的实践,希望与业界技术同学一起交流学习. 无论你从事智能搜索,或是算 ...

  5. 爬虫(五):代理IP、Cookie

    1. 代理IP 代理IP这个功能呢,在urllib和requests中都存在,但是这个在大的爬虫项目中是非常重要的,所以我拿出来单独讲解. 对于某些网站,如果同一个 IP 短时间内发送大量请求,则可能 ...

  6. Sql: Oracle paging

    --书分类目录kind --涂聚文 Geovin Du create table geovindu.BookKindList ( BookKindID INT PRIMARY KEY, BookKin ...

  7. 【红宝书】第20章.JSON

      JSON是一种轻量级的数据格式.JSON使用JS语法的子集表示对象.数组.字符串.数值.布尔值和null,不支持undefined JSON.stringify() // JSON.stringi ...

  8. JavaScript 函数工具

    组 all:布尔全等判断 const all = (arr, fn = Boolean) => arr.every(fn); all([4, 2, 3], x => x > 1); ...

  9. SpringCloudConfig对称加密 yml配置文件while parsing a block mapping

    错误的配置!!! #-----------------db------------------ mybatis: type-aliases-package: com.book.product.pojo ...

  10. stdc++.6.0.9动态库缺失

    问题 ld: library not found for -lstdc++.6.0.9 clang: error: linker command failed with exit code 1 (us ...