2

I have nginx with enabled keepalive. I create simple java code, which sends 2 requests to nginx:

public class Main {
    public static void main(String[] args) {
        var apacheHttpClient = HttpClients.custom()
                .setConnectionManager(new PoolingHttpClientConnectionManager())
                .build();

        HttpGet httpGet = new HttpGet("http://127.0.0.1:5000");
        
        for (int i = 0; i < 2; i++) {
            try (CloseableHttpResponse httpResponse = apacheHttpClient.execute(httpGet)) {
                System.out.println(httpResponse.getStatusLine().getStatusCode());
            } catch (ClientProtocolException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

I have checked with wireshark that the java program creates 2 connection to execute this command. Why Java apache httpclient do not reuse connection? I assume that 1 connection should be established and be reused.enter image description here

Kreans
  • 60
  • 7
  • Does the nginx set a Keep-Alive header in the response? IIUC, it should reuse the connection if there is no Keep-Alive header anyways, but I'm not sure about that. See https://stackoverflow.com/questions/68990038/apache-httpclient-keep-alive-strategy-for-active-connections – GeertPt Mar 24 '23 at 08:29
  • yes, Nginx set a keep-alive header in the response – Kreans Mar 24 '23 at 23:11
  • OK, then what's the value of the keep-alive header? – GeertPt Mar 27 '23 at 07:45
  • Keep alive is set to 2000ms - confirmed by debug logs – Kreans Apr 11 '23 at 19:28

0 Answers0