Cloud Manager code quality · Java code rules (SonarQube) · updated
AEMSRE-870: Reuse instances of a HTTPClient
AEMSRE-870 fires when code creates a new HttpClient (for example with HttpClients.createDefault()) inside a method that runs per request. Create the client once, as a field set in the constructor or the OSGi activate() method, and reuse it for every call.
| Rule key | AEMSRE-870 |
|---|---|
| Adobe's rule name | Reuse instances of a HTTPClient |
| Type | Software quality |
| Severity | Minor |
| Since | 2023.11 |
| Checked by | SonarQube analysis of your Java code |
Why Cloud Manager flags it
Each Apache HttpClient owns a connection pool. Creating one per call throws the pool away every time: no keep-alive, a new TCP and TLS handshake per request, and sockets left in TIME_WAIT. Under load that turns into slow pages and, on Cloud Service with advanced networking, exhausted egress ports. Adobe's compliant example keeps the client as a class-level field initialised in the constructor or activate().
Reusing the client also gives one place to set timeouts (ConnectionTimeoutMechanism), proxy settings and pool sizes, and one place to close it in @Deactivate.
Lifecycle in an OSGi service
| Step | Where | What |
|---|---|---|
| Create | @Activate / @Modified | Build the client with timeouts and pool limits from the OSGi config |
| Use | Service methods | Execute requests; always consume or close the response |
| Close | @Deactivate | client.close() so a bundle restart does not leak the pool |
Example
Non-compliant (as in Adobe's example)
public String fetch(String url) throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
return EntityUtils.toString(client.execute(new HttpGet(url)).getEntity());
}Compliant
private CloseableHttpClient client;
@Activate
protected void activate(Config cfg) {
client = clientBuilderFactory.newBuilder()
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(cfg.connectMs()).setSocketTimeout(cfg.socketMs()).build())
.build();
}
@Deactivate
protected void deactivate() throws IOException { client.close(); }How to fix it
- Search for HttpClients.createDefault(), HttpClientBuilder.create() and newBuilder().build() inside non-lifecycle methods.
- Move creation into @Activate (or the constructor for plain classes) and store the client in a field.
- Close it in @Deactivate, and close every response in try-with-resources.
- Use AEM's HttpClientBuilderFactory so proxy settings from the platform apply.
Does it stop the pipeline?
Code smells feed the Maintainability Rating, which is based on the total estimated fix time as a share of development time, not on any single finding. One finding rarely moves it, but hundreds can push the rating below A, which is an important failure: the pipeline pauses until someone overrides or fixes it.
If a finding is a genuine false positive, Adobe's code quality page lets you annotate the code with @SuppressWarnings and the rule ID. In a full-stack pipeline, important failures can be overridden by a deployment manager, project manager or business lead; in a code-quality-only pipeline they cannot.
Questions
Is a shared HttpClient thread-safe?
Yes. Apache HttpClient with a pooling connection manager is designed to be shared between threads.
What if different calls need different timeouts?
Keep one client and pass a RequestConfig per request, or keep one client per backend. Both avoid a new pool per call.
Does this rule stop the pipeline?
No, it is a minor software-quality finding. The performance impact under load is the real reason to fix it.
See the Cloud Service blockers in your code before Cloud Manager shows them. The free Surge Migrate readiness scan reads a repository zip or a Best Practices Analyzer CSV in your browser and lists them with file, fix and effort. Nothing is uploaded.
Sources
- Adobe: Custom code quality rules (checked 10 Nov 2026)
- Adobe: Code quality testing in Cloud Manager (checked 10 Nov 2026)
Rule key, type, severity and version are as listed on Adobe's page, checked 10 Nov 2026. Explanations and examples are ours unless marked as Adobe's.