Message acknowledgement

You don’t want messages getting lost. If you have a crucial message on the queue, and the one bean who gets it can’t commit—or worse, throws an exception and dies—what happens to your critical message?

That’s the point of acknowledgement. The Container tells the messaging service (not the original producer) that the message was delivered and everything is fine.

But if later, while the consumer (the message-driven bean) is processing the message, a Bad Thing happens, the Container can tell the messaging service to put the message back in the queue.

How does the Container really know that something went wrong? Two ways, and it’s your choice as a Bean Provider.

  1. The transaction status

    Message acknowledgement is tied to whether the transaction commits or rolls back. If the transaction rolls back, the message goes back on the queue. You get this behavior for beans that use container-managed transaction demarcation (we’ll cover transactions in the next chapter).

    OR

  2. The method completion

    Message acknowledgement is tied to whether the method completes successfully. If the method throws a runtime exception, the message goes back on the queue. You get this behavior for beans that use bean-managed transaction demarcation.

Get Head First EJB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.