java用timer定时任务执行了一段同步代码,当天同步没有问题,但是到第二天同步的时候就会出现同步多次的问题,有没有遇到类似问题的呢,具体代码如下
public class TaskManager extends HttpServlet {
private static final long serialVersionUID = 1L;
private Log log = LogFactory.createLog();
@Override
public void init() throws ServletException {
long daySpan = 24 * 60 * 60 * 1000;
final SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd 21:12:00”);
Date startTime = null;
System.out.println(“进入init”);
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);
timer.schedule(task, startTime, daySpan);
}
}