redis分布式锁

事例:

 public void updateArchiveByIdsAndDeleteResumeForFactoryContract(List veWmsArchives, List resumeIds) {
        log.info("********************Provider层更新回滚整机档案耦合/锁定状态以及是否签署厂家合同状态自定义传输对象******************** veWmsArchives={}", veWmsArchives);

        AssertUtil.isEmpty(veWmsArchives, VEHICLE_ARCHIVE_NOT_EXIST);

        // 循环加锁修改整机档案
        log.info("********************Provider层更新回滚整机档案耦合/锁定状态以及是否签署厂家合同状态开始******************** veWmsArchives={}", veWmsArchives);
        veWmsArchives.forEach(veWmsArchive -> {
            String vin = veWmsArchive.getVin();
            if (JinhuiStringUtil.isBlank(vin)) {
                return;
            }
            Long id = veWmsArchive.getId();
            RLock lock = redissonClient.getLock(CrmLockConstant.VEHICLE_ARCHIVE_VIN_KEY + vin);
            log.info("********************Provider层更新回滚整机档案耦合/锁定状态以及是否签署厂家合同状态 creating a RedissonLock lockName: {}, lock: {}, interrupted: {}, hold: {}, threadId: {}, 涉及vin: {}********************", lock.getName(), lock, Thread.currentThread().isInterrupted(), lock.isHeldByCurrentThread(), Thread.currentThread().getId(), vin);
            boolean leaseFlag = false;
            try {
                //释放时间必须合理化,设置不合理导致释放锁抛异常
                boolean flag = lock.tryLock(CrmLockConstant.VEHICLE_ORDER_COUPLING_WAIT_TIME, CrmLockConstant.VEHICLE_ORDER_COUPLING_LEASE_TIME, TimeUnit.MILLISECONDS);
                log.info("********************Provider层更新回滚整机档案耦合/锁定状态以及是否签署厂家合同状态 trying a RedissonLock lockName: {}, lock: {}, interrupted: {}, hold: {}, threadId: {}, flag: {}, 涉及vin: {}********************", lock.getName(), lock, Thread.currentThread().isInterrupted(), lock.isHeldByCurrentThread(), Thread.currentThread().getId(), flag, vin);
                if (!flag) {
                    log.error("********************Provider层更新回滚整机档案耦合/锁定状态以及是否签署厂家合同状态 Redisson分布式锁获取失败 lock: {}, 涉及vin: {}********************", lock, vin);
                    throw new BusinessException(VIN_OCCUPIED);
                }

                VeWmsArchivesDTO veWmsArchivesDto = new VeWmsArchivesDTO();
                veWmsArchivesDto.setId(id);
                veWmsArchivesDto.setCouplingFlag(veWmsArchive.getCouplingFlag()).setStatus(veWmsArchive.getStatus())
                        .setFirstSalesDate(veWmsArchive.getFirstSalesDate()).setContractSignStatus(veWmsArchive.getContractSignStatus());
                veWmsArchivesService.updateCouplingFlagAndContractSignStatusById(veWmsArchivesDto);

                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                    log.info("********************Provider层更新回滚整机档案耦合/锁定状态以及是否签署厂家合同状态 unlocking a RedissonLock lockName: {}, lock: {}, interrupted: {}, hold: {}, threadId: {}, 涉及vin: {}********************", lock.getName(), lock, Thread.currentThread().isInterrupted(), lock.isHeldByCurrentThread(), Thread.currentThread().getId(), vin);
                    leaseFlag = true;
                }
            } catch (InterruptedException e) {
                log.error("********************Provider层更新回滚整机档案耦合/锁定状态以及是否签署厂家合同状态 Redisson分布式锁出现中断异常 lockName: {} lock: {}, 涉及vin: {}********************", lock.getName(), lock, vin);
                Thread.currentThread().interrupt();
                throw new BusinessException(VIN_COUPLING_FAILED);
            } catch (Exception e) {
                log.error("********************Provider层更新回滚整机档案耦合/锁定状态以及是否签署厂家合同状态出现业务异常 archiveId: {}, 涉及vin: {}********************", id, vin, e);
                throw new BusinessException(e.getMessage());
            } finally {
                if (!leaseFlag) {
                    lock.unlock();
                    log.info("********************Provider层更新回滚整机档案耦合/锁定状态以及是否签署厂家合同状态 unlocking a RedissonLock lockName: {}, lock: {}, interrupted: {}, hold: {}, threadId: {}, 涉及vin: {}********************", lock.getName(), lock, Thread.currentThread().isInterrupted(), lock.isHeldByCurrentThread(), Thread.currentThread().getId(), vin);
                }
            }
        });
        log.info("********************Provider层更新回滚整机档案耦合/锁定状态以及是否签署厂家合同状态结束******************** veWmsArchives={}", veWmsArchives);

        veWmsResumeService.removeByIds(resumeIds);
    }

你可能感兴趣的:(分布式)