|
@@ -5,8 +5,17 @@ import com.sw.service.TMaskService;
|
|
import com.sw.service.TOrderService;
|
|
import com.sw.service.TOrderService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
+import org.springframework.jms.annotation.JmsListener;
|
|
|
|
+import org.springframework.jms.core.JmsMessagingTemplate;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import javax.xml.bind.annotation.XmlID;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.PrintWriter;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 秒杀服务
|
|
* 秒杀服务
|
|
*
|
|
*
|
|
@@ -24,7 +33,10 @@ public class SpikeController {
|
|
private TMaskService tMaskService;
|
|
private TMaskService tMaskService;
|
|
@Autowired
|
|
@Autowired
|
|
private RedisTemplate redisTemplate;
|
|
private RedisTemplate redisTemplate;
|
|
-
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private JmsMessagingTemplate jmsMessagingTemplate;
|
|
|
|
+ @Autowired
|
|
|
|
+ HttpServletResponse response;
|
|
@GetMapping("spike/{mid}/{uid}")
|
|
@GetMapping("spike/{mid}/{uid}")
|
|
public String spike(@PathVariable("mid") Integer mid, @PathVariable("uid") Integer uid) {
|
|
public String spike(@PathVariable("mid") Integer mid, @PathVariable("uid") Integer uid) {
|
|
//判断用户是否存在
|
|
//判断用户是否存在
|
|
@@ -40,4 +52,31 @@ public class SpikeController {
|
|
String res = tMaskService.spike(mid, uid);
|
|
String res = tMaskService.spike(mid, uid);
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
+ @GetMapping("spikeMq/{mid}/{uid}")
|
|
|
|
+ public String spikeMq(@PathVariable("mid") Integer mid, @PathVariable("uid") Integer uid) {
|
|
|
|
+ //判断用户是否存在
|
|
|
|
+ Boolean HasUserUid = redisTemplate.opsForSet().isMember("userUid", uid);
|
|
|
|
+ if (!HasUserUid) {
|
|
|
|
+ return "用户不存在";
|
|
|
|
+ }
|
|
|
|
+ //2.是否已经抢购成功
|
|
|
|
+ Boolean successUser = redisTemplate.opsForSet().isMember("success", uid);
|
|
|
|
+ if (successUser) {
|
|
|
|
+ return "一个用户只能抢购一次";
|
|
|
|
+ }
|
|
|
|
+ Map<String,Integer> map = new HashMap<>();
|
|
|
|
+ map.put("uid",uid);
|
|
|
|
+ map.put("mid",mid);
|
|
|
|
+ jmsMessagingTemplate.convertAndSend("smile.event.demo", map);
|
|
|
|
+ return "send message success";
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * queueListener 对应 ActiveMqConfig中的Bean
|
|
|
|
+ */
|
|
|
|
+ @JmsListener(destination="smile.event.demo", containerFactory = "queueListener")
|
|
|
|
+ public void receiveData(Map message) {
|
|
|
|
+ Integer mid = (Integer) message.get("mid");
|
|
|
|
+ Integer uid = (Integer) message.get("uid");
|
|
|
|
+ String res = tMaskService.spike(mid, uid);
|
|
|
|
+ }
|
|
}
|
|
}
|