Does Java Apache HttpClient reuse keepalive connection?

192 Views Asked by At

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

0

There are 0 best solutions below