Skip to content

Instantly share code, notes, and snippets.

@searover
Created September 23, 2015 16:35
Show Gist options
  • Save searover/089792f878ad8ea08d00 to your computer and use it in GitHub Desktop.
Save searover/089792f878ad8ea08d00 to your computer and use it in GitHub Desktop.
package com.searover.hctest;
import org.apache.commons.lang.time.StopWatch;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
/**
* Created by searover on 9/19/15.
*/
public class ClientMultiThreadedExecution {
public static void main(String[] args) throws Exception {
// 创建连接池管理器
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// 设置最大并发连接数
cm.setMaxTotal(100);
// 设置单路由最大连接数
cm.setDefaultMaxPerRoute(20);
// 计时器,记录代码执行时长
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// *** test-1 *** 测试使用单一实例进行并发连接请求
CloseableHttpClient httpClient = HttpClients.createDefault();
//*** test-3 *** 测试使用连接池进行迸发连接请求
// CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
try {
String url = "http://jobserver:4242";
GetThread[] threads = new GetThread[2000];
for (int i = 0; i < threads.length; i++) {
HttpGet httpGet = new HttpGet(url);
//*** test-2 *** 测试使用多实例进行并发连接请求
// CloseableHttpClient httpClient = HttpClients.createDefault();
threads[i] = new GetThread(httpClient, httpGet, i + 1);
}
// Start the threads
for (int j = 0; j < threads.length; j++) {
threads[j].start();
}
// Join the threads
for (int k = 0; k < threads.length; k++) {
threads[k].join();
}
} finally {
httpClient.close();
}
stopWatch.stop();
System.out.println("============== End ================");
System.out.println("Total requests cost: " + stopWatch.getTime() + " ms");
}
/**
* A thread that performs a GET
*/
static class GetThread extends Thread {
private final CloseableHttpClient httpClient;
private final HttpContext context;
private final HttpGet httpGet;
private final int id;
public GetThread(CloseableHttpClient httpClient, HttpGet httpGet, int id) {
this.httpClient = httpClient;
this.context = new BasicHttpContext();
this.httpGet = httpGet;
this.id = id;
}
public void run() {
try {
System.out.println(id + " - about to get something from " + httpGet.getURI());
CloseableHttpResponse response = httpClient.execute(httpGet, context);
try {
System.out.println(id + " - get executed");
HttpEntity entity = response.getEntity();
if (entity != null) {
byte[] bytes = EntityUtils.toByteArray(entity);
System.out.println(id + " - " + bytes.length + " bytes read");
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment