dinglan %!s(int64=2) %!d(string=hai) anos
pai
achega
5b48790be0

+ 6 - 8
src/main/java/com/sw/activemq/consumer/MessageConsumerService.java

@@ -7,6 +7,7 @@ import org.springframework.jms.annotation.JmsListener;
 import org.springframework.stereotype.Service;
 
 import javax.jms.JMSException;
+import javax.jms.MapMessage;
 import javax.jms.Message;
 import javax.jms.ObjectMessage;
 import java.util.Date;
@@ -21,15 +22,12 @@ public class MessageConsumerService {
 
 	@JmsListener(destination="mask")
 	public void receiveMessage(Message info) {	// 进行消息接收处理
-		System.out.println(new Date());
-		log.info("【*** 接收消息 ***】--->info", info);
-		if(info instanceof ObjectMessage){
-			ObjectMessage objectMessage=(ObjectMessage) info;
+		if(info instanceof MapMessage){
+			MapMessage objectMessage=(MapMessage) info;
 			try {
-				Map<String, Object> mesg = (Map) objectMessage.getObject();
-				Integer mid = Integer.valueOf(mesg.get("mid").toString());
-				Integer uid = Integer.valueOf(mesg.get("uid").toString());
-				snatchMaskService.doDecrAndCreateOrder(uid, mid);
+				Integer mid = objectMessage.getInt("mid");
+				Integer uid = objectMessage.getInt("uid");
+//				snatchMaskService.doDecrAndCreateOrder(uid, mid);
 			} catch (JMSException e) {
 				e.printStackTrace();
 			}

+ 28 - 16
src/main/java/com/sw/controller/SnatchMaskController.java

@@ -27,40 +27,52 @@ public class SnatchMaskController {
     private SnatchMaskService snatchMaskService;
 
     @GetMapping()
-    private String snatch(Integer uid){
-        if(redisTemplate.opsForSet().isMember("userUids", uid)){
-            if(redisTemplate.opsForSet().isMember("successUserUids", uid)){
+    private String snatch(Integer uid) {
+        if (redisTemplate.opsForSet().isMember("userUids", uid)) {
+            if (redisTemplate.opsForSet().isMember("successUserUids", uid)) {
                 return "你已经抢到口罩了";
-            }else{
+            } else {
                 return snatchMaskService.doSnatch(uid);
             }
-        }else{
+        } else {
             return "非法输入";
         }
     }
 
     /**
      * 库存保存redis  订单交给mq稍后异步处理
+     *
      * @param uid
      * @param mid
      * @return
      */
     @GetMapping("/{mid}")
-    private String snatchMQ(Integer uid, @PathVariable("mid") Integer mid){
-        if(redisTemplate.opsForSet().isMember("userUids", uid)){
-            if(redisTemplate.opsForSet().isMember("successUserUids", uid)){
+    private String snatchMQ(Integer uid, @PathVariable("mid") Integer mid) {
+
+        if (redisTemplate.opsForSet().isMember("userUids", uid)) {
+            if (redisTemplate.opsForSet().isMember("successUserUids", uid)) {
                 return "你已经抢到口罩了";
-            }else{
-                Boolean flag = snatchMaskService.checkAndDecrRemaining(mid);
-                if(flag){
-                    System.out.println(new Date());
-                    return snatchMaskService.doSnatchMQ(uid, mid);
-                }else{
-                    return  "库存不足 抢购失败 ";
+            } else {
+                Object o = new Object();
+                synchronized (o){
+                    Boolean flag = snatchMaskService.checkAndDecrRemaining(mid);
+                    if (flag) {
+                        return snatchMaskService.doSnatchMQ(uid, mid);
+                    } else {
+                        return "库存不足 抢购失败 ";
+                    }
                 }
+
             }
-        }else{
+        } else {
             return "非法输入";
         }
     }
+
+    @RequestMapping("/redisDecr")
+    public void redisDecr(){
+
+       Long res = redisTemplate.opsForValue().increment("remain",-1);
+       System.out.println(res);
+    }
 }

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

@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Value;
 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;
@@ -65,11 +66,19 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
      * 查看redis库存是否充足
      */
     @Override
-    public Boolean checkAndDecrRemaining(Integer mid) {
-        Integer remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());
-        Long res = redisTemplate.opsForHash().increment("usableMasks", "mask-" + mid, -amount);
-        System.out.println("increment res : "+res);//扣减之后的库存数量
-        return  remaining.compareTo(amount) > 0;
+    public  Boolean checkAndDecrRemaining(Integer mid) {
+        Object o = new Object();
+        Integer remaining = 0;
+//        synchronized (o) {
+            remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());
+            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;
+            }
+//        }
     }
 
     @Override
@@ -91,7 +100,7 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
     public void doDecrAndCreateOrder(Integer uid, Integer maskId) {
         //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

+ 4 - 4
src/test/java/com/sw/RedisTest.java

@@ -58,13 +58,13 @@ public class RedisTest {
         redisTemplate.opsForHash().put("usableMasks", "mask-1", 100);
         redisTemplate.opsForHash().put("usableMasks", "mask-2", 100);
 
-//        System.out.printf("res: "  + redisTemplate.opsForHash().get("usableMask", "mask-1").toString());
+//        System.out.printf("res: "  + redisTemplate.opsForHash().get("usableMasks", "mask-1").toString());
 
     }
     @Test
     public void decr(){
-        Long res = redisTemplate.opsForHash().increment("usableMasks", "mask-1", -5l);
-        System.out.println(res);
-
+//        Long res = redisTemplate.opsForHash().increment("usableMasks", "mask-1", -5l);
+//        System.out.println(res);
+        redisTemplate.opsForValue().set("remain", 100);
     }
 }