0
我正在爲httpclient的apache-httpclient編寫測試示例來測試多線程請求錯誤。HttpClient 4.5.x多線程請求錯誤:尚未從此池中租用
我在for循環中運行它,並在每個循環中創建2個線程以運行2個不同主機的測試用例設置爲共用管理器。
在10個循環中,我總是遇到1個錯誤: 線程「線程-2」中的異常java.lang.IllegalStateException:Entry [id:17] [route:{} - > http:// *** :8000] [state:null]尚未從此池租用 at org.apache.http.util.Asserts.check(Asserts.java:46)
我的代碼是否有任何問題?我該怎麼辦?
樣品低於:
private void testHttpClient() {
HttpHost proxy = new HttpHost("dev.host.com", 8001);
final DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(20);
cm.setDefaultMaxPerRoute(1);
HttpHost localhost1 = new HttpHost("dev.test1.com", 8001);
cm.setMaxPerRoute(new HttpRoute(localhost1), 2);
HttpHost localhost2 = new HttpHost("dev.test2.com", 8000);
cm.setMaxPerRoute(new HttpRoute(localhost2), 2);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(1000)
.setSocketTimeout(1000)
.setConnectionRequestTimeout(1000)
.build();
final CloseableHttpClient httpclient1 = HttpClients.custom()
.setConnectionManager(cm)
.setRoutePlanner(routePlanner)
.setDefaultRequestConfig(requestConfig)
.build();
RequestConfig requestConfig2 = RequestConfig.custom()
.setConnectTimeout(1000)
.setSocketTimeout(1000)
.setConnectionRequestTimeout(1000)
.build();
final CloseableHttpClient httpclient2 = HttpClients.custom()
.setConnectionManager(cm)
.setRoutePlanner(routePlanner)
.setDefaultRequestConfig(requestConfig2)
.build();
class HttpClientThead1 implements Runnable {
public void run() {
for (int i = 1; i <= 1; i++) {
System.out.println("HttpClientThead1 Start");
HttpClientContext context = HttpClientContext.create();
try {
HttpGet httpget = new HttpGet("http://dev.test1.com:8001/test/id/10001");
CloseableHttpResponse response = httpclient1.execute(httpget, context);
long t = System.currentTimeMillis();
System.out.println("HttpClientThead1 " + i + ":" + response.getEntity().toString() + " " + t);
response.close();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
cm.closeExpiredConnections();
cm.closeIdleConnections(30, TimeUnit.SECONDS);
}
}
}
}
class HttpClientThead2 implements Runnable {
public void run() {
for (int i = 1; i <= 1; i++) {
System.out.println("HttpClientThead2 Start");
HttpClientContext context = HttpClientContext.create();
try {
HttpGet httpget2 = new HttpGet("http://dev.test2.com:8000/test/id/10002");
CloseableHttpResponse response2 = httpclient2.execute(httpget2, context);
long t = System.currentTimeMillis();
System.out.println("HttpClientThead2 " + i + ":" + response2.getEntity().toString() + " " + t);
response2.close();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
cm.closeExpiredConnections();
cm.closeIdleConnections(30, TimeUnit.SECONDS);
}
}
}
}
for (int j = 1; j <= 10; j++) {
HttpClientThead1 t1 = new HttpClientThead1();
HttpClientThead2 t2 = new HttpClientThead2();
Thread thread1 = new Thread(t1);
Thread thread2 = new Thread(t2);
thread1.start();
thread2.start();
}
}
我有檢查它,似乎我的主機設置不起作用,如果我改變了cm.setDefaultMaxPerRoute(10); 那麼它將會是oK。爲什麼? –