23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

In this example, we are using a message as an event, instead of transporting meaningful<br />

business data.<br />

The Retailer now indicates in its output window that it has received the second "Hot Buy"<br />

message and is placing an order. You will now see the messages for both the Surfboards<br />

and the Wetsuits in the Wholesaler window. The messages appear now because the order<br />

was placed using a transactional session. The Retailer published each message separately<br />

as it saw the "Hot Deals" coming in. However, since these messages were part of a<br />

transaction, the messages were held by the JMS provider, and not delivered. When<br />

Retailer saw the "SEQUENCE_MARKER" message, it performed a commit( ) on the transaction,<br />

causing the JMS provider to deliver both messages to Wholesaler.<br />

The basic logic that makes this work is in the autoBuy( ) method of Retailer:<br />

private void autoBuy (javax.jms.<strong>Message</strong> message){<br />

...<br />

if ( strmMsg.propertyExists("SEQUENCE_MARKER") ){<br />

String sequence = strmMsg.getStringProperty( "SEQUENCE_MARKER" );<br />

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

...<br />

session.commit( );<br />

}<br />

return;<br />

}<br />

...<br />

If you peeked ahead at the full example, note that the logic is more complex than this. The<br />

rest of the logic handles the rollback and redelivered conditions.<br />

Now let's look at the other case, in which one of the items doesn't have an acceptable price.<br />

Enter the following in the Wholesaler command window:<br />

Surfboards, 999.99, 499.99<br />

Wetsuits, 299.99, 299.99<br />

end<br />

This time, the new price for the wetsuits is the same as the old price - hence a "bad deal."<br />

You should see a "bad deal" message in the Retailer command window, with a number of<br />

indicators that messages are being redelivered. Let's review the part of Retailer's autoBuy(<br />

) that makes all this happen. It's a little complex, but we will walk through it step by step:<br />

private void autoBuy (javax.jms.<strong>Message</strong> message){<br />

int count = 1000;<br />

try {<br />

boolean redelivered = message.getJMSRedelivered( );<br />

Stream<strong>Message</strong> strmMsg = (Stream<strong>Message</strong>)message;<br />

if ( redelivered ){<br />

System.out.println("\n<strong>Message</strong> redelivered, inRollback: "<br />

+ inRollback + " rollbackOnly: " + rollbackOnly );<br />

strmMsg.reset( );<br />

}<br />

if ( strmMsg.propertyExists("SEQUENCE_MARKER") ){<br />

String sequence = strmMsg.getStringProperty( "SEQUENCE_MARKER" );<br />

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

{<br />

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

inRollback = false;<br />

rollbackOnly = false;<br />

99

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

Saved successfully!

Ooh no, something went wrong!