Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 229
Yanosh Avatar asked Nov 19 '14 19:11

Yanosh


2 Answers

@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.

like image 164
Brett Kail Avatar answered Sep 27 '22 23:09

Brett Kail


@MessageDriven (MDBs) is part of JMS API. JMS has all sorts of extras when it comes to retrying on failed message consumption,transaction support and also allows you to control the queue of messages.

@Asynchronous annotation was not introduced unti java-ee-6 (ejb 3.1).

Assuming the usecase is simple asynchronous invocation in a java-ee-6 container or above, use @Asynchronous (arun guptas blog on this)

If you need more beyond that, JMS might be an option

like image 36
Aksel Willgert Avatar answered Sep 27 '22 22:09

Aksel Willgert