分布式锁
/** * 设置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); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界