<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连接池的更多相关文章

  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. JAVA正确地自定义比较对象---如何重写equals方法和hashCode方法

    在实际应用中经常会比较两个对象是否相等,比如下面的Address类,它有两个属性:String province 和 String city. public class Address { priva ...

  2. Sqlserver脚本创建登录名密码

    use table1 GO ', default_database=table1; GO create user abc for login abc with default_schema=dbo; ...

  3. Windows 查看某个端口号是否被占用

    Ø  前言 在 Windows 下很多系统或服务都需要使用独立的端口号,实现网络数据传输,如果需要知道某个端口号是否被占用,就可以使用下面步骤了. 1.   首先打开命令窗口,Windows + R ...

  4. Javaweb学习笔记——(十七)——————JDBC的原理、四大核心类、四大参数、预编译、Dao模式、批处理、大数据、时间类型的转换

    JDBC入门 *导入jar包:驱动 *加载驱动类:Class.forName("类名"); *给出url.username.password,其中url背下来 *使用DriverM ...

  5. 【游记】THUWC2018踹线记

    Day1. 早上九点多报道,然后就是试机.一开始有一些懵,没看清门外的通知,操作起来各种懵逼.不过提前适应过了在Linux下面编程,所以问题不大.调了gedit的界面,试了一下对拍,敲了一道试机题,然 ...

  6. ue4 笔记

    关卡场景内的模型在运行中,改变构建脚本后无法立即更新模型骨骼中心点碰到物理边缘 physcX会出问题 bug 纹理启用碰撞会与碰撞顶点冲突 造成效果不正确 IsInGameThread() 渲染时 会 ...

  7. CPU缓存一致性协议—MESI详解

    MESI(也称伊利诺斯协议)是一种广泛使用的支持写回策略的缓存一致性协议,该协议被应用在Intel奔腾系列的CPU中. MESI协议中的状态 CPU中每个缓存行使用的4种状态进行标记(使用额外的两位b ...

  8. [ASP.net教程]IIS服务器 远程发布(Web Deploy)配置

    asp.net 网站有三种常用的发布方式:分别是拷贝开发机上发布好的文件到服务器硬盘上.打包成安装程序到服务器上安装.通过Visual Studio 开发平台远程发布到服务器. 在asp.net网站的 ...

  9. 【多线程补充】SimpleDateFormat非线程安全与线程中、线程组中异常的处理

    1.SimpleDateFormat非线程安全的问题 类SimpleDateFormat主要负责日期的转换与格式化,但在多线程环境中,使用此类容易造成数据转换及处理的不正确,因为SimpleDateF ...

  10. pytorch1.0 用torch script导出模型

    python的易上手和pytorch的动态图特性,使得pytorch在学术研究中越来越受欢迎,但在生产环境,碍于python的GIL等特性,可能达不到高并发.低延迟的要求,存在需要用c++接口的情况. ...