带有Httpclient 4.0.3的多个帖子随机挂起 [英] Multiple post with Httpclient 4.0.3 hanging randomly

查看:75
本文介绍了带有Httpclient 4.0.3的多个帖子随机挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我解释一下情况.

我有一个servlet,它将传出的GET/POST重定向到另一个域(某种代理)上的另一个项目,该域的工作是处理它并返回一些内容(参数和gif).我正在使用HttpClient 4.0.3来做到这一点.

I have a servlet redirecting outgoing GET/POST to another project on another domain (some kind of proxy) whose job is to handle it and return some stuff (params and a gif). Im using HttpClient 4.0.3 to do this.

我的应用在启动时发送了多个GET/POST,因此我将ThreadSafeClientConnManager设置为一次,以这种方式处理多个线程.

There are multiple GET/POST sent by my app on startup, so I setup once a ThreadSafeClientConnManager to handle multiple threads this way.

cm_params = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(cm_params, 200);

ConnPerRouteBean connPerRoute = new ConnPerRouteBean();
HttpHost localhost = new HttpHost("localhost");
connPerRoute.setMaxForRoute(new HttpRoute(localhost), 50);

ConnManagerParams.setMaxConnectionsPerRoute(cm_params, connPerRoute);

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

cm = new ThreadSafeClientConnManager(cm_params, schemeRegistry);

然后我用这些参数创建一个新的HttpClient,它应该足以同时处理一堆请求.当然,对于每个GET/POST,我都会在 public void service()上执行此操作,但是在创建它之后,我会使用相同的Httpclient对象.

Then I create a new HttpClient with those params, which should be enough to handle a bunch of request at the same time. Of course, I do this on public void service() for every GET/POST but I use the same Httpclient object after I created it.

httpclient = new DefaultHttpClient(cm, cm_params);

此后,我建立了POST并通过execute将其发送,并验证了所有必需的参数和三元组.

After that I build up my POST and send it via execute, with all the requiered params and such triple verified.

    HttpPost httpPost = new HttpPost(target+tmpString); 

    httpPost.setHeader("Host", request.getHeader("host"));
    httpPost.setHeader("User-Agent", request.getHeader("user-agent"));
    httpPost.setHeader("Accept-Encoding", request.getHeader("accept-encoding"));
    httpPost.setHeader("Accept", request.getHeader("accept"));
    ..etc..

    UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params);
    urlEncodedFormEntity.setContentEncoding(HTTP.UTF_8);
    httpPost.setEntity(urlEncodedFormEntity);

    HttpResponse response = httpclient.execute(httpPost);

最后,我阅读了流并处理了实体...

Finally I read the stream and handle the entities...

    OutputStream os = res.getOutputStream();
    InputStream is = response.getEntity().getContent();
    byte[] buf = new byte[1024];
    for(int n;(n=is.read(buf))!=-1;)
    {
        os.write(buf, 0, n);
    }
    // Make sure to close
    is.close();
    os.close();

    // Flush entities just in case
    EntityUtils.consume(urlEncodedFormEntity);
    EntityUtils.consume(response.getEntity());
    urlEncodedFormEntity.getContent().close();
    response.getEntity().getContent().close();

所以我的问题是,当我加载页面时,代码运行良好.正确处理了4个发送的请求(1个GET,3个POST).基本上返回一些参数和我在页面上打印的1个小gif.

So my problem is, the code is working perfectly well when I load my page. There are 4 request sent (1 GET, 3 POST) correctly handled. Basicly returning some params and 1 small gif that I print on my page.

但是,一旦我开始对我的应用进行压力测试,即在4-5个标签中加载同一页面时,每当我同时执行多个POST时,我的应用似乎就会随机挂起.我认为即使我使用相同的Httpclient对象,也不会有任何问题,因为我正确地声明了ThreadSafeClientConnManager(我认为?),因此它应该处理多个线程.

But as soon I start to Stress Test my app, I.E. loading the same page in 4-5 tabs, my app seems to hang randomly whenever I execute multiple POST at the same time. I thought I wouldn't have any problem even if I use the same Httpclient object since I declared my ThreadSafeClientConnManager correctly (I think?) so it should handle multiple thread.

有人知道我在做什么错吗?如果我按1加载我的标签页,它不会挂起.当我同时刷新多个标签时.

Anyone know what I am doing wrong? If I load my tabs 1 by 1, it doesnt hang. Just when I refresh more than 1 tab at the same time.

有人知道吗? :S(抱歉,我的母语不是英语^^;)

Anyone have a clue? :S (sry english isnt my first language ^^;)

推荐答案

@Apache Fan,设置如下:

@Apache Fan, setting this :

ConnManagerParams.setMaxTotalConnections(cm_params,200); connPerRoute.setDefaultMaxPerRoute(50);

ConnManagerParams.setMaxTotalConnections(cm_params, 200); connPerRoute.setDefaultMaxPerRoute(50);

怀疑我无法连接,我打开3-4选项卡,它用完了...每个选项卡可能有4个POST,所以不会加起来:S

Doubt I can run out of connection, I open 3-4 tab and it runs out... theres maybe 4 POST per tab so that doesnt add up :S

这篇关于带有Httpclient 4.0.3的多个帖子随机挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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