Search code examples
jakarta-eeejbejb-3.0message-driven-bean

Message driven bean applicable for this requirement?


Basically I have the problem of a long running task in my web application. Currently this task is executed by an EJB which is blocking for the time it is executing the task (task is an external programm and the EJB just waits for the result).

Now according to this post, I thought of using a Message Driven Bean together with a JMS Queue.

My Question: What are the advantages of this approach? If I'd annotate the calling method of the EJB as @Asychnronous wouldn't I have the same features like with a MDB? Is the feature that pending jobs won't be lost the only advantage of the JMS/MDB approach? In terms of resource management is it better to let an EJB wait for the result of the task or a MDB?


Solution

  • I have used JMS for this pattern a lot, and the reason is that before EJB 3.1, it was simply not possible to make asynchronous calls to Session Beans. That's why you are likely to find such code quite often in existing apps.

    Using asynchronous calls on EJBs has the benefit to be easier to setup (using JMS is not that complicated, but you need to create the resources, etc.). Using JMS has a couple of benefits if you are running in an environment where availability and scalability are important topics:

    • having JMS messages persisted is sometimes interesting, if you think about your app server crashing.
    • more importantly, in my opinion, using JMS makes it possible to add several "processing" nodes behind the JMS queue, which makes it easy to add capacity if your processing is long and your load increases (note that this can also be achieved by some app servers if you use remote session beans, but it's not that simple).