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.ejb; 018 019 import javax.ejb.*; 020 import javax.jms.*; 021 import javax.naming.*; 022 023 import org.apache.geronimo.samples.daytrader.util.*; 024 025 public class TradeStreamerMDB 026 implements MessageDrivenBean, MessageListener { 027 028 029 private transient MessageDrivenContext mdc = null; 030 private Context context; 031 032 private TradeHome tradeHome; 033 034 //Message send/receive timing Statistics 035 private MDBStats mdbStats; 036 private int statInterval = 100; 037 038 039 public void onMessage(Message message) { 040 try { 041 if (Log.doTrace()) 042 Log.trace("TradeStream:onMessage -- received message -->" 043 + ((TextMessage)message).getText() + "command-->" 044 + message.getStringProperty("command") + "<--"); 045 String command = message.getStringProperty("command"); 046 if (command==null) 047 { 048 Log.debug("TradeStreamerMDB:onMessage -- received message with null command. Message-->"+message); 049 return; 050 } 051 if (command.equalsIgnoreCase("updateQuote")) 052 { 053 if (Log.doTrace()) 054 Log.trace("TradeStreamer:onMessage -- received message -->" 055 + ((TextMessage)message).getText() 056 + "\n\t symbol = " + message.getStringProperty("symbol") 057 + "\n\t current price =" + message.getStringProperty("price") 058 + "\n\t old price =" + message.getStringProperty("oldPrice") 059 ); 060 long publishTime = message.getLongProperty("publishTime"); 061 long receiveTime = System.currentTimeMillis(); 062 063 TimerStat currentStats = mdbStats.addTiming("TradeBrokerStreamer:udpateQuote", publishTime, receiveTime ); 064 065 if ( (currentStats.getCount() % statInterval) == 0) 066 { 067 Log.log(new java.util.Date()+ "\nTradeStreamerMDB: 100 Trade stock prices updated: " + 068 "\nCurrent Statistics\n\tTotal update Quote Price message count = " + currentStats.getCount() + 069 "\n\tTime to receive stock update alerts messages (in seconds):" + 070 "\n\t\tmin: " +currentStats.getMinSecs()+ 071 "\n\t\tmax: " +currentStats.getMaxSecs()+ 072 "\n\t\tavg: " +currentStats.getAvgSecs()+ 073 "\n\n\n\tThe current price update is:\n\t"+((TextMessage)message).getText()) ; 074 } 075 } 076 else if (command.equalsIgnoreCase("ping")) { 077 if (Log.doTrace()) 078 Log.trace("TradeStreamerMDB:onMessage received ping command -- message: " + ((TextMessage)message).getText()); 079 080 long publishTime = message.getLongProperty("publishTime"); 081 long receiveTime = System.currentTimeMillis(); 082 083 TimerStat currentStats = mdbStats.addTiming("TradeStreamerMDB:ping", publishTime, receiveTime ); 084 085 if ( (currentStats.getCount() % statInterval) == 0) 086 { 087 Log.log(new java.util.Date()+ "\nTradeStreamerMDB: received 100 ping messages. " + 088 "\nCurrent Ping Message Statistics\n\tTotal ping message count = " + currentStats.getCount() + 089 "\n\tTime to receive messages (in seconds):" + 090 "\n\t\tmin: " +currentStats.getMinSecs()+ 091 "\n\t\tmax: " +currentStats.getMaxSecs()+ 092 "\n\t\tavg: " +currentStats.getAvgSecs()+ 093 "\n\n\n\tThe current message is:\n\t"+((TextMessage)message).getText()); 094 } 095 } 096 else 097 Log.error("TradeStreamerMDB:onMessage - unknown message request command-->" + command + "<-- message=" + ((TextMessage)message).getText()); 098 } 099 catch (Throwable t) 100 { 101 //JMS onMessage should handle all exceptions 102 Log.error("TradeStreamerMDB: Exception", t); 103 //UPDATE - Not rolling back for now -- so error messages are not redelivered 104 //mdc.setRollbackOnly(); 105 } 106 } 107 108 109 public TradeStreamerMDB() { 110 if (Log.doTrace()) Log.trace("TradeStreamerMDB:TradeStreamerMDB()"); 111 } 112 113 public void setMessageDrivenContext(MessageDrivenContext mdc) { 114 if (Log.doTrace()) Log.trace("TradeStreamerMDB:setMessageDriventContext()"); 115 this.mdc = mdc; 116 } 117 118 public void ejbCreate() { 119 if (Log.doTrace()) Log.trace("TradeStreamerMDB:ejbCreate()"); 120 try { 121 InitialContext ic = new InitialContext(); 122 statInterval = ( (Integer) ic.lookup("java:comp/env/statInterval") ).intValue(); 123 if ( statInterval <= 0 ) statInterval = 100; 124 mdbStats = MDBStats.getInstance(); 125 } catch (Exception e) { 126 Log.error("TradeStreamerMDB:ejbCreate Lookup of EJB environment failed\n" + e); 127 } 128 } 129 130 public void ejbRemove() { 131 if (Log.doTrace()) Log.trace("TradeStreamerMDB:ejbRemove()"); 132 } 133 134 }