配置 http 连接池:
# 在 spring-context.xml 配置文件中引入
<import resource="classpath:spring/spring-httpclient.xml" /> |
# 连接池配置文件 spring-httpclient.xml
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> | |
<!-- 配置一个 httpClient 连接池 --> | |
<bean id="poolHttpClientConnectionManager" | |
class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager"> | |
<!-- 最大链接数 --> | |
<property name="maxTotal" value="200"/> | |
<!-- 设置每个主机地址的并发数 --> | |
<property name="defaultMaxPerRoute" value="20"/> | |
</bean> | |
<!-- 生成 httpClientBuilder (用于得到 httpClient) --> | |
<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"> | |
<!-- 注入连接池 --> | |
<property name="connectionManager" ref="poolingHttpClientConnectionManager"/> | |
</bean> | |
<!-- 配置 httpClient (通过 httpClientBuilder 得到 httpClient 对象,并且要设置 httpClient 为多利模式) --> | |
<bean class="org.apache.http.impl.client.CloseableHttpClient" | |
factory-bean="httpClientBuilder" factory-method="build" scope="prototype"></bean> | |
<!-- 构造 (配置) 请求参数 --> | |
<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig.Builder"> | |
<!-- 创建连接的最长时间 --> | |
<property name="connectTimeout" value="1000"/> | |
<!-- 从连接池中获取到连接的最长时间 --> | |
<property name="socketTimeout" value="10000"/> | |
<!-- 提交请求前测试连接是否可用 --> | |
<property name="connectionRequestTimeout" value="500"/> | |
</bean> | |
<bean class="org.apache.http.client.config.RequestConfig" | |
factory-bean="requestConfigBuilder" factory-method="build"></bean> | |
<!-- 定期清理无效的连接 --> | |
<bean class="com.a.b.common.utils.http.IdleConnectionEvictor" destroy-method="shutdown"> | |
<constructor-arg index="0" ref="poolingHttpClientConnectionManager" /> | |
<!-- 间隔一分钟清理一次 --> | |
<constructor-arg index="1" value="60000" /> | |
</bean> | |
</beans> |
# 类 IdleConnectionEvictor
public class IdleConnectionEvictor extends Thread{ | |
private final HttpClientConnectionManager connMgr; | |
private Integer waitTime; | |
private volatile boolean shutdown; | |
private static final Logger logger = Logger.getLogger(HttpPoolUtil.class); | |
public IdleConnectionEvictor(HttpClientConnectionManager connMgr, Integer waitTime) { | |
this.connMgr = connMgr; | |
this.waitTime = waitTime; | |
this.start(); | |
} | |
@Override | |
public void run() { | |
try { | |
while (!shutdown) { | |
synchronized (this) { | |
wait(waitTime); | |
// 关闭失效的连接 | |
connMgr.closeExpiredConnections(); | |
} | |
} | |
} catch (InterruptedException ex) { | |
logger.error("清理异常"); | |
} | |
} | |
/** | |
* 销毁释放资源 | |
*/ | |
public void shutdown() { | |
shutdown = true; | |
synchronized (this) { | |
notifyAll(); | |
} | |
} | |
} |