Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Handling Http Requests, HttpClient, in AEM

Avatar

Community Advisor

6/13/24

handling-http-requests-httpclient-in-aem-featured-image (1).jpg


Handling Http Requests, HttpClient, in AEM

 
by SourcedCode.com (Brian Ka Sing Li)

Overview

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.


Learn More: https://sourcedcode.com/blog/aem/handling-http-requests-httpclient-in-aem

 

4 Comments

Avatar

Community Advisor

6/14/24

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

Ref : https://stackoverflow.com/questions/21082692/using-java-apache-poolingclientconnectionmanager-leaks-...  

 

Avatar

Level 4

8/7/24

@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...
 

Avatar

Community Advisor

8/8/24

Hi @bhavigoyal 
You can use the CloseableHttpClient but if you are just reading the status from response then connection would not be closed automatically.

 

Example:

1. Connection is not closed

CloseableHttpClient closeableHttpClient = httpClientService.getConfiguredHttpClient();
response = closeableHttpClient.execute(method);
statusCode = response.getStatusLine().getStatusCode();

 

2. Connection is closed

CloseableHttpClient closeableHttpClient = httpClientService.getConfiguredHttpClient();
response = closeableHttpClient.execute(method);
statusCode = response.getStatusLine().getStatusCode();
// Connection will only be autoclosed if response is called.
response.getEntity();

Avatar

Level 4

8/8/24

yes i agree with that i was known that connection should be closed but I want to cnfrm on CloseableHttpClient and HttpClient