ThreadPoolExecutor in AEM | Community
Skip to main content
January 15, 2025
Solved

ThreadPoolExecutor in AEM

  • January 15, 2025
  • 3 replies
  • 1487 views

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 

Best answer by arunpatidar

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 }); }

3 replies

arunpatidar
Community Advisor
Community Advisor
January 15, 2025
nbg19aAuthor
January 15, 2025

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.

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
January 15, 2025

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
joerghoh
Adobe Employee
Adobe Employee
January 17, 2025

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.

 

nbg19aAuthor
January 20, 2025

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

Thank you everyone for your inputs.

AmitVishwakarma
Community Advisor
Community Advisor
January 19, 2025

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.