| | |
| | | import java.time.ZonedDateTime; |
| | | import java.time.temporal.ChronoUnit; |
| | | import java.util.*; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.locks.ReentrantLock; |
| | | |
| | |
| | | @Autowired |
| | | private DataSourceConfig dataSourceConfig; |
| | | |
| | | private final ReentrantLock reentrantLock = new ReentrantLock(); |
| | | private final Map<String, ReentrantLock> lockMap = new ConcurrentHashMap<>(); |
| | | |
| | | // 用来跟踪已调度任务的用户ID集合,避免重复添加任务 |
| | | private final Set<String> scheduledTasks = new HashSet<>(); |
| | |
| | | |
| | | // 创建定时任务的方法,用户ID不存在任务时才会添加 |
| | | public void createTask(DictHosp dictHosp) { |
| | | log.info("进入创建任务方法 ->{}",dictHosp.getCode()); |
| | | log.info("已有任务集合 ->{}", scheduledTasks); |
| | | // 如果任务已经存在,则不重复添加 |
| | | if (scheduledTasks.contains(dictHosp.getCode())) { |
| | | return; |
| | |
| | | }, Instant.now().plus(15, ChronoUnit.MINUTES)); |
| | | } |
| | | |
| | | private ReentrantLock getLock(String id) { |
| | | return lockMap.computeIfAbsent(id, k -> new ReentrantLock()); |
| | | } |
| | | |
| | | // 执行日志同步任务的方法 |
| | | public void executeTask(DictHosp dictHosp) { |
| | | // 获取锁,确保同一时间只有一个任务执行 |
| | | log.info("{}执行结果同步", dictHosp.getCode()); |
| | | dataSourceConfig.addDataSource(dictHosp.getDbname()); |
| | | DataSourceContextHolder.setDataSourceKey(dictHosp.getDbname()); |
| | | String dsqkssj = configService.selectConfigByKey("dsqkssj"); |
| | | String dsqjssj = configService.selectConfigByKey("dsqjssj"); |
| | | boolean panduandangqianshijian = panduandangqianshijian(dsqkssj, dsqjssj); |
| | | |
| | | log.info("{}执行结果同步加锁前",JSONUtil.toJsonStr(dictHosp)); |
| | | ReentrantLock reentrantLock = getLock(dictHosp.getCode()); |
| | | boolean lock = reentrantLock.tryLock(); |
| | | try { |
| | | log.info("{}执行结果同步加锁后",JSONUtil.toJsonStr(dictHosp)); |
| | | log.info("{}执行结果同步", dictHosp.getCode()); |
| | | dataSourceConfig.addDataSource(dictHosp.getDbname()); |
| | | DataSourceContextHolder.setDataSourceKey(dictHosp.getDbname()); |
| | | String dsqkssj = configService.selectConfigByKey("dsqkssj"); |
| | | String dsqjssj = configService.selectConfigByKey("dsqjssj"); |
| | | boolean panduandangqianshijian = panduandangqianshijian(dsqkssj, dsqjssj); |
| | | if (!panduandangqianshijian) { |
| | | if (lock) { |
| | | log.info("任务开始执行时间 -> {}", dateTimeFormat.format(new Date())); |
| | |
| | | List<LtkjExamJcsqd> list = jcsqdService.getLtkjJcsqdByTjh(s); |
| | | if(null !=list && !list.isEmpty()){ |
| | | for (LtkjExamJcsqd jcsqd : list) { |
| | | if(jcsqd.getJgzt().contains("。") || jcsqd.getJgzt().contains(";") || jcsqd.getJgzt().contains(";")){ |
| | | if(StringUtil.isNotBlank(jcsqd.getJgzt())){ |
| | | String[] split = jcsqd.getJgzt().replaceAll("\n", "").split("。|;|;"); |
| | | for (String jg : split) { |
| | | if(StringUtil.isNotBlank(jg) && !jg.contains("未见异常") |
| | |
| | | log.error("定时器执行报错"); |
| | | log.error(String.valueOf(e),e.getMessage()); |
| | | } finally { |
| | | if (reentrantLock.isLocked()) { |
| | | scheduledTasks.remove(dictHosp.getCode()); |
| | | reentrantLock.unlock(); |
| | | } |
| | | DataSourceContextHolder.setDataSourceKey(DataSourceType.MASTER.name()); |
| | | DataSourceContextHolder.clear(); |
| | | |
| | | reentrantLock.unlock(); |
| | | scheduledTasks.remove(dictHosp.getCode()); |
| | | // DataSourceContextHolder.setDataSourceKey(DataSourceType.MASTER.name()); |
| | | // DataSourceContextHolder.clear(); |
| | | log.info("任务{}已释放锁",dictHosp.getCode()); |
| | | } |
| | | } |
| | | |