Sfoglia il codice sorgente

fix:商城库存限制

liujunchi 3 anni fa
parent
commit
a8f68f491e

+ 2 - 2
cooleshow-mall/mall-portal/src/main/java/com/yonge/cooleshow/portal/controller/OmsCartItemController.java

@@ -45,7 +45,7 @@ public class OmsCartItemController {
     public CommonResult<OmsCartItem> add(@RequestBody OmsCartItem cartItem) {
 
         OmsCartItem omsCartItem = DistributedLock.of(redissonClient)
-                                                 .runIfLockCanGet(
+                                                 .runIfLockWaitGet(
                                                          LOCK_STOCK_MALL.getRedisKey(cartItem.getProductSkuId()),
                                                          () -> cartItemService.add(cartItem), 10, TimeUnit.SECONDS);
         return CommonResult.success(omsCartItem);
@@ -78,7 +78,7 @@ public class OmsCartItemController {
 
         OmsCartItem omsCartItem = cartItemService.get(id);
         DistributedLock.of(redissonClient)
-                       .runIfLockCanGet(LOCK_STOCK_MALL.getRedisKey(omsCartItem.getProductSkuId())
+                       .runIfLockWaitGet(LOCK_STOCK_MALL.getRedisKey(omsCartItem.getProductSkuId())
                                , () -> cartItemService.updateQuantity(id, memberService.getCurrentMember().getId(), quantity), 10, TimeUnit.SECONDS);
         return CommonResult.success(1);
 

+ 37 - 0
toolset/toolset-payment/src/main/java/com/yonge/toolset/payment/util/DistributedLock.java

@@ -195,4 +195,41 @@ public class DistributedLock {
             unlock(lock);
         }
     }
+
+    public <T> T runIfLockWaitGet(final String lockName, Callable<T> callable, final long timeout, TimeUnit unit) {
+        RLock lock = redissonClient.getLock(lockName);
+        if (Objects.isNull(lock)) {
+            log.info("callIfLockCanGet lock is null lockName : {}", lockName);
+            return null;
+        }
+        ExecutorService executor = Executors.newCachedThreadPool();
+        try {
+            if (lock.tryLock(timeout, timeout, unit)) {
+                log.info("callIfLockCanGet lock lockName : {} time is {}", lockName, System.currentTimeMillis());
+                Future<T> submit = executor.submit(callable);
+                return submit.get();
+            } else {
+                return null;
+            }
+        } catch (BizException e) {
+            throw e;
+        } catch (ExecutionException e) {
+            Throwable cause = e.getCause();
+            cause.printStackTrace();
+            ;
+
+            String message = cause.getMessage();
+            if (StringUtil.isEmpty(message)) {
+                throw new RuntimeException("任务执行异常");
+            } else {
+                throw new BizException(message);
+            }
+        } catch (Exception e) {
+            log.error("callIfLockCanGet error lockKey {}", lockName);
+            throw new RuntimeException("任务执行异常");
+        } finally {
+            executor.shutdown();
+            unlock(lock);
+        }
+    }
 }