background image

Coding the Message-Driven Beans

<< Writing the Application Components | Creating Resources >>
<< Writing the Application Components | Creating Resources >>

Coding the Message-Driven Beans

3. Creates a MessageConsumer for the TemporaryQueue, sets the MessageConsumer's message
listener, and starts the connection
4. Creates a MessageProducer and a MapMessage
5. Creates five new employees with randomly generated names, positions, and ID numbers (in
sequence) and publishes five messages containing this information
The message listener, HRListener, waits for messages that contain the assigned office and
equipment for each employee. When a message arrives, the message listener displays the
information received and determines whether all five messages have arrived. When they have,
the message listener notifies the main program, which then exits.
Coding the Message-Driven Beans for the clientmdbentity Example
This example uses two message-driven beans:
clientmdbentity-ejb/src/java/EquipmentMDB.java
clientmdbentity-ejb/src/java/OfficeMDB.java
The beans take the following steps:
1. They inject MessageDrivenContext and ConnectionFactory resources.
2. The onMessage method retrieves the information in the message. The EquipmentMDB's
onMessage
method chooses equipment, based on the new hire's position; the OfficeMDB's
onMessage
method randomly generates an office number.
3. After a slight delay to simulate real world processing hitches, the onMessage method calls a
helper method, compose.
4. The compose method takes the following steps:
a. It either creates and persists the SetupOffice entity or finds it by primary key.
b. It uses the entity to store the equipment or the office information in the database, calling
either the doEquipmentList or the doOfficeNumber business method.
c. If the business method returns true, meaning that all of the information has been stored,
it creates a connection and a session, retrieves the reply destination information from
the message, creates a MessageProducer, and sends a reply message that contains the
information stored in the entity.
d. It removes the entity.
Coding the Entity Class for the clientmdbentity Example
The SetupOffice class, SetupOffice.java, is an entity class. The entity and the
message-driven beans are packaged together in an EJB JAR file. The entity class is declared as
follows:
@Entity
public class SetupOffice implements Serializable {
A Java EE Application That Uses the JMS API with an Entity
Chapter 32 · Java EE Examples Using the JMS API
973