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_StoreBuyNow implements MessageDrivenBean , MessageListener 27 { 28 30 private DataSource dataSource; 31 private MessageDrivenContext messageDrivenContext; 32 private TopicConnectionFactory connectionFactory; 33 private TopicConnection connection; 34 private Topic topic; 35 private TopicSession session; 36 private Context initialContext = null; 37 38 39 public MDB_StoreBuyNow() 40 { 41 42 } 43 44 public void onMessage(Message message) 45 { 46 try 47 { 48 MapMessage request = (MapMessage)message; 49 int itemId = request.getInt("itemId"); 50 int userId = request.getInt("userId"); 51 int qty = request.getInt("quantity"); 52 53 createBuyNow(itemId, userId, qty); 55 } 56 catch (Exception e) 57 { 58 throw new EJBException ("Message traitment failed for MDB_StoreBuyNow: " +e); 59 } 60 } 61 62 70 public void createBuyNow(int itemId, int userId, int qty) throws RemoteException 71 { 72 PreparedStatement stmt = null; 73 ResultSet rs = null; 74 Connection conn = null; 75 76 try 78 { 79 String now = TimeManagement.currentDateToString(); 82 int quantity; 83 try 84 { 85 conn = dataSource.getConnection(); 86 stmt = conn.prepareStatement("SELECT quantity, end_date FROM items WHERE id=?"); 87 stmt.setInt(1, itemId); 88 rs = stmt.executeQuery(); 89 } 90 catch (SQLException e) 91 { 92 try { stmt.close(); } catch (Exception ignore) {} 93 try { conn.close(); } catch (Exception ignore) {} 94 throw new RemoteException ("Failed to execute Query for the item: " +e+"<br>"); 95 } 96 PreparedStatement update = null; 97 try 98 { 99 if (rs.first()) 100 { 101 quantity = rs.getInt("quantity"); 102 quantity = quantity -qty; 103 if (quantity == 0) 104 { 105 update = conn.prepareStatement("UPDATE items SET end_date=?,quantity=? WHERE id=?"); 106 update.setString(1, now); 107 update.setInt(2, quantity); 108 update.setInt(3, itemId); 109 update.executeUpdate(); 110 } 111 else 112 { 113 update = conn.prepareStatement("UPDATE items SET quantity=? WHERE id=?"); 114 update.setInt(1, quantity); 115 update.setInt(2, itemId); 116 update.executeUpdate(); 117 } 118 update.close(); 119 } 120 stmt.close(); 121 } 122 catch (Exception e) 123 { 124 try { update.close(); } catch (Exception ignore) {} 125 try { stmt.close(); } catch (Exception ignore) {} 126 try { conn.close(); } catch (Exception ignore) {} 127 throw new RemoteException ("Failed to update item's quantity: " +e+"<br>"); 128 } 129 try 130 { 131 stmt = conn.prepareStatement("INSERT INTO buy_now VALUES (NULL, \""+userId+ 132 "\", \""+itemId+"\", \""+qty+"\", \""+now+"\")"); 133 stmt.executeUpdate(); 134 } 135 catch (Exception e) 136 { 137 try { stmt.close(); } catch (Exception ignore) {} 138 try { conn.close(); } catch (Exception ignore) {} 139 throw new RemoteException ("Failed to create buy_now item: " +e+"<br>"); 140 } 141 if (stmt != null) stmt.close(); 142 if (conn != null) conn.close(); 143 145 } 146 catch (Exception e) 147 { 148 try { stmt.close(); } catch (Exception ignore) {} 149 try { conn.close(); } catch (Exception ignore) {} 150 throw new RemoteException ("Cannot insert the item into buy_now items table: " +e+"<br>"); 154 } 160 } 161 162 164 175 public void setMessageDrivenContext(MessageDrivenContext ctx) 176 { 177 messageDrivenContext = ctx; 178 if (dataSource == null) 179 { 180 try 182 { 183 initialContext = new InitialContext (); 184 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 185 } 186 catch (Exception e) 187 { 188 throw new EJBException ("Cannot get JNDI InitialContext"); 189 } 190 } 191 } 192 193 197 public void ejbCreate() 198 { 199 200 } 201 202 211 public void ejbRemove() {} 212 213 214 215 } 216 | Popular Tags |