Search code examples
asynchronousglassfishsingletonejbstateless-session-bean

Most efficient way to provide EJB @Aynchronous methods for stateless logic


I need to create some EJBs to provide some @Aynchronous methods.

Some of the EJBs will have no state whatsoever.

Some might have CDI-injected (using @Inject) threadsafe instance variables that are @ApplicationScoped.

Is the following the most efficient way to do this?:

@Singleton
@ConcurrencyManagement(BEAN)
public class EjbClass {
    @Asynchronous
    public void asyncMethod() {
        //some code
    }
}

My second guess would be to use: @Stateless instead of the pair of @Singleton & @ConcurrencyManagement(BEAN).

But there shouldn't be any need for more than one instance of any of these EJBs, so, unless I'm missing some gotcha for @Singleton, I imagine that @Singleton would be most efficient.

I am using EJB 3.2 in GlassFish 4.1.


Solution

  • I agree with your assessment that @Singleton + @ConcurrencyManagement(BEAN) is a good fit for your problem description. As you highlight, @Stateless has the disadvantage that it might construct multiple instances if there are concurrent calls. That overhead is probably, but might as well avoid it since it's easy to do so.