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 logic is fairly simple: if the message is NOT redelivered AND it is NOT already in a<br />

rollback, AND the rollbackOnly flag is true, THEN roll back the transaction. Otherwise<br />

commit the transaction.<br />

The inRollback flag is needed because of the behavior of the Session.rollback( )<br />

method. It causes all of the messages to be redelivered. In our case, that includes the<br />

"SEQUENCE_MARKER" message, which will always be the last message that is redelivered as a<br />

result of the rollback. [4] Without this flag, it would be impossible to tell whether the<br />

messages are being redelivered as a result of the transaction rollback, or for some other<br />

reason.<br />

[4] This illustrates the importance of the JMS provider's role in maintaining proper delivery order,<br />

especially in the redelivery case.<br />

Now you can see the reason for using the delayed rollback. This allows us to isolate the<br />

commit( ), rollback( ), and message redelivery logic in the handling of the<br />

"SEQUENCE_MARKER" message. Dealing with the rollback individually as each message<br />

arrives can quickly become unwieldy.<br />

Finally, let's try this redelivery logic out by simulating a failure. In the Wholesaler<br />

window, type:<br />

Surfboards, 999.99, 499.99<br />

Wetsuits, 299.99, 149.99<br />

Before you type end, simulate an abnormal shut down of the Retailer application by<br />

typing Ctrl-C in the Retailer command window. Then type "end" in the Wholesaler<br />

window.<br />

Now restart the Retailer application. When the retailer app comes back up, it shows that it<br />

is receiving the surfboard and wetsuit messages as redelivered messages and is placing the<br />

order again. The JMS provider knew that the Retailer had failed in the middle of a<br />

transaction. When the Retailer came back up and reconnected, the JMS provider<br />

redelivered the messages to it. The Retailer logic knew that the message was being<br />

redelivered as a result of a failure, as shown in the following section of autoBuy( ). The<br />

logic is fairly simple. If we are at the end marker:<br />

if ( sequence.equalsIgnoreCase("END_SEQUENCE") )<br />

{<br />

AND we are both redelivered AND in a rollback (we aren't in a rollback):<br />

if ( redelivered && inRollback ){ // At the end, start fresh<br />

inRollback = false;<br />

rollbackOnly = false;<br />

session.commit();<br />

}<br />

Else if we are in rollbackOnly mode (we aren't, but we could be and it would still work):<br />

else if ( rollbackOnly ){<br />

inRollback = true;<br />

session.rollback( );<br />

}<br />

101

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

Saved successfully!

Ooh no, something went wrong!