You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Christopher BROWN <br...@reflexe.fr> on 2014/09/03 11:01:16 UTC

Changing the connect/socket timeout for an instance of CloseableHttpClient, without creating a new instance

Hello,

I'm using the 4.3.x "fluent API" of HTTP client.  I'd like to be able to
change the socket timeout and the connect timeout at runtime without having
to shutdown an active HTTP client and its associated pooling connection
manager (because that would likely cause a brief period of unavailability
adding further complexity to calling code, or failure).  Here's a
(simplified) bit of code showing how I set it up at first.

final HttpClientBuilder builder = HttpClients.custom().useSystemProperties()
.disableAuthCaching()
 .disableAutomaticRetries()
.disableContentCompression()
 .disableCookieManagement()
.disableRedirectHandling();

if (highCapacity)
{
_pool = new PoolingHttpClientConnectionManager();
 doSetNetworkPoolSize(_ps.getNetworkPoolSize(), _pool);
builder.setConnectionManager(_pool);
}
else
{
_pool = null;
 builder.setConnectionManager(new BasicHttpClientConnectionManager());
}

builder.*setDefaultRequestConfig*(RequestConfig.custom()
 .setAuthenticationEnabled(false)
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
 .setRedirectsEnabled(false)
.*setSocketTimeout(SOCKET_TIMEOUT)*
 .*setConnectTimeout(CONNECTION_TIMEOUT)*
.build()
);

_http = builder.build();

The JavaDoc for "*setDefaultRequestConfig*" states:
*Assigns default RequestConfig instance which will be used for request
execution if not explicitly set in the client execution context.*

...but doesn't go as far as indicating how to explicitly set in the client
execution context.  I currently execute requests via:
CloseableHttpClient.execute(HttpUriRequest)

...so I assumed that this meant I should instead use:
CloseableHttpClient.execute(HttpUriRequest,HttpContext)

...but *HttpContext* is a very generic interface, and (assuming that's what
I'm supposed to be using), I can't see how to create or obtain an instance
that might override just these specific properties without clobbering
anything else unintentionally.

What is the intended way to override (per request, or for all subsequent
requests) the socket timeout and the connect timeout?

Thanks in advance.
Christopher

Re: Changing the connect/socket timeout for an instance of CloseableHttpClient, without creating a new instance

Posted by Todd W Lainhart <la...@us.ibm.com>.
HttpRequestBase has a "setRequestConfig" method.

Note that the default provided by the HttpClient builder and the 
RequestConfig set on the request method are not "merged" if both are 
present.  If there is a RequestConfig associated to the request, all of 
its values are considered, regardless if there is a default set on the 
HttpClient instance.





Todd Lainhart
Rational software
IBM Corporation
550 King Street, Littleton, MA 01460-1250
1-978-899-4705
2-276-4705 (T/L)
lainhart@us.ibm.com




From:   Christopher BROWN <br...@reflexe.fr>
To:     Apache HTTP Client Users List <ht...@hc.apache.org>
Date:   09/03/2014 05:02 AM
Subject:        Changing the connect/socket timeout for an instance of 
CloseableHttpClient, without creating a new instance



Hello,

I'm using the 4.3.x "fluent API" of HTTP client.  I'd like to be able to
change the socket timeout and the connect timeout at runtime without 
having
to shutdown an active HTTP client and its associated pooling connection
manager (because that would likely cause a brief period of unavailability
adding further complexity to calling code, or failure).  Here's a
(simplified) bit of code showing how I set it up at first.

final HttpClientBuilder builder = 
HttpClients.custom().useSystemProperties()
.disableAuthCaching()
 .disableAutomaticRetries()
.disableContentCompression()
 .disableCookieManagement()
.disableRedirectHandling();

if (highCapacity)
{
_pool = new PoolingHttpClientConnectionManager();
 doSetNetworkPoolSize(_ps.getNetworkPoolSize(), _pool);
builder.setConnectionManager(_pool);
}
else
{
_pool = null;
 builder.setConnectionManager(new BasicHttpClientConnectionManager());
}

builder.*setDefaultRequestConfig*(RequestConfig.custom()
 .setAuthenticationEnabled(false)
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
 .setRedirectsEnabled(false)
.*setSocketTimeout(SOCKET_TIMEOUT)*
 .*setConnectTimeout(CONNECTION_TIMEOUT)*
.build()
);

_http = builder.build();

The JavaDoc for "*setDefaultRequestConfig*" states:
*Assigns default RequestConfig instance which will be used for request
execution if not explicitly set in the client execution context.*

...but doesn't go as far as indicating how to explicitly set in the client
execution context.  I currently execute requests via:
CloseableHttpClient.execute(HttpUriRequest)

...so I assumed that this meant I should instead use:
CloseableHttpClient.execute(HttpUriRequest,HttpContext)

...but *HttpContext* is a very generic interface, and (assuming that's 
what
I'm supposed to be using), I can't see how to create or obtain an instance
that might override just these specific properties without clobbering
anything else unintentionally.

What is the intended way to override (per request, or for all subsequent
requests) the socket timeout and the connect timeout?

Thanks in advance.
Christopher