多线程httpClient

发布于 2024-12-15 19:13:57 字数 4606 浏览 1 评论 0原文

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(它工作得很好),但是当我向其中添加多个线程时,它会抛出非法状态异常。

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();
        }

    }


}

While running this code, I get the following exception:

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

When I execute http request, then I see these Exceptions. It works perfectly fine for single-threaded. I am trying to call a specific URL (which works perfectly fine) but when I add more than one thread to it, it throws an illegal state exception.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深巷少女 2024-12-22 19:13:57

来这里是想说,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);

Came here to say, MultiThreadedHttpConnectionManager is outdated. Currently (HttpClient version 4.*) this is the way: 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);
雨夜星沙 2024-12-22 19:13:57

您需要定义多线程 HttpConnectionManager 例如

MultiThreadedHttpConnectionManager connectionManager = 
                                  new MultiThreadedHttpConnectionManager();
HttpClient client = new HttpClient(connectionManager);

有关更多详细信息,您可以参见 http://hc.apache.org/ httpclient-3.x/threading.html

You need define multi-threaded HttpConnectionManager e.g.

MultiThreadedHttpConnectionManager connectionManager = 
                                  new MultiThreadedHttpConnectionManager();
HttpClient client = new HttpClient(connectionManager);

For more details you can see http://hc.apache.org/httpclient-3.x/threading.html

孤君无依 2024-12-22 19:13:57

我意识到自己的愚蠢! httpClient 和 httpRequest 都是静态的。在我将它们设为非静态后,它工作得很好! Executor Service 使我能够更好地控制线程管理,并且我热衷于使用它。

I realised my folly! httpClient and httpRequest were both static. After i made them non-static it works fine! Executor Service gives me better control on managing threads and I was keen on using it.

深空失忆 2024-12-22 19:13:57

除了 bpgergo 答案之外 - 连接管理器已再次更新(从 HttpClient 版本 >= 4.3 开始),现在您应该使用 PoolingHttpClientConnectionManager 代替。 PoolingHttpClientConnectionManager 的默认限制是总共 20 个连接,每个路由 2 个 - 但这些限制可以被覆盖。

PoolingHttpClientConnectionManager cm=new PoolingHttpClientConnectionManager();
cm.setDefaultMaxPerRoute(40);
cm.setMaxTotal(500);
CloseableHttpClient client = HttpClients.createMinimal(cm);

In addition to bpgergo answer's - the Connection manager has updated yet again (as of HttpClient version >= 4.3), and now you should use PoolingHttpClientConnectionManager instead. The default limitations of the PoolingHttpClientConnectionManager are total of 20 connections, and 2 per route - but those can be overridden.

PoolingHttpClientConnectionManager cm=new PoolingHttpClientConnectionManager();
cm.setDefaultMaxPerRoute(40);
cm.setMaxTotal(500);
CloseableHttpClient client = HttpClients.createMinimal(cm);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文