[Java][apache-httpclient] Apply connect timeout to requests, add read timeout support
Fixes #24269.
For the Java client with library=apache-httpclient, ApiClient.setConnectTimeout(int) stored the value in a connectionTimeout field that was never read again, so the setter was a silent no-op: the client fell back to Apache HttpClient 5's own defaults (3-minute connect timeout, unbounded response wait).
Changes
invokeAPI()now builds a per-requestRequestConfigfrom the configured timeouts and sets it on theHttpClientContext, sosetConnectTimeout(ms)actually bounds the TCP connect time, consistent with the other Java libraries (okhttp-gson, native).- Added symmetric
getReadTimeout()/setReadTimeout(ms)mapped to the response timeout, as suggested in the issue (mirrors the okhttp-gson naming). - A value of
0(the default) leaves the underlying client's configuration untouched.
The per-request RequestConfig approach was chosen over rebuilding the client with a PoolingHttpClientConnectionManager/ConnectionConfig because:
ConnectionConfigonly exists since httpclient5 5.2, while the Gradle template still ships 5.1.3 (RequestConfig#setConnectTimeout(Timeout)exists since 5.0 and is honored in all 5.x versions, taking precedence over connection-manager defaults in 5.3+);- it preserves a user-supplied custom
CloseableHttpClient(constructor orsetHttpClient) instead of silently discarding it.
Tests
Added ApiClientTest to the apache-httpclient petstore sample:
- getter/setter round-trips for both timeouts;
- a regression test that a request against a socket that accepts but never responds fails after the configured 500 ms read timeout (verified: 0.52 s elapsed) instead of hanging forever;
- a regression test that a connect to a non-routable TEST-NET-1 address fails within bounds instead of taking Apache's 3-minute default.
All 136 test classes in the sample pass; the echo_api and jackson3 samples were regenerated (./bin/generate-samples.sh bin/configs/java-apache-httpclient*.yaml) and compile.
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
./bin/generate-samples.sh bin/configs/java-apache-httpclient*.yaml(only the apache-httpclient templates changed; no doc changes, soexport_docs_generators.shproduces no diff). - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
cc @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg
Summary by cubic
Applies per-request connect and read timeouts in the Java apache-httpclient client to prevent hangs and make setConnectTimeout effective. Preserves defaults from a custom CloseableHttpClient. Fixes #24269.
-
Bug Fixes
- Applies
setConnectTimeout(ms)per request to bound TCP connect time. - Builds per-request
RequestConfigfrom the underlying client's defaults (viaConfigurable); 0 uses client defaults. - Rejects negative values in
setConnectTimeout()/setReadTimeout().
- Applies
-
New Features
- Added
getReadTimeout()/setReadTimeout(ms)to control response wait time. - Works with httpclient5 5.1.x+; per-request config is compatible across versions.
- Added