求大神指导一下为什么timer定时任务会执行多次

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);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值