Java Reference
In-Depth Information
Writing the Application Components for the
clientmdbentity
Example
Writing the components of the application involves coding the application client, the
message-driven beans, and the entity class.
Coding the Application Client:
HumanResourceClient.java
The application client,
clientmdbentity-app-client/src/java/HumanRe-
sourceClient.java
, performs the following steps:
1.
Injects
ConnectionFactory
and
Topic
resources
2.
Creates a
TemporaryQueue
to receive notification of processing that occurs,
based on new-hire events it has published
3.
Creates a
MessageConsumer
for the
TemporaryQueue
, sets the
Mes-
sageConsumer
'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
method, which then exits.
Coding the Message-Driven Beans for the
clientmdbentity
Example
This example uses two message-driven beans:
•
clientmdbentity-ejb/src/java/eb/EquipmentMDB.java
•
clientmdbentity-ejb/src/java/eb/OfficeMDB.java
The beans take the following steps:
1.
They inject
MessageDrivenContext
and
ConnectionFactory
re-
sources.
2.
The
onMessage
method retrieves the information in the message. The
Equip-
mentMDB
'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
.