|
@@ -16,6 +16,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.locks.Condition;
|
|
|
+import java.util.concurrent.locks.LockSupport;
|
|
|
+import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
|
|
/**
|
|
|
* @Description TODO
|
|
@@ -37,26 +40,29 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
private IMessageProducerService mqService;
|
|
|
|
|
|
@Value("${amount}")
|
|
|
- private Integer amount;
|
|
|
+ private Integer amount;
|
|
|
Object o = new Object();
|
|
|
+ ReentrantLock reentrantLock = new ReentrantLock();
|
|
|
+ Condition condition = reentrantLock.newCondition();
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional
|
|
|
public String doSnatch(Integer uid) {
|
|
|
Integer maskId = 1;
|
|
|
//1.get the amount of remaining masks
|
|
|
TMask mask = maskService.getById(maskId);
|
|
|
- if(mask.getMaskStock() > amount){
|
|
|
+ if (mask.getMaskStock() > amount) {
|
|
|
//3.create order
|
|
|
int res = orderService.creatOrder(uid, maskId);
|
|
|
//4.decrease the stock of mask
|
|
|
Boolean flag = maskService.decrease(maskId, mask.getMaskStock() - amount);
|
|
|
- if(res > 0 && flag){
|
|
|
+ if (res > 0 && flag) {
|
|
|
|
|
|
//3. add to successUserUids
|
|
|
redisTemplate.opsForSet().add("successUserUids", uid);
|
|
|
return "恭喜你 抢购成功";
|
|
|
- }else{
|
|
|
- return "抢购失败 稍后重试";
|
|
|
+ } else {
|
|
|
+ return "抢购失败 稍后重试";
|
|
|
}
|
|
|
}
|
|
|
return "库存不足 抢购失败";
|
|
@@ -66,9 +72,25 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
* 查看redis库存是否充足
|
|
|
*/
|
|
|
@Override
|
|
|
- public Boolean checkAndDecrRemaining(Integer mid) {
|
|
|
+ public Boolean checkAndDecrRemaining(Integer mid) {
|
|
|
|
|
|
- synchronized (o) {
|
|
|
+ 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());
|
|
|
System.out.println("库存扣减前:"+remaining+"---" + Thread.currentThread());
|
|
@@ -79,7 +101,7 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
}else{
|
|
|
return false;
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -93,6 +115,7 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
|
|
|
/**
|
|
|
* mq消费端 调用方法 扣减sql库存 创建未支付订单
|
|
|
+ *
|
|
|
* @param uid
|
|
|
* @param maskId
|
|
|
*/
|
|
@@ -102,7 +125,7 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
//1.get the amount of remaining masks
|
|
|
TMask mask = maskService.getById(maskId);
|
|
|
System.out.println(mask.getMaskStock() + "----" + Thread.currentThread());
|
|
|
- if(mask.getMaskStock() >= amount) {
|
|
|
+ if (mask.getMaskStock() >= amount) {
|
|
|
//3.create order
|
|
|
int res = orderService.creatOrder(uid, maskId);
|
|
|
//4.decrease the stock of mask
|
|
@@ -112,7 +135,7 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
//3. add to successUserUids
|
|
|
redisTemplate.opsForSet().add("successUserUids", uid);
|
|
|
}
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
// TODO 如果sql中库存不够 扣减失败之后 消息已经消费了 或者是事物回滚了 如何处理???
|
|
|
return;
|
|
|
}
|