background image

A Message Acknowledgment Example

<< Nontransacted Sessions | The ackequivexample Project >>
<< Nontransacted Sessions | The ackequivexample Project >>

A Message Acknowledgment Example

A Message Acknowledgment Example
The AckEquivExample.java program shows how both of the following two scenarios ensure
that a message will not be acknowledged until processing of it is complete:
Using an asynchronous message consumer (a message listener) in an AUTO_ACKNOWLEDGE
session
Using a synchronous receiver in a CLIENT_ACKNOWLEDGE session
With a message listener, the automatic acknowledgment happens when the onMessage method
returns (that is, after message processing has finished). With a synchronous receiver, the client
acknowledges the message after processing is complete. If you use AUTO_ACKNOWLEDGE with a
synchronous receive, the acknowledgment happens immediately after the receive call; if any
subsequent processing steps fail, the message cannot be redelivered.
The program is in the following directory:
tut-install/javaeetutorial5/examples/jms/advanced/ackequivexample/src/java/
The program contains a SynchSender class, a SynchReceiver class, an AsynchSubscriber class
with a TextListener class, a MultiplePublisher class, a main method, and a method that runs
the other classes' threads.
The program uses the following objects:
jms/ConnectionFactory
, jms/Queue, and jms/Topic: resources that you created in
"Creating JMS Administered Objects for the Synchronous Receive Example" on page 915
jms/ControlQueue
: an additional queue
jms/DurableConnectionFactory
: a connection factory with a client ID (see
"Creating
Durable Subscriptions" on page 942
, for more information)
To create the new queue and connection factory, you can use Ant targets defined in the file
tut-install/javaeetutorial5/examples/jms/advanced/ackequivexample/build.xml.
To run this example, follow these steps:
1. In a terminal window, go to the following directory:
tut-install/javaeetutorial5/examples/jms/advanced/ackequivexample/
2. To create the objects needed in this example, type the following commands:
ant create-control-queue
ant create-durable-cf
3. To compile and package the program using NetBeans IDE, follow these steps:
a. In NetBeans IDE, choose Open Project from the File menu.
Creating Robust JMS Applications
The Java EE 5 Tutorial · September 2007
938