FileDocCategorySizeDatePackage
QWholesaler.javaAPI DocExample7883Thu Feb 01 14:39:26 GMT 2001chap5.b2b

QWholesaler

public class QWholesaler extends Object implements javax.jms.MessageListener

Fields Summary
private javax.jms.TopicConnection
tConnect
private javax.jms.TopicSession
tSession
private javax.jms.TopicPublisher
tPublisher
private javax.jms.QueueConnection
qConnect
private javax.jms.QueueSession
qSession
private javax.jms.Queue
receiveQueue
private javax.jms.Topic
hotDealsTopic
private javax.jms.TemporaryTopic
buyOrdersTopic
Constructors Summary
public QWholesaler(String broker, String username, String password)


          
      try{
        TopicConnectionFactory tFactory = null;
        QueueConnectionFactory qFactory = null;
        InitialContext jndi = null;

        Properties env = new Properties();
        // ... specify the JNDI properties specific to the JNDI SPI being used
        env.put("BROKER", broker);
        jndi = new InitialContext(env);

        tFactory =
            (TopicConnectionFactory)jndi.lookup("TopicConnectionFactory");
        qFactory =
            (QueueConnectionFactory)jndi.lookup("QueueConnectionFactory");
        tConnect = tFactory.createTopicConnection (username, password);
        qConnect = qFactory.createQueueConnection (username, password);
        tConnect.setClientID(username + "-topic" );
        qConnect.setClientID(username + "-queue" );

        tSession =
            tConnect.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
        qSession =
            qConnect.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);

        hotDealsTopic = (Topic)jndi.lookup("Hot Deals");
        receiveQueue = (Queue)jndi.lookup("Reply Q");

        tPublisher = tSession.createPublisher(hotDealsTopic);

        QueueReceiver qReceiver = qSession.createReceiver(receiveQueue);
        qReceiver.setMessageListener(this);

        // Now that setup is complete, start the Connection
        qConnect.start();
        tConnect.start();

      }catch (javax.jms.JMSException jmse){
         jmse.printStackTrace(); System.exit(1);
      }catch(javax.naming.NamingException jne){
         jne.printStackTrace(); System.exit(1);
      }
   
Methods Summary
public voidexit()

      try{
        tConnect.close();
        qConnect.close();
      }catch (javax.jms.JMSException jmse){
        jmse.printStackTrace();
      }
      System.exit(0);
   
public static voidmain(java.lang.String[] argv)

      String broker, username, password;
      if(argv.length == 3){
         broker = argv[0];
         username = argv[1];
         password = argv[2];
      }else{
         System.out.println("Invalid arguments. Should be: ");
         System.out.println("java QWholesaler broker username password");
         return;
      }

      QWholesaler wholesaler = new QWholesaler(broker, username, password);

      try{
         // Read all standard input and send it as a message.
         java.io.BufferedReader stdin = new java.io.BufferedReader
            (new java.io.InputStreamReader( System.in ) );
         System.out.println ("Enter: Item, Old Price, New Price ");
         System.out.println("\ne.g. Bowling Shoes, 100.00, 55.00");

         while ( true ){
            String dealDesc = stdin.readLine();
            if(dealDesc != null && dealDesc.length() > 0){
                // parse the deal description
                String itemDesc = null;
                String temp = null;
                float oldPrice = 0;
                float newPrice = 0;
                try {
                    StringTokenizer tokenizer = 
                        new StringTokenizer(dealDesc,",") ;
                    itemDesc = tokenizer.nextToken();
                    temp = tokenizer.nextToken();
                    oldPrice = 
                        Float.valueOf(temp.trim()).floatValue();
                    temp = tokenizer.nextToken();
                    newPrice = 
                        Float.valueOf(temp.trim()).floatValue();
                } catch ( java.util.NoSuchElementException e ) {
                    System.err.println( "Cannot parse deal descriptor " + 
                                        dealDesc );
                    continue;
                } catch ( NumberFormatException e ) {
                    System.err.println( "Cannot parse deal descriptor " + 
                                        dealDesc );
                    continue;
                } 

               wholesaler.publishPriceQuotes(dealDesc,username,
                                             itemDesc, oldPrice,newPrice);
            }else{
                wholesaler.exit();
            }
         }
      }catch( java.io.IOException ioe ){
         ioe.printStackTrace();
      }
   
public voidonMessage(javax.jms.Message message)

      try{
         TextMessage textMessage = (TextMessage) message;
         String text = textMessage.getText();
         System.out.println("Order received - "+text+
                            " from " + message.getJMSCorrelationID());
      }catch (java.lang.Exception rte){
         rte.printStackTrace();
      }
   
private voidpublishPriceQuotes(java.lang.String dealDesc, java.lang.String username, java.lang.String itemDesc, float oldPrice, float newPrice)

      try{
        javax.jms.StreamMessage message = tSession.createStreamMessage();
        message.writeString(dealDesc);
        message.writeString(itemDesc);
        message.writeFloat(oldPrice);
        message.writeFloat(newPrice);

        message.setStringProperty("Username", username);
        message.setStringProperty("itemDesc", itemDesc);

        message.setJMSReplyTo(receiveQueue);

        tPublisher.publish(
            message,
            javax.jms.DeliveryMode.PERSISTENT,
            javax.jms.Message.DEFAULT_PRIORITY,
            1800000);
      }catch ( javax.jms.JMSException jmse ){
         jmse.printStackTrace();
      }