wuheng 2 年之前
父节点
当前提交
fc8b1bc9a2

二进制
rebbitmq/RabbitMQ.pdf


+ 38 - 0
rebbitmq/consumer/src/main/java/com/lovecoding/rabbitmq/consumer/ConsumerTemplate.java

@@ -1,8 +1,13 @@
 package com.lovecoding.rabbitmq.consumer;
 
+import com.rabbitmq.client.AMQP;
+import com.rabbitmq.client.Channel;
+import org.springframework.amqp.core.Message;
 import org.springframework.amqp.rabbit.annotation.RabbitListener;
 import org.springframework.stereotype.Component;
 
+import java.io.IOException;
+
 @Component
 public class ConsumerTemplate {
 
@@ -58,4 +63,37 @@ public class ConsumerTemplate {
         System.out.println( "哈尔滨天气:" + msg );
     }
 
+    @RabbitListener(queues = "topicQueue")
+    public void t7( String msg){
+        System.out.println( "北京天气:" + msg );
+    }
+
+    @RabbitListener(queues = "topicQueueT8")
+    public void t8( String msg){
+        System.out.println( "哈尔滨天气:" + msg );
+    }
+
+    @RabbitListener(queues = "topicQueueT9")
+    public void t9( String msg){
+        System.out.println( "通用天气:" + msg );
+    }
+
+    @RabbitListener(queues = "deadQueueDemo")
+    public void t10(Message msg, Channel channel){
+
+        long deliveryTag = msg.getMessageProperties().getDeliveryTag();
+
+        try {
+            channel.basicNack(deliveryTag, false, false);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        System.out.println( "收到消息:" + msg );
+    }
+
+//    @RabbitListener(queues = "deadQueue")
+//    public void t11(String msg){
+//        System.out.println( "收到死信:" + msg );
+//    }
+
 }

+ 51 - 0
rebbitmq/consumer/src/main/java/com/lovecoding/rabbitmq/consumer/DeadConfig.java

@@ -0,0 +1,51 @@
+package com.lovecoding.rabbitmq.consumer;
+
+
+import org.springframework.amqp.core.Binding;
+import org.springframework.amqp.core.BindingBuilder;
+import org.springframework.amqp.core.DirectExchange;
+import org.springframework.amqp.core.Queue;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.HashMap;
+
+@Configuration
+public class DeadConfig {
+
+    @Bean
+    public DirectExchange deadDirectExchange(){
+        return new DirectExchange("deadDirectExchange");
+    }
+
+    @Bean
+    public Queue deadQueue(){
+        return new Queue("deadQueue");
+    }
+
+    @Bean
+    public DirectExchange deadDirectExchangeDemo(){
+        return new DirectExchange("deadDirectExchangeDemo");
+    }
+
+    @Bean
+    public Queue deadQueueDemo(){
+        HashMap<String, Object> args = new HashMap<>();
+        args.put("x-dead-letter-exchange", "deadDirectExchange" );
+        args.put("x-dead-letter-routing-key", "dead" );
+        //args.put("x-max-length", 6);
+        args.put("x-message-ttl", 10000);
+        return new Queue("deadQueueDemo",
+                false, false, false, args);
+    }
+
+    @Bean
+    public Binding deadQueueDemoBinding(Queue deadQueueDemo, DirectExchange deadDirectExchangeDemo){
+        return BindingBuilder.bind(deadQueueDemo).to(deadDirectExchangeDemo).with("tianqi");
+    }
+
+    @Bean
+    public Binding deadQueueBinding(Queue deadQueue, DirectExchange deadDirectExchange){
+        return BindingBuilder.bind(deadQueue).to(deadDirectExchange).with("dead");
+    }
+}

+ 49 - 0
rebbitmq/consumer/src/main/java/com/lovecoding/rabbitmq/consumer/TopicConfig.java

@@ -0,0 +1,49 @@
+package com.lovecoding.rabbitmq.consumer;
+
+import org.springframework.amqp.core.Binding;
+import org.springframework.amqp.core.BindingBuilder;
+import org.springframework.amqp.core.Queue;
+import org.springframework.amqp.core.TopicExchange;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class TopicConfig {
+
+    @Bean
+    public TopicExchange topicExchange(){
+        return new TopicExchange("topicExchange");
+    }
+    //接收北京天气
+    @Bean
+    public Queue topicQueue(){
+        return new Queue("topicQueue");
+    }
+    //接收哈尔滨天气
+    @Bean
+    public Queue topicQueueT8(){
+        return new Queue("topicQueueT8");
+    }
+    //全国天气
+    @Bean
+    public Queue topicQueueT9(){
+        return new Queue("topicQueueT9");
+    }
+
+    @Bean
+    public Binding topicExchangeBindingT8(Queue topicQueueT8, TopicExchange topicExchange ){
+        return BindingBuilder.bind(topicQueueT8).to(topicExchange).with("com.lc.haerbin");
+    }
+
+    @Bean
+    public  Binding topicExchangeBinding(Queue topicQueue, TopicExchange topicExchange) {
+        return BindingBuilder.bind(topicQueue).to(topicExchange).with("com.lc.beijing");
+    }
+
+    @Bean
+    public Binding topicExchangeBindingT9(Queue topicQueueT9, TopicExchange topicExchange){
+        //return BindingBuilder.bind(topicQueueT9).to(topicExchange).with("com.#");
+        return BindingBuilder.bind(topicQueueT9).to(topicExchange).with("com.lc.*");
+    }
+
+}

+ 26 - 0
rebbitmq/producer/src/test/java/com/lovecoding/rabbitmq/producer/ProducerApplicationTests.java

@@ -61,6 +61,32 @@ class ProducerApplicationTests {
                 "com.lc.direct.demo", "哈尔滨", "多云有雷阵雨" );
     }
 
+    @Test
+    void t5(){
+        rabbitTemplate.convertAndSend(
+                "topicExchange", "com.lc.haerbin", "多云" );
+    }
+
+    @Test
+    void t6(){
+        rabbitTemplate.convertAndSend(
+                "topicExchange", "com.lc.beijing", "雷阵雨" );
+    }
+
+    @Test
+    void t7(){
+        rabbitTemplate.convertAndSend(
+                "topicExchange", "com.lc.shenzhen", "雾霾" );
+    }
+
+    @Test
+    void t8(){
+        for (int i = 0; i < 10; i++) {
+            rabbitTemplate.convertAndSend(
+                    "deadDirectExchangeDemo", "tianqi", "雾霾" + i );
+        }
+    }
+
     /**
      * 手工发送消息
      * @throws IOException