/** * 处理加锁方法 * * @return */ public boolean tryLock(String key, boolean otherFlag) { if (StringUtils.isRealEmpty(key)) { throw new ApplicationException(ResponseCode.BILL_BATCH_NUMBER_NULL); } logger.info("加锁putIfAbsent key={}", buildLockKey(key)); boolean flag = redisTemplate.opsForHash().putIfAbsent(buildLockKey(key), LOCK_TIME, DateUtils.toString(new Date())); logger.info("加锁putIfAbsent result={}", flag); if (flag) { redisTemplate.opsForHash().putAll(buildLockKey(key), buildLockContext()); redisTemplate.expire(buildLockKey(key), 5L, TimeUnit.MINUTES); logger.info("修改加锁{}5分钟", buildLockKey(key)); } else { logger.info("账单已锁", buildLockKey(key)); if (otherFlag) { List<Object> values = redisTemplate.opsForHash().multiGet(buildLockKey(key), Arrays.asList(LOCK_TIME, LOCK_USERNAME)); String username = (String) values.get(1); String lockTime = (String) values.get(0); throw new ApplicationException(ResponseCode.BILL_LOCKED_EDIT.getCode(), String.format(ResponseCode.BILL_LOCKED_EDIT.getMessage(), lockTime, username)); } return false; } return true; }