001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.samples.daytrader.client;
018    
019    
020    import javax.naming.InitialContext;
021    import javax.jms.*;
022    
023    import org.apache.geronimo.samples.daytrader.util.*;
024    
025    import java.math.*;
026    
027    public class TradeClientMessageListener implements MessageListener {
028            private TradeClient client;
029            private boolean useENC;
030    
031            public TradeClientMessageListener(TradeClient client, boolean useENC) {
032                    this.client = client;
033                    this.useENC = useENC;
034            }
035    
036            public void subscribe() {
037                    try     {
038                            System.out.println("TradeStreamer getInitial Context");
039                            InitialContext context = client.getInitialContext();
040                            
041                            Log.trace("TradeStreamer pub/sub JNDI starting");
042                            ConnectionFactory connFactory;
043                            if (useENC) {
044                                    connFactory = (ConnectionFactory) context.lookup("java:comp/env/jms/TopicConnectionFactory");
045                            }
046                            else {
047                                    connFactory = (ConnectionFactory) context.lookup("jms/TopicConnectionFactory");
048                            }
049    
050                            Topic streamerTopic;
051                            if (useENC) {
052                                    streamerTopic = (Topic) context.lookup("java:comp/env/jms/TradeStreamerTopic");
053                            }
054                            else {
055                                    streamerTopic = (Topic) context.lookup("jms/TradeStreamerTopic");
056                            }
057    
058                            Log.trace("TradeStreamer pub/sub JNDI ending");
059    
060                            Connection conn = connFactory.createConnection();
061                            Log.trace("TradeStreamer pub/sub after create Topic");
062                            Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
063                            Log.trace("TradeStreamer pub/sub after create session");
064                            MessageConsumer consumer = sess.createConsumer(streamerTopic);
065                            Log.trace("TradeStreamer pub/sub after create sub");
066                            conn.start();
067                            Log.trace("TradeStreamer pub/sub after tconn startc");
068                            consumer.setMessageListener(this);
069                            Log.trace("TradeStreamer pub/sub after set message listener");
070                            
071                            consumer = sess.createConsumer(streamerTopic);
072                            Log.trace("TradeStreamer pub/sub listener registered successfully");
073                    }
074                    catch (Exception e)     {
075                            Log.error("TradeStreamer Subscribe Exception: " + e);
076                    }
077            }
078    
079            public void onMessage(Message message) {
080                    TextMessage msg = null;
081                    try     {
082                            if (message instanceof TextMessage) {
083                                    msg = (TextMessage) message;
084    
085                                    long recvTime = System.currentTimeMillis();
086                                    String command = message.getStringProperty("command");
087                                    
088                                    if ( (command !=null) && (command.equalsIgnoreCase("updateQuote")) ) {
089                                            String symbol = message.getStringProperty("symbol");
090                                            String company = message.getStringProperty("company");
091                                            BigDecimal price = new BigDecimal(message.getStringProperty("price"));                          
092                                            BigDecimal oldPrice = new BigDecimal(message.getStringProperty("oldPrice"));                                                    
093                                            BigDecimal open = new BigDecimal(message.getStringProperty("open"));
094                                            BigDecimal low = new BigDecimal(message.getStringProperty("low"));
095                                            BigDecimal high = new BigDecimal(message.getStringProperty("high"));
096                                            double volume = message.getDoubleProperty("volume");            
097                                    
098                                            BigDecimal changeFactor = new BigDecimal(message.getStringProperty("changeFactor"));            
099                                            double sharesTraded = message.getDoubleProperty("sharesTraded");                                
100                                            long publishTime = message.getLongProperty("publishTime");
101    
102                                            TradeQuoteAuditStats stats = client.getAuditStats();
103                                            BigDecimal priceDiff = price.subtract(oldPrice);
104                                            stats.updateSymbol(symbol, company, price, open, low, high, volume, publishTime, priceDiff, sharesTraded);
105                                            /*                      
106                                                                    jTextArea1.append("Streamer--> " + new java.util.Date() + " Stock: " + symbol  + " ");
107                                                                    BigDecimal diff = price.subtract(oldPrice);
108                                                                    String direction = null;
109                                                                    if (diff.doubleValue() < 0.0)
110                                                                    direction = "DOWN";
111                                                                    else
112                                                                    direction = "UP";
113                                    
114                                                                    jTextArea1.append(price + " (" + diff + ") " + direction  + "\n");
115                                            */
116                                    }
117                                    else {
118                                            System.out.println("msg = " + msg + " action = " + msg.getStringProperty("action"));
119                                            client.updateStatusMessage(msg.getText());
120                                    }
121                            }
122                            else {
123                                    System.out.println("Message of wrong type: " + message.getClass().getName());
124                            }
125                    }
126                    catch (JMSException e) {
127                            System.out.println("JMSException in onMessage(): " + e.toString());
128                    }
129                    catch (Throwable t) {
130                            System.out.println("Exception in onMessage():" + t.getMessage());
131                    }
132            }
133    }