ソースを参照

Merge remote-tracking branch 'origin/master'

yegang 2 年 前
コミット
a6f1e1e96d
1 ファイル変更11 行追加11 行削除
  1. 11 11
      src/main/java/com/sw/service/impl/SnatchMaskServiceImpl.java

+ 11 - 11
src/main/java/com/sw/service/impl/SnatchMaskServiceImpl.java

@@ -82,10 +82,11 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
         RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
         Boolean flag = false;
         try {
-
-            while (!connection.setNX(redisTemplate.getKeySerializer().serialize("mask-setnx-mq"), redisTemplate.getValueSerializer().serialize(1))) {
-                Thread.sleep(3);
-            }
+//
+//            while (!connection.setNX(redisTemplate.getKeySerializer().serialize("mask-setnx-mq-" + mid), redisTemplate.getValueSerializer().serialize(1))) {
+//                Thread.sleep(3);
+//            }
+            if(connection.setNX(redisTemplate.getKeySerializer().serialize("mask-setnx-mq-" + mid), redisTemplate.getValueSerializer().serialize(1))){
                 Integer remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());
                 System.out.println("库存扣减前:" + remaining + "---" + Thread.currentThread());
                 if (remaining.compareTo(amount) >= 0) {
@@ -95,19 +96,18 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
                 } else {
                     flag = false;
                 }
+                connection.del(redisTemplate.getKeySerializer().serialize("mask-setnx-mq-" + mid));
+                connection.close();
+                return flag;
+            }else {
+                return flag;
+            }
 
-            connection.close();
-            return flag;
-        } catch (InterruptedException e) {
-            e.printStackTrace();
         } finally {
-            connection.del(redisTemplate.getKeySerializer().serialize("mask-setnx-mq"));
             connection.close();
-            return  flag;
         }
 
 
-
 //        reentrantLock.lock();
 //        try {
 //            Integer remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());