FileDocCategorySizeDatePackage
QRetailer.javaAPI DocExample7429Thu Feb 01 14:39:26 GMT 2001chap5.b2b

QRetailer

public class QRetailer extends Object implements javax.jms.MessageListener

Fields Summary
private javax.jms.QueueConnection
qConnect
private javax.jms.QueueSession
qSession
private javax.jms.QueueSender
qSender
private javax.jms.TopicConnection
tConnect
private javax.jms.TopicSession
tSession
private javax.jms.Topic
hotDealsTopic
private javax.jms.TopicSubscriber
tsubscriber
private static String
uname
Constructors Summary
public QRetailer(String broker, String username, String password)


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

            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,
                    javax.jms.Session.AUTO_ACKNOWLEDGE);

            hotDealsTopic = (Topic)jndi.lookup("Hot Deals");
            hotDealsTopic = tSession.createTopic ("Hot Deals");

            tsubscriber = tSession.createDurableSubscriber(hotDealsTopic,
                    "Hot Deals Subscription");
            tsubscriber.setMessageListener(this);
            tConnect.start();

        }catch (javax.jms.JMSException jmse){
            jmse.printStackTrace();
            System.exit(1);
        }catch(javax.naming.NamingException jne){
         jne.printStackTrace(); System.exit(1);
        }
    
Methods Summary
private voidautoBuy(javax.jms.Message message)

        try{
            StreamMessage strmMsg = (StreamMessage)message;
            String dealDesc = strmMsg.readString();
            String itemDesc = strmMsg.readString();
            float oldPrice = strmMsg.readFloat();
            float newPrice = strmMsg.readFloat();
            System.out.println("Received Hot Buy :"+dealDesc);

            // if price reduction greater than 10 percent, buy
            if (newPrice == 0 || oldPrice / newPrice > 1.1){
                int count = (int)(java.lang.Math.random() * (double)1000);
                System.out.println ("\nBuying " + count + " " + itemDesc);

                TextMessage textMsg = tSession.createTextMessage();
                textMsg.setText(count + " " + itemDesc );
                textMsg.setIntProperty("QTY", count);

                textMsg.setJMSCorrelationID(uname);

                Queue buyQueue = (Queue)message.getJMSReplyTo();

	            qSender = qSession.createSender(buyQueue);
                qSender.send( textMsg,
                                javax.jms.DeliveryMode.PERSISTENT,
                                javax.jms.Message.DEFAULT_PRIORITY,
                                1800000);
            }else{
                System.out.println ("\nBad Deal.  Not buying");
            }
        }catch (javax.jms.JMSException jmse){
            jmse.printStackTrace();
        }
    
private voidexit(java.lang.String s)

        try {
            if ( s != null &&
                s.equalsIgnoreCase("unsubscribe"))
            {
                tsubscriber.close();
                tSession.unsubscribe("Hot Deals Subscription");
            }
            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 QRetailer broker username password");
            return;
        }

        QRetailer retailer  = new QRetailer(broker, username, password);

        try{
            System.out.println("\nRetailer application started.\n");
            // Read all standard input and send it as a message.
            java.io.BufferedReader stdin =
                new java.io.BufferedReader
                ( new java.io.InputStreamReader( System.in ) );
            while ( true ){
                String s = stdin.readLine();
                if ( s == null || s.length() == 0 )retailer.exit(null);
                else if ( s.equalsIgnoreCase("unsubscribe") )
                    retailer.exit ( s );
            }
        }catch ( java.io.IOException ioe ){
            ioe.printStackTrace();
        }
    
public voidonMessage(javax.jms.Message aMessage)

        try{
            autoBuy(aMessage);
        }catch (java.lang.RuntimeException rte){
            rte.printStackTrace();
        }