1.      <context:property-placeholder location="classpath:conf/framework/httpclient.properties"/>
  2. <!-- 定义连接管理器 -->
  3. <bean id="httpClientConnectionManager"
  4. class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager"
  5. destroy-method="close">
  6. <!-- 最大连接数 -->
  7. <property name="maxTotal" value="${http.maxTotal}" />
  8. <!-- 设置每个主机地址的并发数 -->
  9. <property name="defaultMaxPerRoute" value="${http.defaultMaxPerRoute}" />
  10. </bean>
  11. <!-- httpclient对象构建器 -->
  12. <bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder">
  13. <!-- 设置连接管理器 -->
  14. <property name="connectionManager" ref="httpClientConnectionManager" />
  15. </bean>
  16.  
  17. <!-- 定义Httpclient对象 -->
  18. <bean id="httpClient" class="org.apache.http.impl.client.CloseableHttpClient"
  19. factory-bean="httpClientBuilder" factory-method="build" scope="prototype">
  20. </bean>
  21.  
  22. <!-- 定义清理无效连接 -->
  23. <bean class="com.avcon.platform.dledc.res.service.IdleConnectionEvictor"
  24. destroy-method="shutdown">
  25. <constructor-arg index="0" ref="httpClientConnectionManager" />
  26. </bean>
  27.  
  28. <bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig.Builder">
  29. <!-- 创建连接的最长时间 -->
  30. <property name="connectTimeout" value="${http.connectTimeout}"/>
  31. <!-- 从连接池中获取到连接的最长时间 -->
  32. <property name="connectionRequestTimeout" value="${http.connectionRequestTimeout}"/>
  33. <!-- 数据传输的最长时间 -->
  34. <property name="socketTimeout" value="${http.socketTimeout}"/>
  35. <!-- 提交请求前测试连接是否可用 -->
  36. <property name="staleConnectionCheckEnabled" value="${http.staleConnectionCheckEnabled}"/>
  37. </bean>
  38. <!-- 定义请求参数 -->
  39. <bean id="requestConfig" class="org.apache.http.client.config.RequestConfig" factory-bean="requestConfigBuilder" factory-method="build">
  40. </bean>

  

  1. @Service
  2. public class HttpClientService {
  3.  
  4. @Autowired
  5. private CloseableHttpClient httpClient;
  6.  
  7. @Autowired
  8. private RequestConfig requestConfig;
  9.  
  10. /**
  11. * 执行GET请求
  12. *
  13. * @param url
  14. * @return
  15. * @throws IOException
  16. * @throws ClientProtocolException
  17. */
  18. public String doGet(String url) throws ClientProtocolException, IOException {
  19. // 创建http GET请求
  20. HttpGet httpGet = new HttpGet(url);
  21. httpGet.setConfig(this.requestConfig);
  22.  
  23. CloseableHttpResponse response = null;
  24. try {
  25. // 执行请求
  26. response = httpClient.execute(httpGet);
  27. // 判断返回状态是否为200
  28. if (response.getStatusLine().getStatusCode() == 200) {
  29. return EntityUtils.toString(response.getEntity(), "UTF-8");
  30. }
  31. } finally {
  32. if (response != null) {
  33. response.close();
  34. }
  35. }
  36. return null;
  37. }
  38.  
  39. /**
  40. * 带有参数的GET请求
  41. *
  42. * @param url
  43. * @param params
  44. * @return
  45. * @throws URISyntaxException
  46. * @throws IOException
  47. * @throws ClientProtocolException
  48. */
  49. public String doGet(String url, Map<String, String> params)
  50. throws ClientProtocolException, IOException, URISyntaxException {
  51. URIBuilder uriBuilder = new URIBuilder(url);
  52. for (String key : params.keySet()) {
  53. uriBuilder.addParameter(key, params.get(key));
  54. }
  55. return this.doGet(uriBuilder.build().toString());
  56. }
  57.  
  58. /**
  59. * 执行POST请求
  60. *
  61. * @param url
  62. * @param params
  63. * @return
  64. * @throws IOException
  65. */
  66. public HttpResult doPost(String url, Map<String, String> params) throws IOException {
  67. // 创建http POST请求
  68. HttpPost httpPost = new HttpPost(url);
  69. httpPost.setConfig(this.requestConfig);
  70. if (params != null) {
  71. // 设置2个post参数,一个是scope、一个是q
  72. List<NameValuePair> parameters = new ArrayList<NameValuePair>();
  73. for (String key : params.keySet()) {
  74. parameters.add(new BasicNameValuePair(key, params.get(key)));
  75. }
  76. // 构造一个form表单式的实体
  77. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
  78. // 将请求实体设置到httpPost对象中
  79. httpPost.setEntity(formEntity);
  80. }
  81.  
  82. CloseableHttpResponse response = null;
  83. try {
  84. // 执行请求
  85. response = httpClient.execute(httpPost);
  86. return new HttpResult(response.getStatusLine().getStatusCode(),
  87. EntityUtils.toString(response.getEntity(), "UTF-8"));
  88. } finally {
  89. if (response != null) {
  90. response.close();
  91. }
  92. }
  93. }
  94.  
  95. /**
  96. * 执行POST请求
  97. *
  98. * @param url
  99. * @return
  100. * @throws IOException
  101. */
  102. public HttpResult doPost(String url) throws IOException {
  103. return this.doPost(url, null);
  104. }
  105.  
  106. /**
  107. * 提交json数据
  108. *
  109. * @param url
  110. * @param json
  111. * @return
  112. * @throws ClientProtocolException
  113. * @throws IOException
  114. */
  115. public HttpResult doPostJson(String url, String json) throws ClientProtocolException, IOException {
  116. // 创建http POST请求
  117. HttpPost httpPost = new HttpPost(url);
  118. httpPost.setConfig(this.requestConfig);
  119.  
  120. if (json != null) {
  121. // 构造一个form表单式的实体
  122. StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
  123. // 将请求实体设置到httpPost对象中
  124. httpPost.setEntity(stringEntity);
  125. }
  126.  
  127. CloseableHttpResponse response = null;
  128. try {
  129. // 执行请求
  130. response = this.httpClient.execute(httpPost);
  131. return new HttpResult(response.getStatusLine().getStatusCode(),
  132. EntityUtils.toString(response.getEntity(), "UTF-8"));
  133. } finally {
  134. if (response != null) {
  135. response.close();
  136. }
  137. }
  138. }

  

HTTP连接池的更多相关文章

  1. 连接SQLServer时,因启用连接池导致孤立事务的原因分析和解决办法

    本文出处:http://www.cnblogs.com/wy123/p/6110349.html 之前遇到过这么一种情况: 连接数据库的部分Session会出现不定时的阻塞,这种阻塞时长时短,有时候持 ...

  2. C3p0连接池配置

    在Java开发中,使用JDBC操作数据库的四个步骤如下:   ①加载数据库驱动程序(Class.forName("数据库驱动类");)   ②连接数据库(Connection co ...

  3. Java第三方数据库连接池库-DBCP-C3P0-Tomcat内置连接池

    连接池原理 数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”.预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去.我们可以通过设定连接池 ...

  4. common-pool2 学习:thrift连接池的另一种实现

    对象池是一种很实用的技术,经典的例子就是数据库连接池.去年曾经从零开始写过一个thrift客户端连接池.如果不想重造轮子,可以直接在apache开源项目commons-pool的基础上开发. 步骤: ...

  5. druid连接池获取不到连接的一种情况

    数据源一开始配置: jdbc.initialSize=1jdbc.minIdle=1jdbc.maxActive=5 程序运行一段时间后,执行查询抛如下异常: exception=org.mybati ...

  6. C3P0连接池配置和实现详解

    一.配置 <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> ...

  7. hibernate+mysql的连接池配置

    1:连接池的必知概念    首先,我们还是老套的讲讲连接池的基本概念,概念理解清楚了,我们也知道后面是怎么回事了. 以前我们程序连接数据库的时候,每一次连接数据库都要一个连接,用完后再释放.如果频繁的 ...

  8. 连接池的实现 redis例子

    # -*- encoding:utf-8 -*- # import pymysql # # conn = pymysql.connect(host="127.0.0.1", por ...

  9. DBCP连接池配置示例

    <bean id="dataSourceOracle2" class="org.apache.commons.dbcp.BasicDataSource" ...

  10. 解决Mysql连接池被关闭 ,hibernate尝试连接不能连接的问题。 (默认mysql连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池。系统发布第二天访问链接关闭问题。

    解决Mysql连接池被关闭  ,hibernate尝试连接不能连接的问题. (默认MySQL连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池. 所以系统发布第二天访问会 ...

随机推荐

  1. 【bzoj 3779】重组病毒

    Description 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病毒. ...

  2. 内容分享-迅为IMX6开发板编译问题及解决方法

    [经验分享]IMX6开发板编译问题及解决方法本文转自迅为IMX6开发板售后讨论群,分享给大家~ 物理主机 win10 64 位专业版.虚拟机 VM12 Pro.开发环境采用迅为提供的开发环境: Ubu ...

  3. border-radius图解

    自己看了理解的border-radius: 设置参数: 100*100的正方形,第一个:border-top-left-radius:100px 100px,即圆的半径为100px.圆弧与上和左bor ...

  4. Django REST Framework API Guide 05

    本节大纲 1.Serializer fields 2.Serializer relations Serializer fields 1.serializer 字段定义在fields.py文件内 2.导 ...

  5. 如何发布自己的 jar 包到 maven 中央仓库(待更新...)

    参考链接 如何发布自己的 jar 包到 maven 中央仓库

  6. 【Git】Git常用命令

    git remote -v : 查看远程仓库地址

  7. Web三层-UI/BLL/DAL/MODEL

    2013传智播客视频\视频\2013-05-28-EF\视频 创建4个程序集,添加引用,model添加映射, P01UI表现层--BLL+MODELP02BLL业务层--DAL+MODELP03DAL ...

  8. JDK中Concurrent包介绍及使用(包含atomic包/lock包/并发容器/执行器)

    Java Concurrent并发包概括  https://blog.csdn.net/u012232736/article/details/79919450 Java中的Atomic包使用指南   ...

  9. UVA315 Network 连通图割点

    题目大意:有向图求割点 题目思路: 一个点u为割点时当且仅当满足两个两个条件之一: 1.该点为根节点且至少有两个子节点 2.u不为树根,且满足存在(u,v)为树枝边(或称 父子边,即u为v在搜索树中的 ...

  10. 生产者消费者模型java

    马士兵老师的生产者消费者模型,我感觉理解了生产者消费者模型,基本懂了一半多线程. public class ProducerConsumer { public static void main(Str ...