/**
* 队列取出数据
*/
public class NewUploadTask implements Runnable {
public void run() {
while (isStarted) {
try {
UploadInfo uploadInfo = uploadList.take();
int num = 10;
if (uploadList.size() < 10) {
num = uploadList.size();
}
List<UploadInfo> tmpTaskList = new ArrayList<UploadInfo>();
tmpTaskList.add(uploadInfo);
for (int i=0;i < num;i++) {
uploadInfo = uploadList.take();
tmpTaskList.add(uploadInfo);
}
/* 事务提交数据 */
boolean isRollback = false;
BaseDao dao = new BaseDao();
try {
dao.beginTrans();
RecordUltrasonicUpdateDao ruDao = new RecordUltrasonicUpdateDao();
ParkingUpdateDao puDao = new ParkingUpdateDao();
ruDao.setTransConn(dao.getTransConn());
puDao.setTransConn(dao.getTransConn());
for (int i=0;i <= num;i++) {
dealAndSaveData(uploadInfo,ruDao,puDao); //调用两个dao的方法:ruDao.insert(xxx);....
}
dao.commitTrans();
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
isRollback = true;
dao.rollbackTrans();
} catch (RuntimeException e) {
log.error(e.getMessage(), e);
isRollback = true;
dao.rollbackTrans();
} catch (Exception e) {
log.error(e.getMessage(), e);
isRollback = true;
dao.rollbackTrans();
} finally{
dao.closeTransConn();
}
/* 插入失败,可能数据库异常 */
if(isRollback) {
uploadList.addAll(tmpTaskList);
}
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
}
}
}
}