• springboot整合rabbitmq入门(三)


    在上一篇文章中介绍了rabbitmq的fanout模式。今天继续学习另一种模式——direct模式。这种模式是rabbitmq的最简单一种模式。

    首先创建一个名为helloDirect1的对列

    1. @Configuration
    2. public class DirectRabbitConfig {
    3. @Bean
    4. public Queue directA(){
    5. return new Queue("helloDirect1");
    6. }
    7. }

    创建发送端

    1. @Component
    2. public class DirectSend {
    3. @Autowired
    4. private RabbitTemplate rabbitTemplate;
    5. public void send(String context){
    6. System.out.println("Sender:" + context);
    7. // 这里需要指定发送的消息队列
    8. this.rabbitTemplate.convertAndSend("helloDirect1",context);
    9. }
    10. }

    接受端

    1. @Component
    2. @RabbitListener(queues = "helloDirect1")
    3. public class DirectReceiveA {
    4. @RabbitHandler
    5. public void receive(String context){
    6. System.out.println("direct.A"+context);
    7. }
    8. }

    在这里,我有点不是特别清楚的地方,就是rabbitmq中说的是direct模式不需要exchange,只需要绑定routingKey即可,即

    this.rabbitTemplate.convertAndSend("helloDirect1",context);中第一个参数为routingKey,但是我在写代码过程发现其实这块应该写的是你想发送到哪个对列,就写这个对列名称。不知道这样理解对不对,如果有不对的地方请指出。

    控制台打印:

    说明我们接受成功。

    在写代码过程中突然想到,@RabbitListener注解是不是可以同时监听到多个对列呢?

    在创建一个为ceshi的对列

    1. @Bean
    2. public Queue directB(){
    3. return new Queue("ceshi");
    4. }

    同时监听多个对列

    1. @Component
    2. @RabbitListener(queues = {"helloDirect1","ceshi"})
    3. public class DirectReceiveA {
    4. @RabbitHandler
    5. public void receive(String context){
    6. System.out.println("direct.A"+context);
    7. }
    8. }

    发送端

    1. @Component
    2. public class DirectSend {
    3. @Autowired
    4. private RabbitTemplate rabbitTemplate;
    5. public void send(String context){
    6. System.out.println("Sender:" + context);
    7. // 这里需要指定发送的消息队列
    8. this.rabbitTemplate.convertAndSend("ceshi",context);
    9. this.rabbitTemplate.convertAndSend("helloDirect1",context);
    10. }
    11. }

    控制台打印

    成功

    当生产者和消费者是一对多时,会是什么样的效果呢?

    在创建一个消费端同时监听helloDirect1对列

    1. @Component
    2. @RabbitListener(queues = "helloDirect1")
    3. public class DirectReceiveB {
    4. @RabbitHandler
    5. public void receive(String context){
    6. System.out.println("direct.B"+context);
    7. }
    8. }

    改一下测试类,循环发送10遍,看效果

    1. @Test
    2. public void DirectSend(){
    3. for (int i=0;i<10;i++){
    4. directSend.send("测试"+i);
    5. }
    6. }

    但是如果一个消费者消费快,另一个慢的话回怎么样呢?

    我们对消费者B休眠2秒,看效果

    1. @Component
    2. @RabbitListener(queues = "helloDirect1")
    3. public class DirectReceiveB {
    4. @RabbitHandler
    5. public void receive(String context){
    6. try {
    7. Thread.sleep(2000);
    8. } catch (InterruptedException e) {
    9. e.printStackTrace();
    10. }
    11. System.out.println("direct.B"+context);
    12. }
    13. }

    会发现A消费者消费结束了,但B消费者还没有完成。这就造成了A消费者空闲下来了,资源浪费。

  • 相关阅读:
    分布式架构下如何选择最佳 Store?
    c++ std::mutex 多个线程同时访问的同步原语
    【英语:语法基础】C7.日常对话-校园生活
    C#多线程学习(二) 如何操纵一个线程
    rtl 开发必会技能
    LoadRunner介绍和脚本录制
    C语言题解 | 移除元素(多种解法)
    C# Modbus 通讯
    20.6 OpenSSL 套接字分发RSA公钥
    小游戏在提升用户留存方面的作用
  • 原文地址:https://blog.csdn.net/java_chegnxuyuan/article/details/90048624