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 />

uses a JMS transaction either to place all of the orders (commit) or to cancel them<br />

(rollback).<br />

Before getting into the details of the example, run the version of the wholesaler and retailer<br />

applications in this chapter. Start the Wholesaler and Retailer applications in separate<br />

command windows:<br />

java chap6.Wholesaler localhost username password<br />

java chap6.Retailer localhost username password<br />

First, we will study the case in which all the prices are acceptable. We start with acceptable<br />

price reductions for both items. When prompted by the Wholesaler application, enter:<br />

Surfboards, 999.99, 499.99<br />

Wetsuits, 299.99, 149.99<br />

When you enter these new prices, the Retailer should indicate that it is purchasing 1000<br />

surfboards. You shouldn't see anything in the Wholesaler window to indicate that it<br />

received the order, because the Retailer is using a transacted session to send the "Buy<br />

Order" messages. The Wholesaler won't receive the message until the Retailer commits<br />

the session. Now go back to the Wholesaler and type "end".<br />

While the Retailer is using transactions in this example, the Wholesaler is not. When you<br />

type in the word "end" on the command line, Wholesaler sends a message to Retailer<br />

indicating that it is finished with its group of messages, as shown in this excerpt from<br />

Wholesaler's command-line processing loop:<br />

...<br />

while ( true ){<br />

String dealDesc = stdin.readLine( );<br />

if ( dealDesc != null && dealDesc.length( ) > 0 ){<br />

if ( dealDesc.substring(0,3).equalsIgnoreCase("END") ){<br />

wholesaler.sendSequenceMarker( "END_SEQUENCE" );<br />

} else {<br />

...<br />

Wholesaler.sendSequenceMarker( ) simply creates a message, sets a property on it, and<br />

publishes it:<br />

private void sendSequenceMarker(String sequenceMarker){<br />

try {<br />

javax.jms.Stream<strong>Message</strong> message = session.createStream<strong>Message</strong>( );<br />

message.setStringProperty("SEQUENCE_MARKER",sequenceMarker);<br />

}<br />

publisher.publish(<br />

message,<br />

javax.jms.DeliveryMode.PERSISTENT,<br />

javax.jms.<strong>Message</strong>.DEFAULT_PRIORITY,<br />

1800000);<br />

} catch ( javax.jms.JMSException jmse ){<br />

jmse.printStackTrace( );<br />

}<br />

This message has no body. We set the user-defined property "SEQUENCE_MARKER", which the<br />

receiving application uses to know when the group of "Hot Deals" messages is completed.<br />

98

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

Saved successfully!

Ooh no, something went wrong!