Skip to main content
Level 2
Jan 15, 2025 10:16 AM UTC
Solved

ThreadPoolExecutor in AEM

hi 

 

can you share examples of how to use ThreadPoolExecutor in AEM along with Apache sling thread pool configuration (in configMgr), I guess these two goes together ?

 

Thank you 

arunpatidar
arunpatidarAccepted solution
Community Advisor
Jan 15, 2025 4:19 PM UTC

Hi @nbg19a 

I am not sure if you can change the thread pool for OOTB features, I have not seen any configuration for that.

There are configuration for sling queue, replication queues, there you can use custom thread pool.

 

I think below code can help?

 

@Reference ThreadPoolManager threadPoolManager; ThreadPool myThreadPool; @Activate public void activate() { myThreadPool = threadPoolManager.get("my-thread-pool"); } @Deactivate public void deactivate() { threadPoolManager.release(myThreadPool); } public void doSomething() { myThreadPool.execute (() -> { // submit a runnable to this threadpool }); }
Arun Patidar

9 replies

arunpatidar
Community Advisor
Community Advisor
Jan 15, 2025 3:51 PM UTC
Arun Patidar
nbg19aAuthor
Level 2
Jan 15, 2025 4:15 PM UTC

Thanks Arun,

 

my goal is not trigger scheduler or such, I would like to modify some existing code (that runs in the loop) to try to make it run in  parallel/concurrent, think that ThreadPoolExecutor is the right tool for that.

Translate
arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
Jan 15, 2025 4:19 PM UTC

Hi @nbg19a 

I am not sure if you can change the thread pool for OOTB features, I have not seen any configuration for that.

There are configuration for sling queue, replication queues, there you can use custom thread pool.

 

I think below code can help?

 

@Reference ThreadPoolManager threadPoolManager; ThreadPool myThreadPool; @Activate public void activate() { myThreadPool = threadPoolManager.get("my-thread-pool"); } @Deactivate public void deactivate() { threadPoolManager.release(myThreadPool); } public void doSomething() { myThreadPool.execute (() -> { // submit a runnable to this threadpool }); }
Translate
Arun Patidar
joerghoh
Adobe Employee
Adobe Employee
Jan 17, 2025 5:39 PM UTC

The Sling documentation provides a good example how to use the Sling threadpools and ThreadPool Manager:

https://sling.apache.org/documentation/bundles/apache-sling-commons-thread-pool.html#how-to-use-a-thread-pool

 

You don't to write your own executor, just submit all your runnables like in the example.

 

Translate
nbg19aAuthor
Level 2
Jan 20, 2025 7:56 AM UTC

Right, that's what Arun pointed out, that's the way to go.

Thank you everyone for your inputs.

Translate
AmitVishwakarma
Community Advisor
Community Advisor
Jan 19, 2025 5:31 PM UTC

To use ThreadPoolExecutor in AEM with Sling's thread pool:

Configure ThreadPoolExecutor:
Create and configure a ThreadPoolExecutor for managing custom background tasks.

ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
executor.execute(() -> {
// Your task logic here
});

Configure Sling Thread Pool in OSGi:

Modify the org.apache.sling.engine.impl.SlingMainServlet config in the OSGi console to set the maximum number of threads. Example config:

org.apache.sling.engine.impl.SlingMainServlet.threadPoolSize = 100

Use Together:
Use Sling's thread pool for request handling.
Use ThreadPoolExecutor for background tasks that don’t interfere with request processing.

This way, both pools can be utilized for their specific use cases: Sling for HTTP requests and ThreadPoolExecutor for background processing.

Translate
Amit Vishwakarma - Adobe Commerce Champion 2025 | 17x Adobe certified | 6x Adobe SME