hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Asankha C. Perera" <asan...@apache.org>
Subject Re: Setting different socket timeouts to connections obtained from a pool
Date Thu, 14 Feb 2013 17:24:22 GMT
On 02/14/2013 10:39 PM, Oleg Kalnichevski wrote:
> On Thu, 2013-02-14 at 22:24 +0530, Asankha C. Perera wrote:
>> On 02/14/2013 09:03 PM, Oleg Kalnichevski wrote:
>>> On Thu, 2013-02-14 at 16:11 +0530, Asankha C. Perera wrote:
>>>> Hi All
>>>>
>>>> I was trying to use a custom connection pool implementation extending
>>>> the BasicNIOConnPool for sending messages. My use case requires me to
>>>> use different socket timeouts for different requests. When I want to
>>>> send out messages, I do something like the following (code simplified
>>>> for illustration)
>>>>
>>>>            final BasicHttpContext coreContext = new BasicHttpContext();
>>>>            coreContext.setAttribute("timeout", 3000);
>>>>            asyncRequester.execute(requestProducer, responseConsumer,
>>>> connectionPool, coreContext);
>>>>
>>>>
>>>> My connection pool overrides the requestCompleted() method, and tries to
>>>> read the socket timeout I had set earlier
>>>>
>>>> public class UltraNIOConnPool extends BasicNIOConnPool {
>>>>        protected void requestCompleted(SessionRequest request) {
>>>>            super.requestCompleted(request);
>>>>            Integer timeout = (Integer)
>>>> request.getSession().getAttribute("timeout");
>>>>            if (timeout != null) {
>>>>                NHttpClientConnection conn = (NHttpClientConnection)
>>>> request.getSession().getAttribute(ExecutionContext.HTTP_CONNECTION);
>>>>                conn.setSocketTimeout(timeout);
>>>>            }
>>>>        }
>>>> }
>>>>
>>>> However, the values I put into the coreContext above is not available in
>>>> the session within the requestCompleted(). I need to set the socket
>>>> timeout before the new connection is used. I am quite sure I am
>>>> overlooking something simpler.. so any help is very much appreciated
>>>>
>>>> regards
>>>> asankha
>>>>
>>> Asankha
>>>
>>> A custom request interceptor should be a better place to do so. The
>>> request interceptor will give you access to the right execution context
>>> containing your custom attribute(s).
>>>
>>> Hope this helps
>>>
>>> Oleg
>> Hi Oleg
>>
>> Do you mean overriding the
>> HttpAsyncRequestExecutor.connected(NHttpClientConnection conn, Object
>> attachment) method? That too does not have the context set..
>>
>> regards
>> asankha
>>
> I was thinking about something like that:
>
> ---
> final HttpParams params = new BasicHttpParams();
> final HttpProcessor httpproc = new ImmutableHttpProcessor(new
> HttpRequestInterceptor[] {
>      new HttpRequestInterceptor() {
>
>          @Override
>          public void process(
>              final HttpRequest request,
>              final HttpContext context) throws HttpException, IOException
> {
>                  final Integer timeout = (Integer)
>                  context.getAttribute("timeout");
>                  if (timeout != null) {
>                      final NHttpClientConnection conn =
> (NHttpClientConnection)
>
> context.getAttribute(ExecutionContext.HTTP_CONNECTION);
>                      conn.setSocketTimeout(timeout);
>                  }
>              }
>      },
>      // Use standard client-side protocol interceptors
>      new RequestContent(),
>      new RequestTargetHost(),
>      new RequestConnControl(),
>      new RequestUserAgent(),
>      new RequestExpectContinue()});
>
> final HttpAsyncRequester requester = new HttpAsyncRequester(
>      httpproc,
>      new DefaultConnectionReuseStrategy(),
>      params);
> ---
>
> Would that do the trick for you?
Many thanks Oleg, That works perfectly :) !

regards
asankha

-- 
Asankha C. Perera
AdroitLogic, http://adroitlogic.org

http://esbmagic.blogspot.com




---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Mime
View raw message