RabbitMQ 五种处理模型

消息发送

 1  2 @SpringBootTest(classes = MqApplication.class)
 3 @RunWith(SpringRunner.class)
 4 public class TestSpringBootMQ {
 5     @Autowired
 6     private RabbitTemplate rabbitTemplate;
 7  8     /**
 9      * hello world 模型
10      * 没有交换机绑定,直接将消息发送到队列中
11      * 这个是只有一个消费者来消费队列中的消息
12      */
13     @Test
14     public void helloword() {
15         rabbitTemplate.convertAndSend("hello", "这是[direct]模型的消息");
16     }
17 18 19     /**
20      * work queue 模型  工作
21      * 在hello world模型上的升级
22      * 不需要交换机绑定,可以有多个消费者消费消息
23      * 默认情况下,消息的分发平均分配到每个不同的消费者上
24      * 可以进行“能者多劳”的设置,即:那个消费者线程处理的快,就可在获得同一队列更多的消息进行处理
25      */
26     @Test
27     public void workQueue() {
28         for (int i = 0; i < 10; i++) {
29             rabbitTemplate.convertAndSend("work", "[" + i + "]这是[work]模型的消息");
30         }
31     }
32 33     /**
34      * fanout模型  广播
35      * 需要生命交换机
36      * 消费者生成临时队列,跟交换机进行绑定,然后处理消息
37      * 处理的消息的类型是不可选择的
38      */
39     @Test
40     public void fanout() {
41         rabbitTemplate.convertAndSend("logs", "fanout", "这是[fanout]模型发送的消息!");
42 43     }
44 45     /**
46      * Routing模型 路由
47      * 需要声明交换机
48      * 消费者需要生成临时队列来绑定交换机,来处理消息
49      * 处理的消息类型是可以选择的
50      */
51     @Test
52     public void route() {
53         String type = "error";
54         rabbitTemplate.convertAndSend("direcets", type, "这是[routing]模型发送[" + type + "]的消息!");
55 56     }
57 58     /**
59      * topic模型 动态路由 订阅模式
60      * 需要声明交换机
61      * 消费者需要生成临时队列,与交换机进行绑定,处理消息
62      * 消息的类型可以用通配符  "*","#"等来归类处理
63      */
64     @Test
65     public void topic() {
66         String type = "user.save";
67         rabbitTemplate.convertAndSend("topics", type, "这是[topic]模型发送[" + type + "]的消息!");
68 69     }
70 }
71

 

消费者消费消息方式

  1. hello world模式

     1 @Component
     2 /**
     3  * @Queue中可以配置队列相关的属性设置
     4  * durable: 是否数据持久化
     5  * autoDelete: 是否自动删除
     6  *
     7  */
     8 @RabbitListener(queuesToDeclare = @Queue(value = "hello",durable = "false",autoDelete = "false"))  // 创建hello队列,并监听
     9 public class Consumer {
    10     @RabbitHandler
    11     public void receiver1(String message){
    12         System.out.println("message = "+message);
    13     }
    14 }
    15

     

  2. work queue模式

     1 @Component
     2 public class WorkConsumer {
     3  4     // 第一个消费者
     5     @RabbitListener(queuesToDeclare = @Queue("work"))
     6     public void receiver1(String message) {
     7         System.out.println("message1 = " + message);
     8     }
     9 10     // 第二个消费者
    11     @RabbitListener(queuesToDeclare = @Queue("work"))
    12     public void receiver2(String message) {
    13         System.out.println("message2 = " + message);
    14     }
    15 }
    16
  3. fanout模式

     1 @Component
     2 public class FanoutConsumer {
     3     /**
     4      * fanout模型
     5      * 1、需要消息队列绑定交换机
     6      * 2、没有必要声明一个队列,需要一个临时队列就可以
     7      *
     8      * @param message
     9      */
    10 11     // 第一个消费者
    12     @RabbitListener(bindings = {
    13             @QueueBinding(    // 监听绑定的队列
    14                     value = @Queue,  // 绑定一个临时队列
    15                     exchange = @Exchange(name = "logs", type = "fanout") // 绑定一个交换机
    16             )
    17     })
    18     public void receiver1(String message) {
    19         System.out.println("message1 = " + message);
    20 21     }
    22 23     // 第二个消费者
    24     @RabbitListener(bindings = {
    25             @QueueBinding(    // 监听绑定的队列
    26                     value = @Queue,  // 绑定一个临时队列
    27                     exchange = @Exchange(name = "logs", type = "fanout") // 绑定一个交换机
    28             )
    29     })
    30     public void receiver2(String message) {
    31         System.out.println("message2 = " + message);
    32 33     }
    34 }
    35 36  
  4. routing 模式

     1 @Component
     2 public class RouteConsumer {
     3     /**
     4      * 根据指定不同的routingkey让不同的消费者来消费信息
     5      * 这个于fanout模型不同之处在于,可以指定不同的消费者
     6      *
     7      * @param message
     8      */
     9     @RabbitListener(bindings = {
    10             @QueueBinding(
    11                     value = @Queue,
    12                     exchange = @Exchange(value = "direcets", type = "direct"),
    13                     key = {"info", "debug"}
    14             )
    15     })
    16     public void receiver1(String message) {
    17         System.out.println("message1 = " + message);
    18 19     }
    20 21     @RabbitListener(bindings = {
    22             @QueueBinding(
    23                     value = @Queue,
    24                     exchange = @Exchange(value = "direcets", type = "direct"),
    25                     key = {"error"}
    26             )
    27     })
    28     public void receiver2(String message) {
    29         System.out.println("message2 = " + message);
    30 31     }
    32 }
    33 34  
  5. topic 模式

    @Component
    public class TopicConsumer {
    ​
        @RabbitListener(bindings = {
                @QueueBinding(
                        value = @Queue,
                        exchange = @Exchange(name = "topics", type = "topic"),
                        key = {"user.*"}
                )
        })
        public void receiver1(String message) {
            System.out.println("message1 = " + message);
    ​
        }
    ​
        @RabbitListener(bindings = {
                @QueueBinding(
                        value = @Queue,
                        exchange = @Exchange(name = "topics", type = "topic"),
                        key = {"user.a", "user.#"}
                )
        })
        public void receiver2(String message) {
            System.out.println("message2 = " + message);
    ​
        }
    }
    ​

     

posted @ 2020-09-09 10:38  小智多磨  阅读(280)  评论(0编辑  收藏  举报