Java Reference
In-Depth Information
4.3.3. Developing a message consumer with MDB
Let's now explore developing an MDB by implementing the Turtle server message con-
sumer.
Listing 4.2
shows the MDB code that first retrieves shipping requests sent to
the queue and then saves each request in Turtle's database table named
SHIPPING_
REQUEST
. For simplicity, the Java Persistence API (JPA) is used in this example to persist
the data to the database. We'll cover more about JPA starting with
chapter 9
. Using JPA
also helps demonstrate the MDB lifecycle and transaction management; if there's a failure
processing the message, the transaction is rolled back and neither the database data gets
written nor is the message removed from the queue.
Listing 4.2. Turtle shipping request message bean