@Scheduled(cron = "${cs.task.cron.custtag.sync:0 0/15 * * * ? }") public void localRun() { Date beginDate = new Date(); log.info("localRun start currentTime={}", beginDate); String currentDate = DateUtil.formatDateYMDH(beginDate);

String lockKey = RedisKeyConstants.LOCK_KEY_PREFIX_ + "cust_tag_sync:" + currentDate;
Long currentSplit = RedisUtil.incrBy(lockKey, 1, 60);
try {
    TimeUnit.SECONDS.sleep(30);
    runByOneDay(beginDate, currentSplit, NumberUtils.createLong(RedisUtil.get(lockKey)));
} catch (InterruptedException e) {
    log.error("本地定时任务获取分片休眠失败", e);
    // 重新获取分片
    currentSplit = RedisUtil.incrBy(lockKey, 1, 60);
    runByOneDay(beginDate, currentSplit, NumberUtils.createLong(RedisUtil.get(lockKey)));
}

}

以上是修改后的代码,主要是在捕获InterruptedException异常后重新获取分片并继续执行任务。这样即使休眠失败,也不会漏掉分片的数据。

解决定时任务休眠失败导致数据丢失问题:分片机制优化

原文地址: https://www.cveoy.top/t/topic/ozx3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录