Consuming messages with an MDB
The last thing we must do to complete our business scenario is to retrieve the posted messages from the queue and create a new exhibition line in the database by using the persistence layer. To do so, follow these steps:
Right-click on the Theater project's name, select New… in the top of the context menu, and then select Other….
Type
Message
in the search field, select the Message-Driven Bean (EJB 3.x) entry, and click on Next.Configure the fields as shown in the following screenshot:
Click on Finish.
As a message carries an instance of class Exhibition
, we will just extract and pass it to a persistence manager that will save the entity in following manner:
Add a reference to inject the default persistence context:
@PersistenceContext EntityManager em;
In the
onMessage
method, we retrieve the object from the message and persist it. As the object is already an instance of exhibition, we didn't have to cast it, but let's do it for clarity and to print a message to...