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.direct.*; 024 import org.apache.geronimo.samples.daytrader.util.*; 025 026 import org.apache.geronimo.samples.daytrader.*; 027 028 public class TradeBrokerMDB 029 implements MessageDrivenBean, MessageListener { 030 031 032 private transient MessageDrivenContext mdc = null; 033 private Context context; 034 035 private TradeHome tradeHome; 036 037 //Message send/receive timing Statistics 038 private MDBStats mdbStats; 039 private int statInterval = 100; 040 041 public void onMessage(Message message) { 042 try { 043 if (Log.doTrace()) 044 Log.trace("TradeBroker:onMessage -- received message -->" 045 + ((TextMessage)message).getText() + "command-->" 046 + message.getStringProperty("command") + "<--"); 047 048 if (message.getJMSRedelivered()) 049 { 050 Log.log("TradeBrokerMDB: The following JMS message was redelivered due to a rollback:\n"+ ((TextMessage)message).getText() ); 051 //Order has been cancelled -- ignore returned messages 052 return; 053 } 054 String command = message.getStringProperty("command"); 055 if (command==null) 056 { 057 Log.debug("TradeBrokerMDB:onMessage -- received message with null command. Message-->"+message); 058 return; 059 } 060 if (command.equalsIgnoreCase("neworder")) 061 { 062 /* Get the Order ID and complete the Order */ 063 Integer orderID = new Integer(message.getIntProperty("orderID")); 064 boolean twoPhase = message.getBooleanProperty("twoPhase"); 065 boolean direct = message.getBooleanProperty("direct"); 066 long publishTime = message.getLongProperty("publishTime"); 067 long receiveTime = System.currentTimeMillis(); 068 069 070 TradeServices trade = null; 071 072 try { 073 trade = getTrade(direct); 074 075 if (Log.doTrace()) 076 Log.trace("TradeBrokerMDB:onMessage - completing order " + orderID + " twoPhase=" +twoPhase + " direct="+direct); 077 078 trade.completeOrder(orderID, twoPhase); 079 080 TimerStat currentStats = mdbStats.addTiming("TradeBrokerMDB:neworder", publishTime, receiveTime ); 081 082 if ( (currentStats.getCount() % statInterval) == 0) 083 { 084 Log.log(new java.util.Date()+ "\nTradeBrokerMDB: processed 100 stock trading orders. " + 085 "\nCurrent NewOrder Message Statistics\n\tTotal NewOrders process = " + currentStats.getCount() + 086 "\n\tTime to receive messages (in seconds):" + 087 "\n\t\tmin: " +currentStats.getMinSecs()+ 088 "\n\t\tmax: " +currentStats.getMaxSecs()+ 089 "\n\t\tavg: " +currentStats.getAvgSecs()+ 090 "\n\n\n\tThe current order being processed is:\n\t"+((TextMessage)message).getText()); 091 } 092 } 093 catch (Exception e) 094 { 095 Log.error("TradeBrokerMDB:onMessage Exception completing order: " + orderID + "\n", e); 096 mdc.setRollbackOnly(); 097 /* UPDATE - order is cancelled in trade if an error is caught 098 try 099 { 100 trade.cancelOrder(orderID, twoPhase); 101 } 102 catch (Exception e2) 103 { 104 Log.error("order cancel failed", e); 105 }*/ 106 } 107 } 108 else if (command.equalsIgnoreCase("ping")) { 109 if (Log.doTrace()) 110 Log.trace("TradeBrokerMDB:onMessage received test command -- message: " + ((TextMessage)message).getText()); 111 112 long publishTime = message.getLongProperty("publishTime"); 113 long receiveTime = System.currentTimeMillis(); 114 115 TimerStat currentStats = mdbStats.addTiming("TradeBrokerMDB:ping", publishTime, receiveTime ); 116 117 if ( (currentStats.getCount() % statInterval) == 0) 118 { 119 Log.log(new java.util.Date()+ "\nTradeBrokerMDB: received 100 ping messages. " + 120 "\nCurrent Ping Message Statistics\n\tTotal ping message count = " + currentStats.getCount() + 121 "\n\tTime to receive messages (in seconds):" + 122 "\n\t\tmin: " +currentStats.getMinSecs()+ 123 "\n\t\tmax: " +currentStats.getMaxSecs()+ 124 "\n\t\tavg: " +currentStats.getAvgSecs()+ 125 "\n\n\n\tThe current message is:\n\t"+((TextMessage)message).getText()); 126 } 127 } 128 else 129 Log.error("TradeBrokerMDB:onMessage - unknown message request command-->" + command + "<-- message=" + ((TextMessage)message).getText()); 130 } 131 catch (Throwable t) 132 { 133 //JMS onMessage should handle all exceptions 134 Log.error("TradeBrokerMDB: Error rolling back transaction", t); 135 mdc.setRollbackOnly(); 136 } 137 } 138 139 private TradeServices getTrade(boolean direct) 140 throws Exception 141 { 142 TradeServices trade; 143 if (direct) 144 trade = new TradeDirect(); 145 else 146 trade = tradeHome.create(); 147 148 return trade; 149 } 150 151 152 public TradeBrokerMDB() { 153 if (Log.doTrace()) Log.trace("TradeBrokerMDB:TradeBrokerMDB()"); 154 } 155 156 public void setMessageDrivenContext(MessageDrivenContext mdc) { 157 if (Log.doTrace()) Log.trace("TradeBrokerMDB:setMessageDriventContext()"); 158 this.mdc = mdc; 159 } 160 161 public void ejbCreate() { 162 if (Log.doTrace()) Log.trace("TradeBrokerMDB:ejbCreate()"); 163 try { 164 InitialContext ic = new InitialContext(); 165 tradeHome = (TradeHome) ic.lookup("java:comp/env/ejb/Trade"); 166 statInterval = ( (Integer) ic.lookup("java:comp/env/statInterval") ).intValue(); 167 if ( statInterval <= 0 ) statInterval = 100; 168 mdbStats = MDBStats.getInstance(); 169 } catch (Exception e) { 170 Log.error("TradeBrokerMDB:ejbCreate Lookup of Local Entity Homes Failed\n" + e); 171 } 172 } 173 174 public void ejbRemove() { 175 if (Log.doTrace()) Log.trace("TradeBrokerMDB:ejbRemove()"); 176 } 177 178 }