23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />

The acknowledge( ) method informs the JMS provider that the message has been<br />

successfully received by the consumer. This method throws an exception to the client if a<br />

provider failure occurs during the acknowledgment process. The provider failure results in<br />

the message being retained by the JMS server for redelivery. Therefore, the exception<br />

handling code should undo the results of any partially processed business logic in<br />

preparation for receiving the message again, or it should log the message as processed so<br />

that the redelivered message can be ignored. The acknowlege( ) method should only be<br />

used with the CLIENT_ACKNOWLEDGE mode; if used with the AUTO_ACKNOWLEDGE or<br />

DUPS_OK_ACKNOWLEDGE mode, the call is ignored by the JMS provider.<br />

6.2.3.1 Grouping multiple messages<br />

The CLIENT_ACKNOWLEDGE mode also gives you the ability to batch together multiple<br />

message receipts and consume them in an all-or-nothing fashion. A consuming client may<br />

receive several messages in a sequence and treat them as a group. CLIENT_ACKNOWLEDGE<br />

does not provide the capability to consume messages selectively. A single<br />

acknowledgment for the last message in the group implicitly acknowledges all previously<br />

unacknowledged messages for the current session. This means that if the client application<br />

fails before the last message is acknowledged, it may recover when it comes back up. All<br />

of the unacknowledged messages will be resent with the JMSRedelivered flag set on each<br />

of the unacknowledged messages. A JMS client may also call the recover( ) method on a<br />

Session object, to force the redelivery of all previously unacknowledged messages, even if<br />

there hasn't been a failure.<br />

JMS also provides a transaction model for grouping multiple sends and receives. We cover<br />

this in detail later in the Section 6.4 of this chapter.<br />

6.3 <strong>Message</strong> Groups and Acknowledgment<br />

When multiple messages need to be dealt with as a group, the application needs to be able<br />

to store or cache interim messages until the entire group has been delivered. This<br />

requirement typically means that the asynchronous invocation of the on<strong>Message</strong>( ) handler<br />

would result in business logic getting executed, and data would be placed temporarily in a<br />

database table in preparation for processing the group of messages as a whole. When the<br />

last message of the group arrives, the application can then go to the database to retrieve the<br />

data from the previous messages to establish any context it may need.<br />

6.3.1 Handling Redelivery of <strong>Message</strong>s in an Application<br />

JMS provides strict rules that govern when the redelivered flag is set. In AUTO_ACKNOWLEDGE<br />

mode, only the most recently consumed message is subject to ambiguous redelivery. In<br />

other modes, multiple messages may have the redelivered flag set. It is up to the<br />

application designer to isolate the conditions under which ambiguity can occur, and to<br />

account for it in the application.<br />

6.3.2 <strong>Message</strong> Groups in QRetailer<br />

A message that has been redelivered should be reconciled against any application state to<br />

resolve ambiguities related to the reason for redelivery. To see this in action, run the<br />

91

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!