使用 MultiThreadedHttpConnectionManager 为同一个 httpclient 设置不同的套接字超时

发布于 12-18 06:09 字数 1705 浏览 5 评论 0原文

我有两个线程使用 HTTPClientTest 的同一实例。两个线程调用同一个 HTTPClientTest 上的 send 方法。我应该如何为调用发送方法的每个线程设置不同的套接字超时。如果我在发送方法中执行类似的操作,那么执行发送方法的两个线程将具有相同的套接字超时。
managerParams.setSoTimeout(60);connectionManager.setParams(managerParams);

我应该如何为在同一 HTTPClientTest 实例上执行 send 方法的多个线程创建不同的套接字超时。

public class HTTPClientTest implements Runnable{
private HttpClient httpClient;
private MultiThreadedHttpConnectionManager connectionManager;
private HttpConnectionManagerParams managerParams;
private HttpClientTest()
{
     connectionManager = new MultiThreadedHttpConnectionManager();
     httpClient = new HttpClient(connectionManager);
}
public static synchronized HTTPClientTest getInstance()
{
    if(instance == null)
        instance = new HTTPClientTest();
    return instance;
}

public void send(String message, String url)
{
    PostMethod post = new PostMethod(url);
    String reply = "";
    String length = message.length() + "";
    post.setRequestHeader("Content-Length", length);
    try 
    {
        System.out.println("HTTP request: " + message);
        StringRequestEntity postBody = new StringRequestEntity(message, "text/xml", "UTF-8");
        post.setRequestEntity(postBody);
        int status = httpClient.executeMethod(post);
        System.out.println("HTTP status: " + status);
        reply = post.getResponseBodyAsString();
        System.out.println("HTTP Post response code: " + reply);

    } 
    catch(HttpException e)
    {
        e.printStackTrace();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        post.releaseConnection();
    }

}
}

i have two threads using the same instance of the HTTPClientTest. two threads call the send method on the same HTTPClientTest. how should i set a different socket timeout for each of those threads that call the send method. if i do something like this within the send method then both threads executing the send method would have same socket timeout.
managerParams.setSoTimeout(60);connectionManager.setParams(managerParams);

how should i create a different socket timeout for multiple threads executing the send method on the same instance of HTTPClientTest.

public class HTTPClientTest implements Runnable{
private HttpClient httpClient;
private MultiThreadedHttpConnectionManager connectionManager;
private HttpConnectionManagerParams managerParams;
private HttpClientTest()
{
     connectionManager = new MultiThreadedHttpConnectionManager();
     httpClient = new HttpClient(connectionManager);
}
public static synchronized HTTPClientTest getInstance()
{
    if(instance == null)
        instance = new HTTPClientTest();
    return instance;
}

public void send(String message, String url)
{
    PostMethod post = new PostMethod(url);
    String reply = "";
    String length = message.length() + "";
    post.setRequestHeader("Content-Length", length);
    try 
    {
        System.out.println("HTTP request: " + message);
        StringRequestEntity postBody = new StringRequestEntity(message, "text/xml", "UTF-8");
        post.setRequestEntity(postBody);
        int status = httpClient.executeMethod(post);
        System.out.println("HTTP status: " + status);
        reply = post.getResponseBodyAsString();
        System.out.println("HTTP Post response code: " + reply);

    } 
    catch(HttpException e)
    {
        e.printStackTrace();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        post.releaseConnection();
    }

}
}

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

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

发布评论

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

评论(1

断爱2024-12-25 06:09:10

这很简单:

get.getParams().setParameter("http.socket.timeout",20000);
httpclient.execute(get);

这些方法不是由线程共享的,不是吗?
根据您的需要进行修改。

It's easy:

get.getParams().setParameter("http.socket.timeout",20000);
httpclient.execute(get);

The mothods aren't shared by the threads,are they?
Modify as what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文