Expand my Community achievements bar.

HttpClient Instance AEM OSGI Workflow

Avatar

Level 2

I have an AEM OSGI Workflow application that makes a number of REST API calls as part of the workflow. For every REST call I am creating a new HttpClient instance and noticing some slow memory leak issues with this over a long period of time. The Apache documentation for HttpClient actually suggests to use a single instance of HttpClient throughout the lifetime of the application. Is something like this possible within AEM where I could maintain a single instance that can be used throughout each step of the workflow? Thanks for your help.

3 Replies

Avatar

Employee Advisor

I would rather look for the reasons of the memory leak...

Jörg

Avatar

Level 2

Hi Jorg,

I'm referring to the HttpClient Performance Guide: HttpClient - HttpClient Performance Optimization Guide

Specifically:

Generally it is recommended to have a single instance of HttpClient per communication component or even per application. However, if the application makes use of HttpClient only very infrequently, and keeping an idle instance of HttpClient in memory is not warranted, it is highly recommended to explicitly shut down the multithreaded connection manager prior to disposing the HttpClient instance. This will ensure proper closure of all HTTP connections in the connection pool.

I've already modified a lot of the code so only one httpclient instance is created per step and noticed a difference. I also have scheduler services that iterate through number of requests. So was wondering if there was a way to refer to just one instance for all components of the application.

Avatar

Employee Advisor

You can implement the connection manager as OSGI service, then it's a singleton by default.

Jörg