1 package edu.rice.rubis.beans; 2 3 import java.rmi.RemoteException ; 4 import javax.ejb.MessageDrivenBean ; 5 import javax.ejb.MessageDrivenContext ; 6 import javax.ejb.EJBException ; 7 import javax.jms.*; 8 import javax.naming.Context ; 9 import javax.naming.InitialContext ; 10 import javax.rmi.PortableRemoteObject ; 11 import javax.sql.DataSource ; 12 import java.sql.Connection ; 13 import java.sql.PreparedStatement ; 14 import java.sql.ResultSet ; 15 import java.sql.SQLException ; 16 import java.io.Serializable ; 17 import javax.transaction.UserTransaction ; 18 19 25 26 public class MDB_StoreBid implements MessageDrivenBean , MessageListener 27 { 28 29 31 private DataSource dataSource; 32 private MessageDrivenContext messageDrivenContext; 33 private TopicConnectionFactory connectionFactory; 34 private TopicConnection connection; 35 private Topic topic; 36 private TopicSession session; 37 private Context initialContext = null; 38 39 40 public MDB_StoreBid() 41 { 42 43 } 44 45 public void onMessage(Message message) 46 { 47 try 48 { 49 MapMessage request = (MapMessage)message; 50 int userId = request.getInt("userId"); 51 int itemId = request.getInt("itemId"); 52 float bid = request.getFloat("bid"); 53 float maxBid = request.getFloat("maxBid"); 54 int qty = request.getInt("quantity"); 55 56 createBid(userId, itemId, bid, maxBid, qty); 58 } 59 catch (Exception e) 60 { 61 throw new EJBException ("Message traitment failed for MDB_StoreBid: " +e); 62 } 63 } 64 65 66 76 public void createBid(int userId, int itemId, float bid, float maxBid, int qty) throws RemoteException 77 { 78 PreparedStatement stmt = null; 79 ResultSet rs = null; 80 Connection conn = null; 81 82 try 84 { 85 try 87 { 88 conn = dataSource.getConnection(); 90 String now = TimeManagement.currentDateToString(); 91 stmt = conn.prepareStatement("INSERT INTO bids VALUES (NULL, \""+userId+ 92 "\", \""+itemId+"\", \""+qty+"\", \""+ 93 bid+"\", \""+maxBid+"\", \""+now+"\")"); 94 stmt.executeUpdate(); 95 stmt.close(); 96 } 97 catch (SQLException e) 98 { 99 try { stmt.close(); } catch (Exception ignore) {} 100 try { conn.close(); } catch (Exception ignore) {} 101 throw new RemoteException ("Error while storing the bid (got exception: " +e+")<br>"); 102 } 103 PreparedStatement update = null; 105 try 106 { 107 stmt = conn.prepareStatement("SELECT nb_of_bids, max_bid FROM items WHERE id=?"); 108 stmt.setInt(1, itemId); 109 rs = stmt.executeQuery(); 110 if (rs.first()) 111 { 112 int nbOfBids = rs.getInt("nb_of_bids"); 113 nbOfBids++; 114 float oldMaxBid = rs.getFloat("max_bid"); 115 if (bid > oldMaxBid) 116 { 117 oldMaxBid = bid; 118 update = conn.prepareStatement("UPDATE items SET max_bid=?,nb_of_bids=? WHERE id=?"); update.setFloat(1, oldMaxBid); 120 update.setInt(2, nbOfBids); 121 update.setInt(3, itemId); 122 update.executeUpdate(); 123 } 124 else 125 { 126 update = conn.prepareStatement("UPDATE items SET nb_of_bids=? WHERE id=?"); 127 update.setInt(1, nbOfBids); 128 update.setInt(2, itemId); 129 update.executeUpdate(); 130 } 131 update.close(); 132 } 133 stmt.close(); 134 } 135 catch (Exception ex) 136 { 137 try { update.close(); } catch (Exception ignore) {} 138 try { stmt.close(); } catch (Exception ignore) {} 139 try { conn.close(); } catch (Exception ignore) {} 140 throw new RemoteException ("Failed to update nb of bids and max bid: " + ex); 141 } 142 if (conn != null) conn.close(); 143 } 145 catch (Exception e) 146 { 147 try { conn.close(); } catch (Exception ignore) {} 148 throw new RemoteException ("Failed to create a new bid (got exception: " +e+")<br>"); 152 } 158 } 159 160 161 163 174 public void setMessageDrivenContext(MessageDrivenContext ctx) 175 { 176 messageDrivenContext = ctx; 177 if (dataSource == null) 178 { 179 try 181 { 182 initialContext = new InitialContext (); 183 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 184 } 185 catch (Exception e) 186 { 187 throw new EJBException ("Cannot get JNDI InitialContext"); 188 } 189 } 190 } 191 192 196 public void ejbCreate() 197 { 198 199 } 200 201 210 public void ejbRemove() {} 211 212 213 } 214 | Popular Tags |