Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I configure the thread pool behind the @Asynchronous calls in Java EE 6?

I recently learned that I can easily make any session bean method Asynchronous by simply adding the @Asynchronous annotation.

E.g.

@Asynchronous
public Future<String> processPayment(Order order) throws PaymentException {
    ... 
}

I know that Java EE 7 added Concurrency Utilities, but in Java EE 6, where is the thread pool configuration of the @Asyncronous methods? is there a way to set a timeout? is it a fixed thread pool? a cached one? what is it's priority? Is it configurable somewhere in the container?

like image 413
Eran Medan Avatar asked Jun 18 '13 17:06

Eran Medan


People also ask

Which of the following method is used to get instance of AsyncContext?

To obtain an instance of AsyncContext , call the startAsync() method on the request object of your service method; for example: public void doGet(HttpServletRequest req, HttpServletResponse resp) { ...

Which annotation is used to mark a specific method or all methods of the bean as asynchronous?

Annotate a business method with javax. ejb. Asynchronous to mark that method as an asynchronous method, or apply @Asynchronous at the class level to mark all the business methods of the session bean as asynchronous methods.

What are asynchronous business methods in Java?

Session beans can implement asynchronous methods, business methods where control is returned to the client by the enterprise bean container before the method is invoked on the session bean instance.


1 Answers

I think timeout could be achieved by invoking Future.cancel(boolean) from a method annotated @Timeout. Requires keeping a reference to the Future returned by the async method, Singleton-ejb can be used for this.

@Stateless
public class AsyncEjb {

    @Resource
    private SessionContext sessionContext;

    @Asynchronous
    public Future<String> asyncMethod() {

        ...
        //Check if canceled by timer
        if(sessionContext.wasCancelCalled()) {
            ...
        }
        ...

    }
}

@Singleton
public class SingletonEjb {
    @EJB
    AsyncEjb asyncEjb;

    Future<String> theFuture;

    public void asyncMethod() {

        theFuture = asyncEjb.asyncMethod();

        //Create programatic timer
        long duration = 6000;
        Timer timer =
        timerService.createSingleActionTimer(duration, new TimerConfig());

    }

    //Method invoked when timer runs out
    @Timeout
    public void timeout(Timer timer) {
        theFuture.cancel(true);
    }
}

Edit (new below):

In glassfish you may configure the ejb-pool by seting below attributes in the admin console

  • Initial and Minimum Pool Size
  • Maximum Pool Size
  • Pool Resize Quantity
  • Pool Idle Timeout

see Tuning the EJB Pool

like image 67
Aksel Willgert Avatar answered Sep 19 '22 21:09

Aksel Willgert