Expand my Community achievements bar.

SOLVED

ThreadPoolExecutor in AEM

Avatar

Level 2

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 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

9 Replies

Avatar

Community Advisor

Hi @nbg19a 

You can create a thread pool using a config 

https://sling.apache.org/documentation/bundles/apache-sling-commons-thread-pool.html

you can use custom thread pools with job and sling schedulers

https://sling.apache.org/documentation/bundles/scheduler-service-commons-scheduler.html#configure-th... 



Arun Patidar

Avatar

Level 2

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.

Avatar

Correct answer by
Community Advisor

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

Avatar

Level 2

Thanks, think it helps.

If using ThreadPoolExecutor does it leverage Apache sling thread pool configuration ?

Avatar

Community Advisor

Avatar

Level 2

Actually I don't see a way to use specific Apache sling thread pool configuration when using ThreadPoolExecutor unless creating CustomExecutorService that extends AbstractExecutorService and use threadPoolManager inside.

I think your approach, with ThreadPoolManager, is the most appropriate for AEM.

Avatar

Employee Advisor

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-th...

 

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

 

Avatar

Level 2

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

Thank you everyone for your inputs.

Avatar

Level 7

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.