Java Reference
In-Depth Information
•
Controlling message acknowledgment
: You can specify various levels of control
over message acknowledgment.
•
Specifying message persistence
: You can specify that messages are persistent,
meaning they must not be lost in the event of a provider failure.
•
Setting message priority levels
: You can set various priority levels for messages,
which can affect the order in which the messages are delivered.
•
Allowing messages to expire
: You can specify an expiration time for messages so
they will not be delivered if they are obsolete.
•
Creating temporary destinations
: You can create temporary destinations that last
only for the duration of the connection in which they are created.
Controlling Message Acknowledgment
Until a JMS message has been acknowledged, it is not considered to be successfully con-
sumed. The successful consumption of a message ordinarily takes place in three stages.
1.
The client receives the message.
2.
The client processes the message.
3.
The message is acknowledged. Acknowledgment is initiated either by the JMS
provider or by the client, depending on the session acknowledgment mode.
In transacted sessions (see “
Using JMS API Local Transactions
”
on page
366
)
, acknow-
ledgment happens automatically when a transaction is committed. If a transaction is rolled
back, all consumed messages are redelivered.
In nontransacted sessions, when and how a message is acknowledged depend on the value
specified as the second argument of the
createSession
method. The three possible
argument values are as follows:
•
Session.AUTO_ACKNOWLEDGE
: The session automatically acknowledges a
client's receipt of a message either when the client has successfully returned from
a call to
receive
or when the
MessageListener
it has called to process the
message returns successfully.
A synchronous receive in an
AUTO_ACKNOWLEDGE
session is the one exception
to the rule that message consumption is a three-stage process as described earlier.
In this case, the receipt and acknowledgment take place in one step, followed by
the processing of the message.
•
Session.CLIENT_ACKNOWLEDGE
: A client acknowledges a message by call-
ing the message's
acknowledge
method. In this mode, acknowledgment takes
place on the session level: Acknowledging a consumed message automatically ac-