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
Solved! Go to Solution.
Views
Replies
Total Likes
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
});
}
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
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.
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
});
}
Thanks, think it helps.
If using ThreadPoolExecutor does it leverage Apache sling thread pool configuration ?
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.
Views
Replies
Total Likes
The Sling documentation provides a good example how to use the Sling threadpools and ThreadPool Manager:
You don't to write your own executor, just submit all your runnables like in the example.
Right, that's what Arun pointed out, that's the way to go.
Thank you everyone for your inputs.
Views
Replies
Total Likes
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.