I canot speak for HttpClient5, but If you are just looking to change some timeouts on the request, in 4.5.5, I do this (as recommended from this emai list some time ago!).
I guess its not too differenent for HC5 (?)
- Create a shared HTTPClient instance. this is used by all requests in the JVM and is instantiated on first use.
- each request (thread) grabs the shared client (it is thread safe)
- each request (thread) creates its own request context
- set the request type specific timeout on the request context (note internally in our app we apply different timeouts depending on several factors (message type,client etc))
- execute the request on the shared client with the request context
Seems to work fine...
Some snippets
// in a client factory class I create the client based on sensible timeout defaults
// this populates a static httpclient which can be returned by a static getClient method to all threads needing a httpClient
CloseableHttpClient newClient = HttpClientBuilder.create().useSystemProperties()
.setDefaultRequestConfig(config).setMaxConnPerRoute(maxConnectionsPerRoute)
.setMaxConnTotal(totalMaxConnection).evictExpiredConnections()
.evictIdleConnections(idleLife, TimeUnit.MINUTES).setConnectionTimeToLive(maxLife, TimeUnit.MINUTES)
.build();
statichttpClient = newClient;
// within the request
httpClient = HttpClientFactory.getClient();
// create the context for this thread
HttpClientContext httpContext = HttpClientContext.create();
httpContext.setRequestConfig(getRequestConfig());
HttpResponse serviceResponse = httpClient.execute(httpRequest, httpContext);
// make the request config
private RequestConfig getRequestConfig() throws PCEConfigParamNotFoundException {
// setup request timeouts
return RequestConfig.custom().setConnectionRequestTimeout(aaa). setConnectTimeout(bbb). setSocketTimeout(ccc).build();
}
-----Original Message-----
From: / [mailto:***@yahoo.co.uk.INVALID]
Sent: Wednesday, May 16, 2018 4:01 PM
To: httpclient-***@hc.apache.org
Subject: Re: Fwd: HttpClient5 : simple example on how to configure timeout after build()
Thanks,
if I understood correctly, the pattern should be:
1) Create a RequestConfig (rc) object and keep it.
2) If you need to modify httpclient, modify the kept RequestConfig object
3) After doing said modifications **re-create httpclient** with kept
RequestConfig.
4) Keep the RequestConfig.
5) Keeping a client and modifying it as see fit is/will be deprecated.
Questions:
can I extract a RequestConfig from current client, modify its timeout
and then re-create a client with this cloned-and-modified RequestConfig?
(so as not to keep a RequestConfig at all but remember all the settings
I did to my client).
thanks
Post by Shawn HeiseyI am looking for an example on how to configure HttpClient5 after it
has been built and how to extract/print some of its configuration.
Once I have an HttpClient object, how do I go about and change some of
its settings, for example connection timeout or user-agent-string or
even cookie jar?
I am looking for the most straight-forward and efficient way to do
this. I don't care about "fluent" APIs neither about streams etc.
myclient.setParameter(connection_timeout, 1000);
For the most part, you can't change settings on an existing HttpClient
object. Since about 4.3, the objects and methods that allow clients to
be changed after creation are all deprecated. That capability is
completely gone in 5.x. Default settings are managed with builder
objects using fluent methods, then you create the client object with the
indicated settings. Here's how I create a client object with explicit
RequestConfig rc = RequestConfig.custom().setConnectTimeout(15000)
.setSocketTimeout(120000).build();
httpClient = HttpClients.custom().setDefaultRequestConfig(rc)
.setMaxConnPerRoute(300).setMaxConnTotal(5000).disableAutomaticRetries()
.build();
The httpClient field is an instance of HttpClient. I do not know what
kind of adjustments might need to be made for 5.x, but that should give
you an idea about how things are done since the way you're trying to do
it is no longer available.
Many of the settings you might be interested in can also be changed at
the request level. I do not know HOW to do this, only that it CAN be
done. I think this is what Oleg was referring to in his reply.
Thanks,
Shawn
---------------------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-***@hc.apache.org
For additional commands, e-mail: httpclient-users-***@hc.apache.org
B�KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKCB��[��X��ܚX�KK[XZ[��Y[�]\�\��][��X��ܚX�P˘\X�K�ܙ�B��܈Y][ۘ[��[X[