yegang 2 年之前
父節點
當前提交
5a22af52e7

+ 46 - 0
src/main/java/com/sw/config/ActiveMqConfig.java

@@ -0,0 +1,46 @@
+package com.sw.config;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.jms.config.JmsListenerContainerFactory;
+import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
+import org.springframework.jms.core.JmsMessagingTemplate;
+
+import javax.jms.ConnectionFactory;
+
+/**
+ * @author yegang
+ * @create 2022-02-18 20:16
+ **/
+@Configuration
+public class ActiveMqConfig {
+
+    @Value("${spring.activemq.broker-url}")
+    private String brokerUrl;
+
+    @Value("${spring.activemq.user}")
+    private String username;
+
+    @Value("${spring.activemq.password}")
+    private String password;
+
+    @Bean
+    public ConnectionFactory connectionFactory(){
+        return new ActiveMQConnectionFactory(username, password, brokerUrl);
+    }
+
+    @Bean
+    public JmsMessagingTemplate jmsMessageTemplate(){
+        return new JmsMessagingTemplate(connectionFactory());
+    }
+
+    @Bean("queueListener")
+    public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){
+        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
+        factory.setConnectionFactory(connectionFactory);
+        factory.setPubSubDomain(false);
+        return factory;
+    }
+}

+ 40 - 1
src/main/java/com/sw/controller/SpikeController.java

@@ -5,8 +5,17 @@ import com.sw.service.TMaskService;
 import com.sw.service.TOrderService;
 import org.springframework.beans.factory.annotation.Autowired;
 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 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;
     @Autowired
     private RedisTemplate redisTemplate;
-
+    @Autowired
+    private JmsMessagingTemplate jmsMessagingTemplate;
+    @Autowired
+    HttpServletResponse response;
     @GetMapping("spike/{mid}/{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);
         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);
+    }
 }

+ 1 - 0
src/main/java/com/sw/service/impl/TMaskServiceImpl.java

@@ -63,6 +63,7 @@ public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TM
         redisTemplate.opsForSet().add("sucess",uid);
         return "抢购成功,订单号为"+tOrder.getId();
     }
+
     @Override
     public boolean decrease(Integer maskId, Integer remain) {
         UpdateWrapper<TMask> update = new UpdateWrapper<TMask>();

+ 10 - 5
src/main/resources/application.yml

@@ -7,26 +7,31 @@ spring:
 #    url: jdbc:mysql://124.70.152.102:3306/mask?useSSL=false
 #    username: sunwin
 #    password: Sunwin@2020
-    url: jdbc:mysql://192.168.20.82:3306/mask?useUnicode=true&characterEncoding=utf-8&useSSL=false
+#    url: jdbc:mysql://192.168.20.82:3306/mask?useUnicode=true&characterEncoding=utf-8&useSSL=false
+#    username: root
+#    password: Sunwin@2020
+    url: jdbc:mysql://localhost:3307/mask?useUnicode=true&characterEncoding=utf-8&useSSL=false
     username: root
-    password: Sunwin@2020
+    password: 123456
     driver-class-name: com.mysql.cj.jdbc.Driver
   redis:
     # 数据库索引,默认0
     database: 4
     #    # 服务器IP地址
-    host: 192.168.20.83
+#    host: 192.168.20.83
+    host: 127.0.0.1
     # 连接端口
     port: 6379
     # Redis服务器连接密码(默认为空)
-    password: sunwin
+#    password: sunwin
   jms:
     # 配置消息的类型 默认false,如果是true则表示为topic消息,如果为false表示Queue消息,一个服务里 只能用一种???
     pub-sub-domain: false
   activemq:
     user: admin    # 连接用户名
     password: admin   # 连接密码
-    broker-url: tcp://192.168.20.76:28085 # 消息组件的连接主机信息
+#    broker-url: tcp://192.168.20.76:28085 # 消息组件的连接主机信息
+    broker-url: tcp://localhost:61616 # 消息组件的连接主机信息
 queuename: mask
 
 mybatis-plus: