Search code examples
asynchronousejbjmsmessage-driven-bean

Mdb vs EJB 3.1 async method


When should I choose ejb async method over MDB with java message service in order to fire async long time tasks?


Solution

  • @Asynchronous is only appropriate if the outer transaction needs to launch several pieces of work in parallel and then wait on them all (or launch a single piece of work in the background, do some work in the foreground, and then wait on the background work). @Asynchronous is not appropriate for transactional "fire and forget" because the container might crash before the asynchronous work ever begins executing (in my opinion, void EJB asynchronous methods are very rarely useful, perhaps for something like updating an in-memory cache). If you want to guarantee work will happen asynchronously without waiting for it to complete, then you should send a message to an MDB or schedule an EJB timer.