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 keyAEMSRE-870
Adobe's rule nameReuse instances of a HTTPClient
TypeSoftware quality
SeverityMinor
Since2023.11
Checked bySonarQube 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

StepWhereWhat
Create@Activate / @ModifiedBuild the client with timeouts and pool limits from the OSGi config
UseService methodsExecute requests; always consume or close the response
Close@Deactivateclient.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

  1. Search for HttpClients.createDefault(), HttpClientBuilder.create() and newBuilder().build() inside non-lifecycle methods.
  2. Move creation into @Activate (or the constructor for plain classes) and store the client in a field.
  3. Close it in @Deactivate, and close every response in try-with-resources.
  4. 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

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.

Scan, Convert, Verify, Install

How Surge Migrate helps with a move to AEM as a Cloud Service

Surge Migrate moves a website from any CMS to any CMS, and AEM as a Cloud Service is one of the systems it writes. Pages are converted automatically, and you check the result before you install anything.

  1. 1

    Scan

    Names the system behind your site, counts pages and templates, lists the blocks to build and the work no tool does, and gives an effort range with its working.

    Scan a site →
  2. 2

    Convert

    Each page becomes an Edge Delivery document, an AEM Core Component page and import files for 7 other systems. 10 pages free; 500 or 2,000 pages in one paid run.

    Convert your pages →
  3. 3

    Verify

    A download is offered only when its own format check passes. The report lists every field not found and every component kept as plain content. Compare old and new pages side by side.

    Parity check →
  4. 4

    Install

    You import with the target system's own tool, on your own environment. Each download's README gives the command. Your system changes only when you run it.

    How each import was tested →

Content packages installed on a local AEM as a Cloud Service SDK. See the worked examples.

On AEM 6.5 code: Scan AEM 6.5 code free. The scan reads a repository zip or a Best Practices Analyzer CSV in your browser and lists Cloud Service blockers with file, fix and effort. Nothing is uploaded.