BasicClientConnManager的使用无效:仍然分配了连接 [英] Invalid use of BasicClientConnManager: connection still allocated

查看:2873
本文介绍了BasicClientConnManager的使用无效:仍然分配了连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用REST URL并尝试测量恢复响应所花费的时间。

I am making a call to REST URL and trying to measure how much time it is taking to get the response back.

我正在使用 DefaultHttpClient 用于从 REST URL 获取响应。

I am using DefaultHttpClient for that to get the response back from REST URL.

在我的在程序下面,每个线程都将在特定范围内工作。像每个线程将在 1 - 100 之间工作,第二个线程将在 101 - 200 之间工作。

In my below program , each thread will be working on a particular range. Like Each Thread will work between 1 - 100 and second thread will work between 101 - 200 etc.

在我的下面的代码中,这是第一次正常工作。但是这是第二次,它在第二次 httpclient.execute 上抛出异常作为 -

SO in my below code, for the first time it works fine. But for the second time, it is throwing exception on this line httpclient.execute for the second time as-

java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.

我在这里做错了吗? -

Is there anything wrong I am doing here?-

下面是我的代码 -

Below is my code-

class Task implements Runnable {

    private DefaultHttpClient httpclient = new DefaultHttpClient();
    private HttpGet httpGet;
    private HttpResponse response;

    @Override
    public void run() {

        try {

            httpGet = new HttpGet(
                    "http://localhost:8080/service/BEService/v1/get/USERID=10000/profile.ACCOUNT.SERVICE
            httpGet.getRequestLine();

            for (int userId = id; userId < id + noOfTasks; userId++) {

                    long start = System.nanoTime();

                    response = httpclient.execute(httpGet);

                    long end = System.nanoTime() - start;
                }
        } catch (Exception e) {
            LOG.error("Threw a Exception in " + getClass().getSimpleName(), e);
        }
    }
}

更新的代码: -

如果我这样做的话 -

If I am doing it something like this-

class Task implements Runnable {

    private DefaultHttpClient httpclient = new DefaultHttpClient();
    private HttpGet httpGet;
    private HttpResponse response;

    @Override
    public void run() {

        try {

            for (int userId = id; userId < id + noOfTasks; userId++) {

                httpGet = new HttpGet("http://localhost:8080/service/BEService/v1/get/USERID=10000/profile.ACCOUNT.SERVICE");
                httpGet.getRequestLine();

                long start = System.nanoTime();

                response = httpclient.execute(httpGet);

                long end = System.nanoTime() - start;

                HttpEntity entity = response.getEntity();
                EntityUtils.consume(entity);
                }
        } catch (Exception e) {
            LOG.error("Threw a Exception in " + getClass().getSimpleName(), e);
        }
    }
}

那么它是好还是不行?

推荐答案


我在这里做错了吗?

Is there anything wrong I am doing here?

是的。如文档中所述:


BasicClientConnectionManager是一个简单的连接管理器,
一次只维护一个连接。即使这个类是
线程安全的,它也应该仅由一个执行线程使用。
BasicClientConnectionManager将努力为后续具有相同路由的请求重用
连接。然而,如果持久连接的路由与连接请求的
不匹配,它将关闭现有连接并为给定的
路由重新打开它。如果连接已经分配了
,则抛出java.lang.IllegalStateException。

BasicClientConnectionManager is a simple connection manager that maintains only one connection at a time. Even though this class is thread-safe it ought to be used by one execution thread only. BasicClientConnectionManager will make an effort to reuse the connection for subsequent requests with the same route. It will, however, close the existing connection and re-open it for the given route, if the route of the persistent connection does not match that of the connection request. If the connection has been already been allocated, then java.lang.IllegalStateException is thrown.

默认情况下,HttpClient使用BasicClientConnectionManager。

BasicClientConnectionManager is used by HttpClient per default.

参见关于如何使用池连接管理器的多线程请求执行 可以跨多个线程处理请求。

See "Multithreaded request execution" on how to use a pooling connection manager that can handle requests across multiple threads.

这篇关于BasicClientConnManager的使用无效:仍然分配了连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆