Chapter 23
900
public void start() throws NamingException, JMSException, IOException
{
setup();
// Start the connections
topicConnection.start();
queueConnection.start();
// Let the user enter stock symbols and prices to send as quotes until he
presses q or Q
while(true){
System.out.print("Enter a stock symbol\nq)uit\n:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String msg = in.readLine();
System.out.println();
if(msg.equalsIgnoreCase("q")){
break; // All done
}
else{
String stockSymbol = msg;
System.out.print("enter the price:");
int stockPrice = Integer.parseInt(in.readLine());
publishStockQuote(stockSymbol,stockPrice);
}
}
// Close connections
topicConnection.close();
queueConnection.close();
}
public static void main(String[] args) throws Exception
{
StockGuy broker = new StockGuy();
broker.start();
}
}
The PurchaseGuy Class
Next, let's look at the PurchaserGuy. The PurchaserGuy is both a subscriber to the
StockQuotesTopic and a sender on the PurchaseOrdersQueue. When the PurchaserGuy first
starts up, the user has the option to enter the symbols of stocks they wish to receive price quotes for:
package JMSExample;
import common.*;
import java.io.*;
import javax.jms.*;
import javax.naming.*;
public class PurchaserGuy implements MessageListener
{
private TopicConnection topicConnection = null;
private TopicSession topicSession = null;