background image

simplemessage Application Client

<< A Message-Driven Bean Example | The Message-Driven Bean Class >>
<< A Message-Driven Bean Example | The Message-Driven Bean Class >>

simplemessage Application Client

The source code for this application is in the
tut-install/javaeetutorial5/examples/ejb/simplemessage/ directory.
The simplemessage Application Client
The SimpleMessageClient sends messages to the queue that the SimpleMessageBean listens to.
The client starts by injecting the the connection factory and queue resources:
@Resource(mappedName=
"jms/ConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName="jms/Queue")
private static Queue queue;
Next, the client creates the connection, session, and message producer:
connection = connectionFactory.createConnection();
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
messageProducer = session.createProducer(queue);
Finally, the client sends several messages to the queue:
message = session.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
message.setText(
"This is message " + (i + 1));
System.out.println(
"Sending message: " +
message.getText());
messageProducer.send(message);
}
Sends
Msg
Queue
Delivers
Msg
Application
Client
Java EE Server
EJB
Container
MDB Instances
FIGURE 23­1
The simplemessage Application
The simplemessage Application Client
The Java EE 5 Tutorial · September 2007
676