看到了一篇文章,是关于发送http请求时,响应时间过长的问题的解决。
侵删
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
/**
* 连接池基类
*
*/
public class BaseHttpClientTest {
protected static final int REQUEST_COUNT = 5;
protected static final String SEPERATOR = " ";
protected static final AtomicInteger NOW_COUNT = new AtomicInteger(0);
protected static final StringBuilder EVERY_REQ_COST = new StringBuilder(200);
/**
* 获取待运行的线程
*/
protected List<Thread> getRunThreads(Runnable runnable) {
List<Thread> tList = new ArrayList<Thread>(REQUEST_COUNT);
for (int i = 0; i < REQUEST_COUNT; i++) {
tList.add(new Thread(runnable));
}
return tList;
}
/**
* 启动所有线程
*/
protected void startUpAllThreads(List<Thread> tList) {
for (Thread t : tList) {
t.start();
// 这里需要加一点延迟,保证请求按顺序发出去
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
protected synchronized void addCost(long cost) {
EVERY_REQ_COST.append(cost);
EVERY_REQ_COST.append("ms");
EVERY_REQ_COST.append(SEPERATOR);
}
}
/**
* 使用连接池测试
*
*/
public class HttpclientWithPoolTest extends BaseHttpClientTest {
private CloseableHttpClient httpClient = null;
@Before
public void before() {
initHttpClient();
}
@Test
public void test() throws Exception {
startUpAllThreads(getRunThreads(new HttpThread()));
// 等待线程运行
for (;;);
}
private class HttpThread implements Runnable {
@Override
public void run() {
HttpGet httpGet = new HttpGet("https://www.baidu.com/");
// 长连接标识,不加也没事,HTTP1.1默认都是Connection: keep-alive的
httpGet.addHeader("Connection", "keep-alive");
long startTime = System.currentTimeMillis();
try {
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
addCost(System.currentTimeMillis() - startTime);
if (NOW_COUNT.incrementAndGet() == REQUEST_COUNT) {
System.out.println(EVERY_REQ_COST.toString());
}
}
}
}
private void initHttpClient() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
// 总连接池数量
connectionManager.setMaxTotal(1);
// 可为每个域名设置单独的连接池数量
connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost("www.baidu.com")), 1);
// setConnectTimeout表示设置建立连接的超时时间
// setConnectionRequestTimeout表示从连接池中拿连接的等待超时时间
// setSocketTimeout表示发出请求后等待对端应答的超时时间
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000).setConnectionRequestTimeout(2000)
.setSocketTimeout(3000).build();
// 重试处理器,StandardHttpRequestRetryHandler这个是官方提供的,看了下感觉比较挫,很多错误不能重试,可自己实现HttpRequestRetryHandler接口去做
HttpRequestRetryHandler retryHandler = new StandardHttpRequestRetryHandler();
httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig)
.setRetryHandler(retryHandler).build();
// 服务端假设关闭了连接,对客户端是不透明的,HttpClient为了缓解这一问题,在某个连接使用前会检测这个连接是否过时,如果过时则连接失效,但是这种做法会为每个请求
// 增加一定额外开销,因此有一个定时任务专门回收长时间不活动而被判定为失效的连接,可以某种程度上解决这个问题
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
// 关闭失效连接并从连接池中移除
connectionManager.closeExpiredConnections();
// 关闭30秒钟内不活动的连接并从连接池中移除,空闲时间从交还给连接管理器时开始
connectionManager.closeIdleConnections(20, TimeUnit.SECONDS);
} catch (Throwable t) {
t.printStackTrace();
}
}
}, 0 , 1000 * 5);
}
}