用timer做的一個定時同步資料任務,當天同步一次,但是到第二天定時任務同步了多次,造成資料重復 這個是因為什么呢
具體代碼如下:
public void init() throws ServletException {
long daySpan = 24 * 60 * 60 * 1000;
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 04:00:00");
Date startTime = null;
try {
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sdf.format(new Date()));
} catch (ParseException e) {
e.printStackTrace();
}
if (System.currentTimeMillis() > startTime.getTime()) {
startTime = new Date(startTime.getTime() + daySpan);
}
Timer timer = new Timer("同步職工、部門資訊");
TimerTask task = new TimerTask() {
public void run() {
System.out.println("---------------------------------------batchStart!!");
Connection conn = null;
try {
conn = DBUtil.getConnection();
//開啟同步
T_systemDAO t_systemDAO = new T_systemDAOImpl(conn);
T_systemEntity t_systemEntity = t_systemDAO.selectEntityForUpdate(" where trcode='TT15'");
Integer nowDate = AssetManageUtil.getDate();
if(nowDate == t_systemEntity.getTrdate()) {
//已經處理完成,不進行任何處理
} else {
// //洗掉歷史日志
// DeleteHistoryLogUtil.deleteHistoryLog();
// //同步人員
// ToHrService toHrService = new ToHrService();
// toHrService.getNoTrlStaff();
// toHrService.getAllBosses();
// toHrService = null;
//房屋歸還待辦
BatchWaitThings waitThings = new BatchWaitThings();
waitThings.revertOfficeWaitThings();
//質保期待辦
waitThings.guaranteWaitThings();
ToEmisService emiseService = new ToEmisService();
try {
log.debug("進入同步部門資訊------");
emiseService.synEmisDepInfo();
log.debug("進入同步人員資訊------");
emiseService.synEmisStaffInfo();
} catch (Exception e) {
System.out.println("同步部門、職工出現問題");
e.printStackTrace();
}
if(EnvironmentInitServet.properties.getProperty("connMark").equalsIgnoreCase("Y")) {
File f=new File(EnvironmentInitServet.properties.getProperty("connLogPath"));
if(f.exists()){
f.delete();
}
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//清除日志物件
LogPool.removeAll();
t_systemEntity.setTrdate(nowDate);
t_systemDAO.update(t_systemEntity, " where trcode='TT15'");
DBUtil.commit(conn);
}
} catch (Exception e) {
try {
DBUtil.rollback(conn);
} catch (Exception e2) {
e2.printStackTrace();
}
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
}
};
timer.scheduleAtFixedRate(task, startTime, daySpan);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/141049.html
標籤:Java相關
