gpt4 book ai didi

java - 多线程httpClient

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:04:20 26 4
gpt4 key购买 nike

public class test {
public static final int nThreads = 2;

public static void main(String[] args) throws ExecutionException, InterruptedException{
// Runnable myrunnable = new myRunnable();
ExecutorService execute = Executors.newFixedThreadPool(nThreads);

for (int i = 0; i < nThreads; ++i) {
execute.execute(new MyTask());
}

execute.awaitTermination(1000, TimeUnit.MILLISECONDS);

execute.shutdown();
}
}

class MyTask implements Runnable {
public static final int maxCalls = 10;
public static final int sleepMillis = 500;
private static HttpResponse response;
private static HttpClient httpclient;

public void run(){
int counter = 0;

while (true) {

if (counter >= maxCalls) {
break;
}
try {
Thread.currentThread().sleep(sleepMillis);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

execHttpRequest();

++counter;
}
}

private void execHttpRequest() {
httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("My URL");

try {

response = httpclient.execute(httpget);
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String output;
while((output=br.readLine())!=null){
System.out.println(Thread.currentThread().getName() +output);
}
br.close();

httpclient.getConnectionManager().shutdown();
//httpclient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{

httpclient.getConnectionManager().shutdown();
}

}


}

运行这段代码时,出现以下异常:

Exception in thread "pool-1-thread-1" java.lang.IllegalStateException: Invalid use of SingleClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.
at org.apache.http.impl.conn.SingleClientConnManager.getConnection(SingleClientConnManager.java:216)
at org.apache.http.impl.conn.SingleClientConnManager$1.getConnection(SingleClientConnManager.java:190)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at MyTask.execHttpRequest(test.java:72)
at MyTask.run(test.java:60)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
java.io.InterruptedIOException: Connection has been shut down
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:543)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at MyTask.execHttpRequest(test.java:72)
at MyTask.run(test.java:60)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: org.apache.http.impl.conn.ConnectionShutdownException
at org.apache.http.impl.conn.AbstractPooledConnAdapter.assertValid(AbstractPooledConnAdapter.java:86)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.getRoute(AbstractPooledConnAdapter.java:112)
at org.apache.http.impl.client.DefaultRequestDirector.establishRoute(DefaultRequestDirector.java:740)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:577)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
... 8 more

当我执行 http 请求时,我会看到这些异常。它对于单线程来说工作得很好。我试图调用一个特定的 URL(它工作得很好)但是当我向它添加多个线程时,它会抛出一个非法状态异常。

最佳答案

到这里来说,MultiThreadedHttpConnectionManager已经过时了。当前(HttpClient 版本 4.*)是这样的:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e639

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(cm);

关于java - 多线程httpClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8138128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com