Why Your Java HTTP Client Hangs for 15 Minutes and How to Fix It
The article recounts a real‑world incident where a Java message‑queue system stalled for over ten minutes because an HTTP client call lacked a timeout, walks through the step‑by‑step debugging process, shows the problematic code, and provides proper timeout configurations for different HttpClient versions to prevent such blocks.
Introduction
In many backend systems, asynchronous calls to third‑party services are performed via a message queue. The author experienced a situation where the queue became blocked for more than ten minutes, causing downstream messages to stop processing.
Problem Symptoms
The queue appeared to be stuck: messages piled up, and no new messages could be added. Logs showed that a producer had posted data once and then stopped, while the consumer continued processing for several minutes after that, indicating that the producer was being blocked by the consumer.
Investigation Steps
The author first considered the queue implementation itself (e.g., a while‑true loop with sleep) and ruled out excessive sleep or thread interruption. By examining logs, it became clear that the consumer’s HTTP request was holding the connection for an unusually long time.
Root Cause
Log analysis revealed an HTTP request whose parameters were logged, but the response was only logged after a 15‑minute delay, showing a remote service exception. The underlying code used Apache HttpClient without any timeout settings, causing the request to wait indefinitely.
public static String doPostWithJSON(String url, String json) throws Exception {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity se = new StringEntity(json, Charset.forName("UTF-8"));
se.setContentType("application/json");
httpPost.setEntity(se);
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
return result;
}Because no timeout was configured, the client waited until the remote service finally timed out after fifteen minutes.
Setting Timeouts in Different HttpClient Versions
HttpClient 4.3 :
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);HttpClient 4.4 and later :
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(10000)
.setConnectTimeout(10000)
.build();
httpGet.setConfig(requestConfig); setConnectTimeoutdefines the maximum time to establish a connection (milliseconds), while setSocketTimeout defines the maximum period of inactivity between data packets. If the remote service does not respond within these limits, the request is aborted and an exception is thrown.
Conclusion
The case demonstrates how a missing timeout configuration in an HTTP client can silently block a production system. Setting appropriate connection and socket timeouts for every HTTP call, and handling the resulting exceptions, is essential to avoid similar incidents.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
