Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managed Executor Service in TomEE

I have a server with 48 CPUs hosting a Java EE 7 REST API on TomEE+ 7.0.2.

Some of the APIs are in need of making use of as many CPUs as possible as they run parallelized algorithms. The parallelized portion do not require any database or other resources, just some heavy lifting in a shared double[][] matrix.

I am usually working in EJB contexts, but for this particular instance it is not a requirement (and also preferred not to be).

So far I was using

ExecutorService pool = Executors.newFixedThreadPool(maxThreads); 

in order to instantiate an executor, but as this seems to spawn actual threads on operating system level I am not a big fan of it - after some JMeter load testing it even led to a point, where the the whole bash was blocked and I could not even SSH the server any more until hard reboot.

I stumbled upon the concept of "Managed Executor Service", but I cannot find a tutorial / example online in how to make use of that (and also configure that).

Could someone please share thoughts on the following?

a) How to configure a thread pool in TomEE (e.g. via server.xml, context.xml or tomee.xml), code examples would be appreciated?

b) Is there a way to just make use of some default thread pool (and is that clever enough to not need tuning, if no, where could I start out tuning that)?

c) How can I look up the thread pool in Java then - preferred via JDNI lookup?

d) If I once decided to make that resource part of EJB, what would the code for Injection look like?

My application context is specified as "myContext" in server.xml, so if you provide samples could you please indicate how the lookup strings would look like, exactly?

Other than that I have a very plain installation of TomEE+ 7.0.2, I did not touch any configuration so far.

Thank you so much for your help!

Daniel

like image 713
Daniel Avatar asked Nov 19 '25 11:11

Daniel


1 Answers

Here's a good tutorial to get started: https://martinsdeveloperworld.wordpress.com/2014/02/25/using-java-ees-managedexecutorservice-to-asynchronously-execute-transactions/

If you inject @ManagedExecutorService, TomEE should give you a default service and pool. If it does not, that's probably a bug:

@Resource
private ManagedExecutorService mes;

You should be able to configure it in TomEE.xml like this (I didn't test this):

<Resource id="myManagedExecutorService" type="javax.enterprise.concurrent.ManagedExecutorService">
    Core = 5
    Max = 25
    KeepAlive = 5 s
    Queue = 15
    WaitAtShutdown = 30 seconds
</Resource>

And in your code:

@Resource("myManagedExecutorService")
private ManagedExecutorService mes;

I figured this out by looking at service-jar.xml. You may also want to JMS and @Asyncronous which are a bit better options than ManagedExecutorService in my opinion

like image 104
Jonathan S. Fisher Avatar answered Nov 21 '25 07:11

Jonathan S. Fisher