1 package async; 2 3 import dinamica.*; 4 import javax.sql.*; 5 import java.sql.*; 6 import java.io.*; 7 8 18 public class BatchProcess implements Runnable 19 { 20 21 String taskID = null; 22 String fileName = null; 23 DataSource ds = null; 24 String sql = null; 25 26 32 public BatchProcess(String taskID, String fileName, DataSource ds, String sql) 33 { 34 this.taskID = taskID; 35 this.fileName = fileName; 36 this.ds = ds; 37 this.sql = sql; 38 } 39 40 43 public void run() 44 { 45 46 Connection conn = null; 47 PreparedStatement pstmt = null; 48 int row = 0; 49 long totalTime = 0; 50 int batchSize = 100; int c = 0; 52 BufferedReader br = null; 53 54 try 55 { 56 57 long t1 = System.currentTimeMillis(); 58 59 conn = ds.getConnection(); 61 62 conn.setAutoCommit(false); 64 65 pstmt = conn.prepareStatement(sql); 67 68 br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "ISO-8859-1")); 70 String line = ""; 71 while (line!=null) 72 { 73 line = br.readLine(); 74 if (line!=null) 75 { 76 row++; 77 c++; 78 79 String values[] = StringUtil.split(line, "\t"); 81 if (values.length!=4) 82 throw new Throwable ("Error parsing row: invalid number of fields per row."); 83 84 Integer orderID = ValidatorUtil.testInteger(values[0]); 86 String customerID = values[1]; 87 java.util.Date orderDate = ValidatorUtil.testDate(values[2],"yyyy-MM-dd"); 88 Double total = ValidatorUtil.testDouble(values[3]); 89 90 if (orderID==null) 92 throw new Throwable ("Error converting field (orderID)."); 93 94 if (customerID==null) 95 throw new Throwable ("Error converting field (customerID)."); 96 97 if (orderDate==null) 98 throw new Throwable ("Error converting field (orderDate)."); 99 100 if (total==null) 101 throw new Throwable ("Error converting field (total)."); 102 103 pstmt.setInt(1, orderID.intValue()); 105 pstmt.setString(2, customerID); 106 pstmt.setDate(3,new java.sql.Date (orderDate.getTime())); 107 pstmt.setDouble(4, total.doubleValue()); 108 pstmt.addBatch(); 109 110 if (c==batchSize) 112 { 113 String msg = "Processing row # " + row; 114 updateTaskStatus(msg, totalTime, 0); 115 c=0; 116 } 117 118 } 119 } 120 121 pstmt.executeBatch(); 123 conn.commit(); 124 125 long t2 = System.currentTimeMillis(); 127 totalTime = t2- t1; 128 129 updateTaskStatus("Task completed. Processed " + row + " rows.", totalTime, 1); 130 131 } 132 catch (Throwable e) 133 { 134 135 if (conn!=null) 136 try {conn.rollback();} catch (SQLException e1){e1.printStackTrace();} 137 138 String msg = e.getMessage() + " - row index:" + row; 139 System.err.print(msg); 140 e.printStackTrace(); 141 142 updateTaskStatus("ABORTED: " + msg, totalTime, 1); 143 144 } 145 finally 146 { 147 if (br!=null) 148 try { br.close(); } catch (IOException e1) {e1.printStackTrace();} 149 150 if (pstmt!=null) 151 try { pstmt.close(); } catch (SQLException e2) {e2.printStackTrace();} 152 153 if (conn!=null) 154 try { conn.close(); } catch (SQLException e3) {e3.printStackTrace();} 155 } 156 157 } 158 159 160 163 public void updateTaskStatus(String msg, long totalTime, int finished) 164 { 165 166 Connection conn = null; 167 PreparedStatement pstmt = null; 168 169 try 170 { 171 172 conn = ds.getConnection(); 174 175 String sql = "update task set status_date = ?, " 177 + "status_time = ?, status_msg = ?, " 178 + "total_time = ?, finished = ? where id = " + taskID; 179 180 pstmt = conn.prepareStatement(sql); 181 182 pstmt.setDate(1, new java.sql.Date (new java.util.Date ().getTime())); 184 pstmt.setString(2, StringUtil.formatDate(new java.util.Date (),"HH:mm:ss")); 185 pstmt.setString(3,msg); 186 pstmt.setLong(4, totalTime); 187 pstmt.setInt(5, finished); 188 189 pstmt.execute(); 191 192 } 193 catch (Throwable e) 194 { 195 e.printStackTrace(); 196 } 197 finally 198 { 199 if (pstmt!=null) 200 try {pstmt.close();} catch (SQLException e1){e1.printStackTrace();} 201 202 if (conn!=null) 203 try {conn.close();} catch (SQLException e2){e2.printStackTrace();} 204 } 205 206 } 207 208 } 209 | Popular Tags |