简体   繁体   English

DefaultHttpClient 的单例实例超时

[英]Timeouts with singleton instance of DefaultHttpClient

I'm using a singleton instance of DefaultHttpClient in conjunction with PoolingClientConnectionManager to perform a cconsiderable number (multiple per second) of HTTP operations in a multithreaded fashion.我将DefaultHttpClient单例实例与PoolingClientConnectionManager结合使用,以多线程方式执行相当数量(每秒多个)的 HTTP 操作。 My code is basically:我的代码基本上是:

final HttpParams httpClientParams = new BasicHttpParams();
httpClientParams.setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(httpClientParams, 700);
HttpConnectionParams.setSoTimeout(httpClientParams, 700);
DefaultHttpClient client = new DefaultHttpClient(poolingClientConnectionManager, httpClientParams);

I will then access the client object from multiple threads in parallel.然后我将从多个线程并行访问client对象。

My problem is, that the timeouts do not get respected and I have HTTP requests that take much longer than the 700ms specified.我的问题是,超时没有得到尊重,并且我的 HTTP 请求需要比指定的 700 毫秒长得多的时间。

  • Is it safe to set the timeout on the client as I do above?像我上面那样在客户端上设置超时是否安全?
  • Is it safe to use a singleton instance of DefaultHttpClient for many parallel requests?许多并行请求使用DefaultHttpClient的单例实例是否安全?
  • Is it safe to set the timeout on the client as I do above?像我上面那样在客户端上设置超时是否安全?

Yes, it is safe.是的,这是安全的。 However, HttpClient level parameters represent the default settings inherited by all requests.但是,HttpClient 级别的参数代表所有请求继承的默认设置。 One should not meddle with those settings at runtime.不应在运行时干预这些设置。 It is generally better to use request level parameters to configure individual requests.通常最好使用请求级别参数来配置单个请求。

  • Is it safe to use a singleton instance of DefaultHttpClient for many parallel requests?对许多并行请求使用 DefaultHttpClient 的单例实例是否安全?

Not only safe but also strongly advisable.不仅安全,而且强烈建议。 By sharing the same HttpClient instance individual requests can be executed more efficiently by re-using a shared pool of persistent connections.通过共享相同的 HttpClient 实例,可以通过重用共享的持久连接池来更有效地执行单个请求。

Please also make sure your expectations with regards to timeout behavior are correct.还请确保您对超时行为的期望是正确的。 Timeout values represent the maximum period of inactivity between two consecutive i/o operations, not the maximum total request execution time.超时值表示两个连续i/o 操作之间的最长不活动时间,而不是最大总请求执行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM