|
@@ -1,6 +1,5 @@
|
|
|
package com.sw.service.impl;
|
|
|
|
|
|
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
|
import com.sw.activemq.producer.IMessageProducerService;
|
|
|
import com.sw.domain.TMask;
|
|
|
import com.sw.service.SnatchMaskService;
|
|
@@ -11,17 +10,16 @@ import org.redisson.api.RLock;
|
|
|
import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.redis.connection.RedisConnection;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.concurrent.locks.Condition;
|
|
|
-import java.util.concurrent.locks.LockSupport;
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
|
|
/**
|
|
@@ -81,23 +79,51 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
*/
|
|
|
@Override
|
|
|
public Boolean checkAndDecrRemaining(Integer mid) {
|
|
|
-
|
|
|
- reentrantLock.lock();
|
|
|
+ RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
|
|
|
+ Boolean flag = false;
|
|
|
try {
|
|
|
- Integer remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());
|
|
|
- System.out.println("库存扣减前:" + remaining + "---" + Thread.currentThread());
|
|
|
- if (remaining.compareTo(amount) >= 0) {
|
|
|
- Long res = redisTemplate.opsForHash().increment("usableMasks", "mask-" + mid, -amount);
|
|
|
- System.out.println("库存扣减后:" + "increment res : " + res + "---" + Thread.currentThread());//扣减之后的库存数量
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- return false;
|
|
|
+
|
|
|
+ while (!connection.setNX(redisTemplate.getKeySerializer().serialize("mask-setnx-mq"), redisTemplate.getValueSerializer().serialize(1))) {
|
|
|
+ Thread.sleep(3);
|
|
|
}
|
|
|
+ Integer remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());
|
|
|
+ System.out.println("库存扣减前:" + remaining + "---" + Thread.currentThread());
|
|
|
+ if (remaining.compareTo(amount) >= 0) {
|
|
|
+ Long res = redisTemplate.opsForHash().increment("usableMasks", "mask-" + mid, -amount);
|
|
|
+ System.out.println("库存扣减后:" + "increment res : " + res + "---" + Thread.currentThread());//扣减之后的库存数量
|
|
|
+ flag = true;
|
|
|
+ } else {
|
|
|
+ flag = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ connection.close();
|
|
|
+ return flag;
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
} finally {
|
|
|
- reentrantLock.unlock();
|
|
|
+ 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());
|
|
|
+// System.out.println("库存扣减前:" + remaining + "---" + Thread.currentThread());
|
|
|
+// if (remaining.compareTo(amount) >= 0) {
|
|
|
+// Long res = redisTemplate.opsForHash().increment("usableMasks", "mask-" + mid, -amount);
|
|
|
+// System.out.println("库存扣减后:" + "increment res : " + res + "---" + Thread.currentThread());//扣减之后的库存数量
|
|
|
+// return true;
|
|
|
+// } else {
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+// } finally {
|
|
|
+// reentrantLock.unlock();
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
/* synchronized (o) {
|
|
|
// Integer remaining = 0;
|
|
|
Integer remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());
|