Преглед на файлове

修改了死信隊列相關代碼

zxy преди 2 години
родител
ревизия
fe1197ad8f

+ 4 - 0
pom.xml

@@ -72,6 +72,10 @@
72 72
             <artifactId>fastjson</artifactId>
73 73
             <version>1.2.72</version>
74 74
         </dependency>
75
+        <dependency>
76
+            <groupId>org.springframework.boot</groupId>
77
+            <artifactId>spring-boot-starter-websocket</artifactId>
78
+        </dependency>
75 79
     </dependencies>
76 80
 
77 81
     <build>

+ 18 - 0
src/main/java/com/example/mqListener/MqListener.java

@@ -0,0 +1,18 @@
1
+package com.example.mqListener;
2
+
3
+import org.springframework.amqp.rabbit.annotation.Queue;
4
+import org.springframework.amqp.rabbit.annotation.QueueBinding;
5
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
6
+import org.springframework.stereotype.Component;
7
+
8
+@Component
9
+public class MqListener {
10
+    public static final String EXCHANGE_NAME = "deviceTopicExchange";
11
+    public static final String ROUTER_KEY = "device1.#";
12
+    public static final String QUE = "device01";
13
+    @RabbitListener( queues = QUE)
14
+    public void device1(String message) {
15
+        System.out.println(message);
16
+    }
17
+
18
+}

+ 2 - 0
src/main/java/com/example/springbootdemo/SpringbootdemoApplication.java

@@ -1,5 +1,6 @@
1 1
 package com.example.springbootdemo;
2 2
 
3
+import com.example.springbootdemo.config.CustomRabbitListener;
3 4
 import org.mybatis.spring.annotation.MapperScan;
4 5
 import org.springframework.boot.SpringApplication;
5 6
 import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -10,6 +11,7 @@ import org.springframework.context.annotation.ComponentScan;
10 11
 public class SpringbootdemoApplication {
11 12
     public static void main(String[] args) {
12 13
         ConfigurableApplicationContext run  = SpringApplication.run(SpringbootdemoApplication.class, args);
14
+        CustomRabbitListener bean = run.getBean(CustomRabbitListener.class);
13 15
 //        String[] names = run.getBeanDefinitionNames();
14 16
 //        for(String name:names) {
15 17
 //            System.out.println(name);

+ 17 - 6
src/main/java/com/example/springbootdemo/config/CustomRabbitListener.java

@@ -5,9 +5,12 @@ import org.springframework.amqp.core.Message;
5 5
 import org.springframework.amqp.rabbit.annotation.Exchange;
6 6
 import org.springframework.amqp.rabbit.annotation.Queue;
7 7
 import org.springframework.amqp.rabbit.annotation.QueueBinding;
8
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
9
+import org.springframework.amqp.support.AmqpHeaders;
8 10
 import org.springframework.messaging.handler.annotation.Headers;
9 11
 import org.springframework.stereotype.Component;
10 12
 
13
+import java.io.IOException;
11 14
 import java.util.Map;
12 15
 
13 16
 @Component
@@ -16,10 +19,18 @@ public class CustomRabbitListener {
16 19
 //    public void a(Message message) {
17 20
 //        System.out.println(message);
18 21
 //    }
19
-//    @org.springframework.amqp.rabbit.annotation.RabbitListener(
20
-//            queues = RabbitMQConfiguration.dealQueueOrder
21
-//    )
22
-//    public void process(String order, Message message, @Headers Map<String, Object> headers, Channel channel) {
23
-//        System.out.println(order);
24
-//    }
22
+    @RabbitListener (queues = RabbitMQConfiguration.dealQueueOrder)
23
+    public void process(String order, Message message, @Headers Map<String, Object> headers, Channel channel) throws IOException {
24
+        System.out.println("死信ie zh++"+ order);
25
+        Long l = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
26
+        channel.basicAck(l,false);
27
+    }
28
+    @RabbitListener (queues = RabbitMQConfiguration.orderQueue)
29
+    public void orderQueue(String order, Message message, @Headers Map<String, Object> headers, Channel channel) throws IOException {
30
+        System.out.println("orderQueue收到隊列消息並進行拒絕"+ order);
31
+        Long l = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
32
+        channel.basicNack(l,false,false);
33
+    }
34
+
35
+
25 36
 }

+ 30 - 0
src/main/java/com/example/springbootdemo/config/DeviceRabbitConfig.java

@@ -0,0 +1,30 @@
1
+package com.example.springbootdemo.config;
2
+
3
+import com.rabbitmq.client.AMQP;
4
+import com.rabbitmq.client.Channel;
5
+import org.springframework.amqp.core.Binding;
6
+import org.springframework.amqp.core.BindingBuilder;
7
+import org.springframework.amqp.core.Queue;
8
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
9
+import org.springframework.beans.factory.annotation.Autowired;
10
+import org.springframework.context.annotation.Bean;
11
+import org.springframework.context.annotation.Configuration;
12
+import org.springframework.stereotype.Component;
13
+
14
+import java.io.IOException;
15
+import java.util.concurrent.TimeoutException;
16
+
17
+@Component
18
+public class DeviceRabbitConfig {
19
+    @Autowired
20
+    RabbitTemplate rabbitTemplate;
21
+    @Bean
22
+    public AMQP.Queue.BindOk declareDeviceQueue() throws IOException, TimeoutException {
23
+        Channel channel = rabbitTemplate.getConnectionFactory().createConnection().createChannel(false);
24
+        channel.queueDeclare("device001", true, false, false, null);
25
+        return channel.queueBind("device001", "deviceExchange", "device001");
26
+//        return new Queue("device001",true,false,false,null);
27
+    }
28
+
29
+
30
+}

+ 1 - 1
src/main/java/com/example/springbootdemo/config/RabbitMQConfiguration.java

@@ -42,7 +42,7 @@ public class RabbitMQConfiguration {
42 42
     public Queue orderQueue() {
43 43
         // 将普通队列绑定到死信队列交换机上
44 44
         Map<String, Object> args = new HashMap<>(2);
45
-        //args.put("x-message-ttl", 5 * 1000);//直接设置 Queue 延迟时间 但如果直接给队列设置过期时间,这种做法不是很灵活
45
+//        args.put("x-message-ttl", 5 * 1000);//直接设置 Queue 延迟时间 但如果直接给队列设置过期时间,这种做法不是很灵活
46 46
         //这里采用发送消息动态设置延迟时间,这样我们可以灵活控制
47 47
         args.put(DEAD_LETTER_QUEUE_KEY, dealExchangeOrder);
48 48
         args.put(DEAD_LETTER_ROUTING_KEY, deadRoutingKeyOrder);

+ 18 - 0
src/main/java/com/example/springbootdemo/config/WebMvcConfig.java

@@ -0,0 +1,18 @@
1
+package com.example.springbootdemo.config;
2
+
3
+import com.example.springbootdemo.interceptor.Myinterceptor;
4
+import org.springframework.context.annotation.Configuration;
5
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
6
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
7
+
8
+import javax.annotation.Resource;
9
+@Configuration
10
+public class WebMvcConfig implements WebMvcConfigurer {
11
+    @Resource
12
+    private Myinterceptor myinterceptor;
13
+    @Override
14
+    public void addInterceptors(InterceptorRegistry registry) {
15
+        registry.addInterceptor(myinterceptor).addPathPatterns("/**");
16
+//        WebMvcConfigurer.super.addInterceptors(registry);
17
+    }
18
+}

+ 4 - 2
src/main/java/com/example/springbootdemo/controller/TrainJavaController.java

@@ -24,8 +24,10 @@ public class TrainJavaController {
24 24
     RabbitTemplate rabbitTemplate;
25 25
     @RequestMapping("/footScan")
26 26
     public String getFootScanInfo() {
27
-        FootScanCommonEntity  footScanCommonInfo = footScanCommonRepository.findByFootScanId(14815);
28
-        return  footScanCommonInfo.getFootScanSign();
27
+//        FootScanCommonEntity  footScanCommonInfo = footScanCommonRepository.findByFootScanId(14815);
28
+//        return  footScanCommonInfo.getFootScanSign();
29
+        System.out.println(123);
30
+        return "sitn";
29 31
     }
30 32
     @RequestMapping("/postTest")
31 33
     public void getFootScanList(@RequestParam Map<Integer,Object> params) {

+ 22 - 0
src/main/java/com/example/springbootdemo/interceptor/Myinterceptor.java

@@ -0,0 +1,22 @@
1
+package com.example.springbootdemo.interceptor;
2
+
3
+
4
+import org.springframework.stereotype.Component;
5
+import org.springframework.web.servlet.HandlerInterceptor;
6
+import org.springframework.web.servlet.ModelAndView;
7
+
8
+import javax.servlet.http.HttpServletRequest;
9
+import javax.servlet.http.HttpServletResponse;
10
+@Component
11
+public class Myinterceptor  implements HandlerInterceptor {
12
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
13
+        System.out.println("pre");
14
+        return true;
15
+    }
16
+    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
17
+        System.out.println("chulihou");
18
+    }
19
+    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
20
+        System.out.println("clean");
21
+    }
22
+}

+ 4 - 0
src/main/java/com/example/springbootdemo/websocket/WSServer.java

@@ -0,0 +1,4 @@
1
+package com.example.springbootdemo.websocket;
2
+
3
+public class WSServer {
4
+}

+ 1 - 1
src/main/resources/application-dev.yml

@@ -18,7 +18,7 @@ spring:
18 18
         login-username: zxy
19 19
         login-password: 123456
20 20
       driver-class-name: com.mysql.cj.jdbc.Driver
21
-      url: jdbc:mysql://192.168.0.201:3306/ceshi?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
21
+      url: jdbc:mysql://192.168.50.132:3306/ceshi?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
22 22
       username: root
23 23
       password: 123456
24 24
   jpa:

+ 6 - 2
src/main/resources/application.yml

@@ -11,7 +11,7 @@ server:
11 11
   port: 8081
12 12
 spring:
13 13
   datasource:
14
-    url: jdbc:mysql://192.168.0.201:3306/ceshi?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
14
+    url: jdbc:mysql://192.168.50.132:3306/ceshi?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
15 15
     username: root
16 16
     password: 123456
17 17
     driver-class-name: com.mysql.cj.jdbc.Driver
@@ -19,12 +19,16 @@ spring:
19 19
     database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
20 20
     open-in-view: false
21 21
   rabbitmq:
22
-    host: 192.168.0.100
22
+    host: 192.168.50.100
23 23
     port: 5672
24 24
     username: guest
25 25
     password: guest
26 26
     publisher-returns: true
27 27
     virtual-host: /scanmq
28
+    listener:
29
+      simple:
30
+        acknowledge-mode: manual
31
+        default-requeue-rejected: false
28 32
 mybatis-plus:
29 33
   configuration:
30 34
     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl