KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > async > BatchProcess


1 package async;
2
3 import dinamica.*;
4 import javax.sql.*;
5 import java.sql.*;
6 import java.io.*;
7
8 /**
9  * BatchProcess<br>
10  * Background process that reads records from flat file and
11  * inserts them in batch mode into a database table, while
12  * updating task status in a specific table for this purpose.
13  * <br><br>
14  * Creation date: 27/12/2004<br>
15  * http://www.martincordova.com<br>
16  * @author mcordova - dinamica@martincordova.com
17  */

18 public class BatchProcess implements Runnable JavaDoc
19 {
20
21     String JavaDoc taskID = null;
22     String JavaDoc fileName = null;
23     DataSource ds = null;
24     String JavaDoc sql = null;
25
26     /**
27      * Constructor
28      * @param taskID ID of the task record
29      * @param fileName Filename (full path) that contains the data to be imported
30      * @param dataSourceName DataSource name
31      */

32     public BatchProcess(String JavaDoc taskID, String JavaDoc fileName, DataSource ds, String JavaDoc sql)
33     {
34         this.taskID = taskID;
35         this.fileName = fileName;
36         this.ds = ds;
37         this.sql = sql;
38     }
39
40     /* (non-Javadoc)
41      * @see java.lang.Runnable#run()
42      */

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; //update status each N rows processed
51
int c = 0;
52         BufferedReader br = null;
53         
54         try
55         {
56
57             long t1 = System.currentTimeMillis();
58             
59             //get db connection
60
conn = ds.getConnection();
61             
62             //begin trans
63
conn.setAutoCommit(false);
64             
65             //prepare statement
66
pstmt = conn.prepareStatement(sql);
67             
68             //read file line by line
69
br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "ISO-8859-1"));
70             String JavaDoc line = "";
71             while (line!=null)
72             {
73                 line = br.readLine();
74                 if (line!=null)
75                 {
76                     row++;
77                     c++;
78
79                     //parse line and get field values
80
String JavaDoc values[] = StringUtil.split(line, "\t");
81                     if (values.length!=4)
82                         throw new Throwable JavaDoc("Error parsing row: invalid number of fields per row.");
83                     
84                     //convert values into Java data types
85
Integer JavaDoc orderID = ValidatorUtil.testInteger(values[0]);
86                     String JavaDoc customerID = values[1];
87                     java.util.Date JavaDoc orderDate = ValidatorUtil.testDate(values[2],"yyyy-MM-dd");
88                     Double JavaDoc total = ValidatorUtil.testDouble(values[3]);
89
90                     //validate native values
91
if (orderID==null)
92                         throw new Throwable JavaDoc("Error converting field (orderID).");
93
94                     if (customerID==null)
95                         throw new Throwable JavaDoc("Error converting field (customerID).");
96
97                     if (orderDate==null)
98                         throw new Throwable JavaDoc("Error converting field (orderDate).");
99
100                     if (total==null)
101                         throw new Throwable JavaDoc("Error converting field (total).");
102                     
103                     //add to batch
104
pstmt.setInt(1, orderID.intValue());
105                     pstmt.setString(2, customerID);
106                     pstmt.setDate(3,new java.sql.Date JavaDoc(orderDate.getTime()));
107                     pstmt.setDouble(4, total.doubleValue());
108                     pstmt.addBatch();
109                     
110                     //update status if required
111
if (c==batchSize)
112                     {
113                         String JavaDoc msg = "Processing row # " + row;
114                         updateTaskStatus(msg, totalTime, 0);
115                         c=0;
116                     }
117                     
118                 }
119             }
120             
121             //commit
122
pstmt.executeBatch();
123             conn.commit();
124             
125             //calculate total time
126
long t2 = System.currentTimeMillis();
127             totalTime = t2- t1;
128
129             updateTaskStatus("Task completed. Processed " + row + " rows.", totalTime, 1);
130
131         }
132         catch (Throwable JavaDoc e)
133         {
134             
135             if (conn!=null)
136                 try {conn.rollback();} catch (SQLException e1){e1.printStackTrace();}
137                 
138             String JavaDoc 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     /* (non-Javadoc)
161      * @see java.lang.Runnable#run()
162      */

163     public void updateTaskStatus(String JavaDoc msg, long totalTime, int finished)
164     {
165
166         Connection conn = null;
167         PreparedStatement pstmt = null;
168         
169         try
170         {
171
172             //get db connection
173
conn = ds.getConnection();
174             
175             //prepare statement
176
String JavaDoc 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             //add to batch
183
pstmt.setDate(1, new java.sql.Date JavaDoc(new java.util.Date JavaDoc().getTime()));
184             pstmt.setString(2, StringUtil.formatDate(new java.util.Date JavaDoc(),"HH:mm:ss"));
185             pstmt.setString(3,msg);
186             pstmt.setLong(4, totalTime);
187             pstmt.setInt(5, finished);
188             
189             //commit
190
pstmt.execute();
191             
192         }
193         catch (Throwable JavaDoc 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