HTTP连接池
- <context:property-placeholder location="classpath:conf/framework/httpclient.properties"/>
- <!-- 定义连接管理器 -->
- <bean id="httpClientConnectionManager"
- class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager"
- destroy-method="close">
- <!-- 最大连接数 -->
- <property name="maxTotal" value="${http.maxTotal}" />
- <!-- 设置每个主机地址的并发数 -->
- <property name="defaultMaxPerRoute" value="${http.defaultMaxPerRoute}" />
- </bean>
- <!-- httpclient对象构建器 -->
- <bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder">
- <!-- 设置连接管理器 -->
- <property name="connectionManager" ref="httpClientConnectionManager" />
- </bean>
- <!-- 定义Httpclient对象 -->
- <bean id="httpClient" class="org.apache.http.impl.client.CloseableHttpClient"
- factory-bean="httpClientBuilder" factory-method="build" scope="prototype">
- </bean>
- <!-- 定义清理无效连接 -->
- <bean class="com.avcon.platform.dledc.res.service.IdleConnectionEvictor"
- destroy-method="shutdown">
- <constructor-arg index="0" ref="httpClientConnectionManager" />
- </bean>
- <bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig.Builder">
- <!-- 创建连接的最长时间 -->
- <property name="connectTimeout" value="${http.connectTimeout}"/>
- <!-- 从连接池中获取到连接的最长时间 -->
- <property name="connectionRequestTimeout" value="${http.connectionRequestTimeout}"/>
- <!-- 数据传输的最长时间 -->
- <property name="socketTimeout" value="${http.socketTimeout}"/>
- <!-- 提交请求前测试连接是否可用 -->
- <property name="staleConnectionCheckEnabled" value="${http.staleConnectionCheckEnabled}"/>
- </bean>
- <!-- 定义请求参数 -->
- <bean id="requestConfig" class="org.apache.http.client.config.RequestConfig" factory-bean="requestConfigBuilder" factory-method="build">
- </bean>
- @Service
- public class HttpClientService {
- @Autowired
- private CloseableHttpClient httpClient;
- @Autowired
- private RequestConfig requestConfig;
- /**
- * 执行GET请求
- *
- * @param url
- * @return
- * @throws IOException
- * @throws ClientProtocolException
- */
- public String doGet(String url) throws ClientProtocolException, IOException {
- // 创建http GET请求
- HttpGet httpGet = new HttpGet(url);
- httpGet.setConfig(this.requestConfig);
- CloseableHttpResponse response = null;
- try {
- // 执行请求
- response = httpClient.execute(httpGet);
- // 判断返回状态是否为200
- if (response.getStatusLine().getStatusCode() == 200) {
- return EntityUtils.toString(response.getEntity(), "UTF-8");
- }
- } finally {
- if (response != null) {
- response.close();
- }
- }
- return null;
- }
- /**
- * 带有参数的GET请求
- *
- * @param url
- * @param params
- * @return
- * @throws URISyntaxException
- * @throws IOException
- * @throws ClientProtocolException
- */
- public String doGet(String url, Map<String, String> params)
- throws ClientProtocolException, IOException, URISyntaxException {
- URIBuilder uriBuilder = new URIBuilder(url);
- for (String key : params.keySet()) {
- uriBuilder.addParameter(key, params.get(key));
- }
- return this.doGet(uriBuilder.build().toString());
- }
- /**
- * 执行POST请求
- *
- * @param url
- * @param params
- * @return
- * @throws IOException
- */
- public HttpResult doPost(String url, Map<String, String> params) throws IOException {
- // 创建http POST请求
- HttpPost httpPost = new HttpPost(url);
- httpPost.setConfig(this.requestConfig);
- if (params != null) {
- // 设置2个post参数,一个是scope、一个是q
- List<NameValuePair> parameters = new ArrayList<NameValuePair>();
- for (String key : params.keySet()) {
- parameters.add(new BasicNameValuePair(key, params.get(key)));
- }
- // 构造一个form表单式的实体
- UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
- // 将请求实体设置到httpPost对象中
- httpPost.setEntity(formEntity);
- }
- CloseableHttpResponse response = null;
- try {
- // 执行请求
- response = httpClient.execute(httpPost);
- return new HttpResult(response.getStatusLine().getStatusCode(),
- EntityUtils.toString(response.getEntity(), "UTF-8"));
- } finally {
- if (response != null) {
- response.close();
- }
- }
- }
- /**
- * 执行POST请求
- *
- * @param url
- * @return
- * @throws IOException
- */
- public HttpResult doPost(String url) throws IOException {
- return this.doPost(url, null);
- }
- /**
- * 提交json数据
- *
- * @param url
- * @param json
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- */
- public HttpResult doPostJson(String url, String json) throws ClientProtocolException, IOException {
- // 创建http POST请求
- HttpPost httpPost = new HttpPost(url);
- httpPost.setConfig(this.requestConfig);
- if (json != null) {
- // 构造一个form表单式的实体
- StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
- // 将请求实体设置到httpPost对象中
- httpPost.setEntity(stringEntity);
- }
- CloseableHttpResponse response = null;
- try {
- // 执行请求
- response = this.httpClient.execute(httpPost);
- return new HttpResult(response.getStatusLine().getStatusCode(),
- EntityUtils.toString(response.getEntity(), "UTF-8"));
- } finally {
- if (response != null) {
- response.close();
- }
- }
- }
HTTP连接池的更多相关文章
- 连接SQLServer时,因启用连接池导致孤立事务的原因分析和解决办法
本文出处:http://www.cnblogs.com/wy123/p/6110349.html 之前遇到过这么一种情况: 连接数据库的部分Session会出现不定时的阻塞,这种阻塞时长时短,有时候持 ...
- C3p0连接池配置
在Java开发中,使用JDBC操作数据库的四个步骤如下: ①加载数据库驱动程序(Class.forName("数据库驱动类");) ②连接数据库(Connection co ...
- Java第三方数据库连接池库-DBCP-C3P0-Tomcat内置连接池
连接池原理 数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”.预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去.我们可以通过设定连接池 ...
- common-pool2 学习:thrift连接池的另一种实现
对象池是一种很实用的技术,经典的例子就是数据库连接池.去年曾经从零开始写过一个thrift客户端连接池.如果不想重造轮子,可以直接在apache开源项目commons-pool的基础上开发. 步骤: ...
- druid连接池获取不到连接的一种情况
数据源一开始配置: jdbc.initialSize=1jdbc.minIdle=1jdbc.maxActive=5 程序运行一段时间后,执行查询抛如下异常: exception=org.mybati ...
- C3P0连接池配置和实现详解
一.配置 <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> ...
- hibernate+mysql的连接池配置
1:连接池的必知概念 首先,我们还是老套的讲讲连接池的基本概念,概念理解清楚了,我们也知道后面是怎么回事了. 以前我们程序连接数据库的时候,每一次连接数据库都要一个连接,用完后再释放.如果频繁的 ...
- 连接池的实现 redis例子
# -*- encoding:utf-8 -*- # import pymysql # # conn = pymysql.connect(host="127.0.0.1", por ...
- DBCP连接池配置示例
<bean id="dataSourceOracle2" class="org.apache.commons.dbcp.BasicDataSource" ...
- 解决Mysql连接池被关闭 ,hibernate尝试连接不能连接的问题。 (默认mysql连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池。系统发布第二天访问链接关闭问题。
解决Mysql连接池被关闭 ,hibernate尝试连接不能连接的问题. (默认MySQL连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池. 所以系统发布第二天访问会 ...
随机推荐
- 【bzoj 3779】重组病毒
Description 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病毒. ...
- 内容分享-迅为IMX6开发板编译问题及解决方法
[经验分享]IMX6开发板编译问题及解决方法本文转自迅为IMX6开发板售后讨论群,分享给大家~ 物理主机 win10 64 位专业版.虚拟机 VM12 Pro.开发环境采用迅为提供的开发环境: Ubu ...
- border-radius图解
自己看了理解的border-radius: 设置参数: 100*100的正方形,第一个:border-top-left-radius:100px 100px,即圆的半径为100px.圆弧与上和左bor ...
- Django REST Framework API Guide 05
本节大纲 1.Serializer fields 2.Serializer relations Serializer fields 1.serializer 字段定义在fields.py文件内 2.导 ...
- 如何发布自己的 jar 包到 maven 中央仓库(待更新...)
参考链接 如何发布自己的 jar 包到 maven 中央仓库
- 【Git】Git常用命令
git remote -v : 查看远程仓库地址
- Web三层-UI/BLL/DAL/MODEL
2013传智播客视频\视频\2013-05-28-EF\视频 创建4个程序集,添加引用,model添加映射, P01UI表现层--BLL+MODELP02BLL业务层--DAL+MODELP03DAL ...
- JDK中Concurrent包介绍及使用(包含atomic包/lock包/并发容器/执行器)
Java Concurrent并发包概括 https://blog.csdn.net/u012232736/article/details/79919450 Java中的Atomic包使用指南 ...
- UVA315 Network 连通图割点
题目大意:有向图求割点 题目思路: 一个点u为割点时当且仅当满足两个两个条件之一: 1.该点为根节点且至少有两个子节点 2.u不为树根,且满足存在(u,v)为树枝边(或称 父子边,即u为v在搜索树中的 ...
- 生产者消费者模型java
马士兵老师的生产者消费者模型,我感觉理解了生产者消费者模型,基本懂了一半多线程. public class ProducerConsumer { public static void main(Str ...