RestTemplate is giving org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool

Viewed 35

I am trying to POST a payload with almost 4-5 MB of data through RestTemplate. Some of the request are passing but some of the requests are not even going to API gateway with this error

org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool

Here is the rest template implementation I am using.

@Component
public class RestTemplateHelper {
    @Autowired
    private RestTemplate restTemplate;

public RestTemplateHelper() {
}
private <X, Y> Y post(final String url, final String accessToken, final MediaType mediaType, final X requestBody, final Class<Y> responseType, final Map<String, String> extraHeaders, HttpMessageConverter httpMessageConverter, RestTemplate restTemplateFromParam) {
        HttpHeaders headers = this.getHeaders(accessToken, mediaType, extraHeaders);
        HttpEntity<X> httpEntity = new HttpEntity(requestBody, headers);

        ResponseEntity responseEntity;
        NonRetriableException nonRetriableException;
        try {
            RestTemplate restTemplateToUse = this.restTemplate;
            if (Objects.nonNull(httpMessageConverter)) {
                restTemplateToUse = Objects.nonNull(restTemplateFromParam) ? restTemplateFromParam : restTemplateToUse;
                restTemplateToUse.getMessageConverters().add(0, httpMessageConverter);
            }

            responseEntity = restTemplateToUse.exchange(url, HttpMethod.POST, httpEntity, responseType, new Object[0]);
        } catch (ResourceAccessException var14) {
            nonRetriableException = new NonRetriableException(var14);
            nonRetriableException.setRootException(var14);
            throw nonRetriableException;
        } catch (RestClientResponseException var15) {
            nonRetriableException = new NonRetriableException(var15);
            nonRetriableException.setRawStatusCode(var15.getRawStatusCode());
            nonRetriableException.setStatusText(var15.getStatusText());
            nonRetriableException.setResponseBody(var15.getResponseBodyAsString());
            nonRetriableException.setResponseHeaders(var15.getResponseHeaders());
            nonRetriableException.setRootException(var15);
            throw nonRetriableException;
        } catch (Exception var16) {
            throw new NonRetriableException(var16);
        }

        return responseEntity.getBody();
    }
}
0 Answers
Related