3

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

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Daniel
  • 176
  • 1
  • 8

2 Answers2

1

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

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
  • your resource declaration can't be valid because it's not valid XML – Andrew Tobilko Nov 13 '19 at 20:51
  • It is both valid XML and valid configuration. It's java properties wrapped in an XML node – Jonathan S. Fisher Nov 13 '19 at 21:10
  • oh, I see, sorry. I thought they were supposed to be `Resource` attributes – Andrew Tobilko Nov 13 '19 at 21:12
  • Yep. The reason they do that is so the same value can be copied from a properties file which can _also_ be used to configure the server – Jonathan S. Fisher Nov 13 '19 at 22:11
  • Thank you, good to know. Actually, I am researching to make it (`ManagedExecutorService` and the package `javax.enterprise.concurrent`) work in plain Tomcat 8.5. Do you know if this is possible? – Andrew Tobilko Nov 13 '19 at 22:14
  • You would have to find an independent implementation :/ It's unlikely you'll be able to do so. Why not just run TomEE? It's 100% compatible with Tomcat (because it is Tomcat) – Jonathan S. Fisher Nov 14 '19 at 00:06
  • It's a big project, and I am not controlling it. It seems I have found the right dependency. https://stackoverflow.com/questions/58846495/how-to-make-javax-enterprise-concurrent-work-in-plain-non-ee-tomcat-8 I asked my question here, I'd love it if you had a look. – Andrew Tobilko Nov 14 '19 at 09:51
0

you can find the documentation here http://tomee.apache.org/admin/configuration/resources.html#_managedexecutorservice

The main advantage of these executors are:

  1. it is configured in the container - no need of a custom app configuration but it is still tunable without recompiling/changing the application
  2. it is not limited like @Asynchronous which doesn't define any particular pool so portability is not very high whereas these managed pools are quite uniform
  3. these pools are "enterprise" friendly because you have listeners to add security and auditing
  4. these pools propagate some context (security and jndi/the classloader typically)

In tomee we ali

Romain Manni-Bucau
  • 3,354
  • 1
  • 16
  • 13