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.

}<br />

System.out.println ("\nBad Deal- Not buying.");<br />

}<br />

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

jmse.printStackTrace( );<br />

}<br />

}<br />

private void exit(String s){<br />

try {<br />

if ( s != null &&<br />

s.equalsIgnoreCase("unsubscribe"))<br />

{<br />

subscriber.close( );<br />

session.unsubscribe("Hot Deals Subscription");<br />

}<br />

connect.close( );<br />

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

jmse.printStackTrace( );<br />

}<br />

System.exit(0);<br />

}<br />

public static void main(String argv[]) {<br />

String broker, username, password;<br />

if (argv.length == 3){<br />

broker = argv[0];<br />

username = argv[1];<br />

password = argv[2];<br />

} else {<br />

System.out.println("Invalid arguments. Should be: ");<br />

System.out.println<br />

("java Retailer broker username password");<br />

return;<br />

}<br />

}<br />

Retailer retailer = new Retailer(broker, username, password);<br />

try {<br />

System.out.println("\nRetailer application started.\n");<br />

// Read all standard input and send it as a message.<br />

java.io.BufferedReader stdin =<br />

new java.io.BufferedReader<br />

( new java.io.InputStreamReader( System.in ) );<br />

while ( true ){<br />

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

if ( s == null )retailer.exit(null);<br />

else if ( s.equalsIgnoreCase("unsubscribe") )<br />

retailer.exit ( s );<br />

}<br />

} catch ( java.io.IOException ioe ){<br />

ioe.printStackTrace( );<br />

}<br />

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

The main( ) method of Retailer is much like the main( ) method of Wholesaler. It creates<br />

an instance of the Retailer class and passes it the information it needs to set up its<br />

publishers and subscribers.<br />

The constructor of the Retailer class is also similar to that of the Wholesaler class, except<br />

that it creates a durable subscription using the "Hot Deals" topic. Durable subscriptions<br />

will be discussed in more detail later in this section.<br />

59

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

Saved successfully!

Ooh no, something went wrong!