Java Reference
In-Depth Information
Turtle can then send the error message back to ActionBazaar:
Message m = context.createTextMessage(
String.format("Item in error %s",tr.getItem()));
JMSProducer producer = context.createProducer();
producer.send(destination, om);
Although we didn't explicitly show it in the example, there's one more MDB feature you
should know about: MDB transaction management. We'll discuss EJB transactions in gen-
eral in much more detail in the next chapter, so here we'll give you the “bargain-basement”
version.
4.3.9. Managing MDB transactions
By default, the container will start a transaction before the onMessage method is invoked
and will commit the transaction when the method returns, unless the transaction was
marked as rollback through the message-driven context or an unhandled runtime ex-
ception is thrown in the onMessage method. You'll learn more about EJB transactions in
chapter 6 , but this happens because the container assumes that a transaction is required by
the MDB. You can specify this explicitly using the following code:
@MessageDriven
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class TurtleShippingRequestMessageBean
If you want, you can also tell the container that the MDB shouldn't use transactions at all.
In this case, you need to ensure database and message acknowledgment reliability yourself.
The message is acknowledged regardless of whether the message listener finishes normally
or not. Generally, we don't recommend this approach. But you can do it as follows:
@MessageDriven
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class TurtleShippingRequestMessageBean
This very brief discussion of transaction management concludes our analysis of the basic
features that MDBs offer. We've discussed how you can use MDBs to use the power of
messaging without dealing with the low-level details of the messaging API. As you've
seen, MDBs provide a host of EJB features for free, such as multithreading, resource in-
Search WWH ::




Custom Search