随笔 - 144  文章 - 0  评论 - 2  阅读 - 92190

分布式锁

复制代码
/**
     * 设置redis分布式锁
     *
     * @param keyType 分布式锁类型
     * @param hKey    分布式锁键值
     * @return 对象列表
     */
    public boolean getRedisTempLock(final String keyType, final String hKey, long tempValue) {
        String key = keyType.concat(hKey);
        Boolean result = redisTemplate.opsForValue().setIfAbsent(key, tempValue, 5, TimeUnit.MINUTES);
        return result != null && result;
    }
复制代码
if (redisUtil.getRedisTempLock(OrderOutType.RECEIVING_GOODS.getType(), serviceNo,
                System.currentTimeMillis())) {
                log.info("Op:confirmReceiving, Locked:{} successfully.", serviceNo);
            } else {
                log.error("Op:confirmReceiving, Locked:{} fail!", serviceNo);
                return BaseExResponse.error(ErrorMsg.OPERATE_FAIL.getMsg());
            }
复制代码
// roll back
        try {
            if (redisUtil.getRedisTempLock(OrderOutType.RECEIVING_GOODS.getType(), serviceNo,
                System.currentTimeMillis())) {
                log.info("Op:confirmReceiving, Locked:{} successfully.", serviceNo);
            } else {
                log.error("Op:confirmReceiving, Locked:{} fail!", serviceNo);
                return BaseExResponse.error(ErrorMsg.OPERATE_FAIL.getMsg());
            }
        } catch (Exception e) {
            log.error("Op:confirmReceiving, params:{} has error:", JSON.toJSONString(record), e);
            if (e instanceof BaseException) {
                throw new RuntimeException("次品收货失败:" + e.getMessage());
            }
            throw new RuntimeException("次品收货失败:请联系管理员处理");
        } finally {
            log.info("Op:confirmReceiving, Lock:{} released successfully", serviceNo);
            redisUtil.deleteRedisTempLock(OrderOutType.RECEIVING_GOODS.getType(), serviceNo);
        }
复制代码

 

posted on   IT-QI  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示