When integrating with external services or APIs from Adobe Experience Manager (AEM), utilizing the HttpClient for outbound HTTP requests is a common requirement. It’s crucial to handle these connections efficiently, securely, and in a way that is compatible with AEM’s architecture to ensure optimal performance and maintainability of your code.
AEM provides a specialized factory, HttpClientFactory, designed to streamline the creation and configuration of HttpClient instances. This factory not only simplifies the process of instantiating HttpClient objects but also ensures that the instances are optimized for use within the AEM environment.
Be careful when you use HttpClient or CloseableHttpClient, which does not auto close the connection until you read a body and you might have endup in issues because of consuming all the connectionPool thread
@arunpatidar I hope tis message finds you well, I just want to ask its recommended to use CloseableHttpClient or whether if we should use HttpClient? because in the AEM documentation, I see they give preference to the CloseableHttpClient...
Hi @bhavigoyal You can use the CloseableHttpClient but if you are just reading the status from response then connection would not be closed automatically.
CloseableHttpClient closeableHttpClient = httpClientService.getConfiguredHttpClient();
response = closeableHttpClient.execute(method);
statusCode = response.getStatusLine().getStatusCode();
// Connection will only be autoclosed if response is called.
response.getEntity();