0

I'm using the following config to create a RestTemplate bean.

@Bean
@Primary
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();

    return builder.requestFactory(() -> new BufferingClientHttpRequestFactory(factory))
            .build();
}

Problem: by default HttpClient is instantiated as follows:

org.apache.http.impl.client.HttpClientBuilder:

    String s = System.getProperty("http.keepAlive", "true");
    if ("true".equalsIgnoreCase(s)) {
        s = System.getProperty("http.maxConnections", "5");
        int max = Integer.parseInt(s);
        poolingmgr.setDefaultMaxPerRoute(max);
        poolingmgr.setMaxTotal(2 * max);
    }

Thus by default having a maximum of 10 concurrent url connections on that rest template.

Question: how could I best configure the max total when using spring-boot? I did not find any application.properties entry to set it to a custom value.

Sidequestion: what does the property per route mean? Is a route localhost:8080/myfirst, and another route is localhost:8080/mysnd? Or are both the same route localhost:8080?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

0

Sorry, I'm missunderstood your question.

It's simply: in application.properties you can create you own configuration. For example:

## Connection pool max size to appache http client
myProjectId.http.maxConnections=100

And then in you Bean/Service/Something else you can inject it by simple action

@Bean
public class HttpClient  {

    @Value( "${myProjectId.http.maxConnections}" )
    private int maxConnections;

    // some code below

}
Scrobot
  • 1,911
  • 3
  • 19
  • 36
  • Ok I thought I could maybe override the system property `http.maxConnections` with `application.properties` somehow... – membersound Apr 02 '19 at 13:31