Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to keep multiple Java HttpConnections open to same destination
    text
    copied!<p>We are using HttpURLConnection API to invoke a REST API to the same provider often (kind of an aggregation usecase). We want to keep a pool of 5 connections always open to the provider host (always the same IP).</p> <p>What is the proper solution? Here is what we tried:</p> <pre><code> System.setProperty("http.maxConnections", 5); // set globally only once ... // everytime we need a connection, we use the following HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection(); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.setDoOutput(false); conn.setUseCaches(true); ... BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); ... </code></pre> <p>At this point we read the input stream until the BufferedReader returns no more bytes. What do we do after that point if we want to reuse the underlying connection to the provider? We were under the impression that if the input stream is completely read, the connection is then added back to the pool.</p> <p>It's been working for several weeks this way, but today it stopped working producing this exception: <code>java.net.SocketException: Too many open files</code></p> <p>We found numerous sockets in the CLOSE_WAIT state like this (by running <code>lsof</code>): <code>java 1814 root 97u IPv6 844702 TCP colinux:58517-&gt;123.123.254.205:www (CLOSE_WAIT)</code></p> <p>Won't either conn.getInputStream().close() or conn.disconnect() completely close the connection and remove it from the pool?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload