background image

Coding the Message-Driven Bean:

<< Writing the Module Components | UsingTwo Application Servers >>
<< Writing the Module Components | UsingTwo Application Servers >>

Coding the Message-Driven Bean:

5. When each reply arrives, the message listener displays its contents and removes it from the
list of expected messages.
6. When all the messages have arrived, the client exits.
Coding the Message-Driven Bean: ReplyMsgBean.java
The message-driven bean class, replybean/src/ReplyMsgBean.java, does the following:
1. Uses the @MessageDriven annotation:
@MessageDriven(mappedName=
"jms/Topic")
2. Injects resources for the MessageDrivenContext and for a connection factory. It does not
need a destination resource because it uses the value of the incoming message's JMSReplyTo
header as the destination.
3. Uses a @PostConstruct callback method to create the connection, and a @PreDestroy
callback method to close the connection.
The onMessage method of the message-driven bean class does the following:
1. Casts the incoming message to a TextMessage and displays the text
2. Creates a connection, a session, and a publisher for the reply message
3. Publishes the message to the reply topic
4. Closes the connection
On both servers, the bean will consume messages from the topic jms/Topic.
Creating Resources for the sendremote Example
This example uses the connection factory named jms/ConnectionFactory and the topic
named jms/Topic. These objects must exist on both the local and the remote servers.
This example uses an additional connection factory, jms/JupiterConnectionFactory, which
communicates with the remote system; you created it in
"Creating Administered Objects for
Multiple Systems" on page 931
. This connection factory must exist on the local server.
The build.xml file for the multiclient module contains targets that you can use to create these
resources if you deleted them previously.
An Application Example That Deploys a Message-Driven Bean on Two Servers
The Java EE 5 Tutorial · September 2007
988