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

QWholesaler and QRetailer applications in separate command windows using the<br />

following command-line options:<br />

java chap6.B2B.QWholesaler localhost username password<br />

java chap6.B2B.QRetailer localhost username password<br />

In the QWholesaler application, enter the following two lines at the prompt (Item<br />

description, Old Price, New Price):<br />

Surfboards, 999.99, 499.99<br />

Wetsuits, 299.99, 149.99<br />

The QRetailer application does not respond with a "Buy Order" until you hit the Enter key<br />

on the second line-item, because QRetailer knows that it can't buy 1000 surfboards<br />

without also buying 1000 wetsuits to go with them. Here is the code for the autoBuy( )<br />

method, which contains this logic:<br />

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

int count = 1000;<br />

try {<br />

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

String dealDesc = strmMsg.readString( );<br />

String itemDesc = strmMsg.readString( );<br />

float oldPrice = strmMsg.readFloat( );<br />

float newPrice = strmMsg.readFloat( );<br />

System.out.println( "Received Hot Buy: "+dealDesc );<br />

// "saveDesc" is our "saved" data<br />

if ( saveDesc == null )<br />

{<br />

if (message.getJMSRedelivered( ))<br />

processCompensatingTransaction( );<br />

processInterim<strong>Message</strong>s( itemDesc );<br />

return;<br />

}<br />

// If price reduction is greater than 10 percent, buy<br />

if ((newPrice == 0 || oldPrice / newPrice > 1.1)){<br />

Text<strong>Message</strong> textMsg = tsession.createText<strong>Message</strong>( );<br />

textMsg.setText(count + " " + saveDesc + ", "<br />

+ count + " " + itemDesc );<br />

textMsg.setJMSCorrelationID("DurableRetailer");<br />

Queue buyQueue = (Queue)message.getJMSReplyTo( );<br />

System.out.println ("\nBuying " + count + " "<br />

+ saveDesc + " " + count + " " + itemDesc);<br />

qsender = qsession.createSender(buyQueue);<br />

qsender.send( textMsg,<br />

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

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

1800000);<br />

// Acknowledge the original message<br />

try {<br />

System.out.println("\nAcknowledging messages");<br />

message.acknowledge( );<br />

System.out.println("\n<strong>Message</strong> acknowledged");<br />

saveDesc = null;<br />

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

System.out.println("\nAcknowledgment failed." +<br />

"\nProcessing compensating transaction for "+<br />

92

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

Saved successfully!

Ooh no, something went wrong!