Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Asynchronous EJB Thread Pool Configuration

I have created a @Stateless EJB with an @Asynchronous method in order to run tasks in parallel.

The thing is, this method will be called a lot, in a loop, in order to process an import in parallel. By default, in Jboss, the EJB Thread Pool for Asynchronous process is 10.

We also have others process with @Asynchronous annotation, and if I understand well, that will put other task in a queue until a thread is available to run it.

I don't want to pollute the others asynchronous task with this new one, so I started to look at a way to isolate this @Asynchronous process in its own Pool. As this process could need 100 threads, the ideal would be to be able to create its own Thread Pool with this size.

I found that you can configure the EJB Pool with the jboss annotation @Pool, so I thought that was it, but I just realised this is for the EJB Pool, not the Thread Pool...

Is there any way to say that this Asynchronous method should be managed by its own Thread Pool ?

I found how to configure an EJB Thread Pool for JBoss here, so I wonder what is the point of doing that if you cannot reference it by an annotation or something ... https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6/html/Administration_and_Configuration_Guide/Create_a_Thread_Pool.html

Maybe this is one of the lack of Java EE 6 that has been fixed with 7 and the ManagedExecutorService ? If yes, I will have to see if my Jboss is Java EE 7 compliant.

Thank you,

F

like image 890
Farid Avatar asked Nov 08 '22 04:11

Farid


1 Answers

Java EE 7

Finally in Java EE 7 with JSR 236: Concurrency Utilities for JavaTM EE the ManagedExecutorService was introduced to run tasks in a configurable container managed thread pool. In Java EE 6 there is no similar approach.

So, if your application runs in a Java 7 EE environment using a ManagedExecutorService is your solution.

Java EE 6 Using JCA Work Manager

If your working in a Java EE 6 environment then you don't have the possibility to use an ManagedExecutorService but some Application Servers are implementing JSR 237: Work Manager for Application Servers.

A simple implementation could look as follows using JCA for injection.

@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class ThreadsResource {

    @Resource(name="jca/workmanager")
    WorkExecutorFactory executorFactory;

    public void execute(){
        try(WorkExecutor executor = executorFactory.newExecutor();){
        Runnable runnable = new Runnable(){
            @Override
            public void run() {
            //some work to do
            }
        };
        executor.execute(runnable);
  }

How to define a work manager please take a look at Configure the Java Connector Architecture (JCA) Subsystem (JBoss EAP 6.3 Doc).

Using JCA Work Managers is limited to certain App Servers e.g. JBoss 6. in case you are using JBoss AS7 this won't work.

like image 110
Paul Wasilewski Avatar answered Nov 15 '22 11:11

Paul Wasilewski