Throwing a RuntimeException from an MDB in JBoss EAP
Environment
JBoss Enterprise Application Platform (EAP)
- 4.2
- 4.3
- 5
- 6
Issue
- Should I throw a RuntimeException from my MDB to force redelivery?
- I am processing a JMS message in an MDB and inserting data into a DB. If the DB is not available, I would like the message to be redelivered at some later time (and hopefully the DB is back online at that point). Since it is advised that we should not throw runtime exceptions from MDB, how will I handle such a condition ?
Resolution
According to the JMS 1.1 specification (section 4.5.2) it isn't appropriate to throw a RuntimeException from onMessage():
A client can register an object that implements the JMS MessageListener interface with a MessageConsumer. As messages arrive for the consumer, the provider delivers them by calling the listener’s onMessage method.
It is possible for a listener to throw a RuntimeException; however, this is considered a client programming error. Well-behaved listeners should catch such exceptions and attempt to divert messages causing them to some form of application-specific 'unprocessable message' destination.
Furthermore, a RuntimeException thrown from any method of the message-driven bean class (including a message listener method and the callbacks invoked by the container) results in the transition to the 'does not exist' state. If a message-driven bean uses bean-managed transaction demarcation and throws a RuntimeException, the container should not acknowledge the message.
Try using CMT and messageDrivenBeanContext.setRollbackOnly to handle such scenarios.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Comments