工作队列模式结构图:

这种模式非常适合多个工人等待任务到来的场景。任务有多个,一个一个丢进消息队列,工人也有很多个,就可以将这些任务分配个各个工人,让他们各自负责一些任务,并且做的快的工人还可以多完成一些(能者多劳)。

要实现这种模式,只需要创建多个监听器即可。

先监听再发消息

这里先介绍先监听再发送消息的情况。

  1. 定义配置类。

    import org.springframework.amqp.core.Binding;
    import org.springframework.amqp.core.BindingBuilder;
    import org.springframework.amqp.core.Exchange;
    import org.springframework.amqp.core.ExchangeBuilder;
    import org.springframework.amqp.core.Queue;
    import org.springframework.amqp.core.QueueBuilder;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration; /**
    * RabbitMQ配置类
    *
    * @author CodeSail
    */
    @Configuration
    public class RabbitMqConfig { /**
    * 定义交换机,可以很多个
    * @return 交换机对象
    */
    @Bean("directExchange")
    public Exchange exchange(){
    return ExchangeBuilder.directExchange("amq.direct").build();
    } /**
    * 定义消息队列
    * @return 消息队列对象
    */
    @Bean("testQueue")
    public Queue queue(){
    return QueueBuilder
    // 非持久化类型
    .nonDurable("test_springboot")
    .build();
    } /**
    * 定义绑定关系
    * @return 绑定关系
    */
    @Bean
    public Binding binding(@Qualifier("directExchange") Exchange exchange,
    @Qualifier("testQueue") Queue queue){
    // 将定义的交换机和队列进行绑定
    return BindingBuilder
    // 绑定队列
    .bind(queue)
    // 到交换机
    .to(exchange)
    // 使用自定义的routingKey
    .with("test_springboot_key")
    // 不设置参数
    .noargs();
    }
    }
  2. 创建两个监听器,监听同一队列。

    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component; /**
    * 工作队列监听器
    *
    * @author 廖航
    */
    @Component
    public class WorkListener { @RabbitListener(queues = "test_springboot")
    public void receiver1(String message) {
    System.out.println("1号监听器:" + message);
    } @RabbitListener(queues = "test_springboot")
    public void receiver2(String message) {
    System.out.println("2号监听器:" + message);
    } }
  3. 定义生产者。

    import org.junit.jupiter.api.Test;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import java.util.concurrent.TimeUnit; @SpringBootTest
    class RabbitMqSpringBootTests { /**
    * RabbitTemplate封装了大量的RabbitMQ操作,已经由Starter提供,因此直接注入使用即可
    */
    @Autowired
    private RabbitTemplate rabbitTemplate; /**
    * 生产者
    */
    @Test
    void producer() throws InterruptedException { for (int i = 0; i < 10; i++) {
    TimeUnit.SECONDS.sleep(1);
    rabbitTemplate.convertAndSend("amq.direct", "test_springboot_key", "Hello World");
    }
    } }
  4. 启动生产者发送消息。

    可以看到,当同一个队列有多个监听器时,默认采用轮询的方式消费消息。

先发消息再监听

前面介绍了先监听再发消息的情况,下面介绍先发消息再监听的情况。

  1. 向队列中发送10条消息。

  2. 启动服务消费消息。

    可以看到,如果是一开始就存在消息,会被一个消费者一次性全部消费,这是因为没有对消费者的Prefetch count(预获取数量,一次性获取消息的最大数量)进行限制。

    默认的Prefetch count为250。

    也就是说如果希望消费者一次只拿一个消息,而不是将所有的消息全部都获取,需要设置Prefetch count。

要对这个数量进行配置,需要在配置类中定义一个自定义的ListenerContainerFactory,可以在这里设置消费者Channel的PrefetchCount的大小。

  1. 配置类中定义ListenerContainerFactory

    import org.springframework.amqp.core.Binding;
    import org.springframework.amqp.core.BindingBuilder;
    import org.springframework.amqp.core.Exchange;
    import org.springframework.amqp.core.ExchangeBuilder;
    import org.springframework.amqp.core.Queue;
    import org.springframework.amqp.core.QueueBuilder;
    import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
    import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration; /**
    * RabbitMQ配置类
    *
    * @author CodeSail
    */
    @Configuration
    public class RabbitMqConfig { @Autowired
    private CachingConnectionFactory connectionFactory; ... @Bean(name = "listenerContainer")
    public SimpleRabbitListenerContainerFactory listenerContainer(){
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    // 将PrefetchCount设定为1表示一次只能取一个
    factory.setPrefetchCount(1);
    return factory;
    }
    }
  2. 监听器中指定。

    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component; /**
    * 工作队列监听器
    *
    * @author 廖航
    */
    @Component
    public class WorkListener { @RabbitListener(queues = "test_springboot", containerFactory = "listenerContainer")
    public void receiver1(String message) {
    System.out.println("1号监听器:" + message);
    } @RabbitListener(queues = "test_springboot", containerFactory = "listenerContainer")
    public void receiver2(String message) {
    System.out.println("2号监听器:" + message);
    } }
  3. 向队列中发送10条消息。

  4. 启动服务消费消息。

    可以看到,两个监听器又实现了轮询消费消息。

    Prefetch count设为了1。

至此,工作队列模式的两种情况就介绍完毕了。


RabbitMQ 06 工作队列模式的更多相关文章

  1. RabbitMQ六种队列模式-工作队列模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列 [本文]RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...

  2. 【RabbitMQ】工作模式介绍

    一.前言 之前,笔者写过< CentOS 7.2 安装 RabbitMQ> 这篇文章,今天整理一下 RabbitMQ 相关的笔记便于以后复习. 二.模式介绍 在 RabbitMQ 官网上提 ...

  3. RabbitMQ六种队列模式-简单队列模式

    前言 RabbitMQ六种队列模式-简单队列 [本文]RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...

  4. RabbitMQ六种队列模式-发布订阅模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅 [本文]RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...

  5. RabbitMQ六种队列模式-路由模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式 [本文]RabbitMQ六种队列模式-主 ...

  6. RabbitMQ六种队列模式-主题模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...

  7. java面试一日一题:rabbitMQ的工作模式

    问题:请讲下rabbitMQ的工作模式 分析:该问题纯属概念题,需要掌握rabbtiMQ的基础知识,同时该题也是切入MQ的一个引子: 回答要点: 主要从以下几点去考虑, 1.rabbitMQ的基本概念 ...

  8. RabbitMQ之消息模式(下)

    目的: RabbitMQ之消息模式(上):https://www.cnblogs.com/huangting/p/11994539.html 消费端限流 消息的ACK与重回队列 TTL消息 死信队列 ...

  9. 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_9.RabbitMQ研究-工作模式-发布订阅模式-消费者

    消费者需要写两个消费者 定义邮件的类 复制以前的代码到邮件类里面进行修改 最上面 声明队列的名称和交换机的名称 监听修改为email 的队列的名称 手机短信接收端 复制一份email的接收端的代码 改 ...

  10. 【RabbitMQ】六种模式与SpringBoot整合

    添加rabbitmq的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

随机推荐

  1. 【Java复健指南02】方法的注意事项

    [方法] 方法基本内容 √访问修饰符 ​ (作用是控制方法使用的范围) ​ 可选,[有四种:public\protected\默认\private],具体在后面说 √返回类型 ​ 1.一个方法最多有一 ...

  2. Elasticsearch使用实战以及代码详解

    Elasticsearch 是一个使用 Java 语言编写.遵守 Apache 协议.支持 RESTful 风格的分布式全文搜索和分析引擎,它基于 Lucene 库构建,并提供多种语言的 API.El ...

  3. 【Azure 事件中心】Event Hub 消费端出现 Timeout Exception,errorContext中 LINK_CREDIT为0的解释

    问题描述 在使用Event Hub SDK消费数据过程中,出现大量的Timeout Exception,详细消息为: com.microsoft.azure.eventhubs.TimeoutExce ...

  4. GDB调试入门笔记

    目录 What? Why How 安装GDB 安装命令 查看是否安装成功 调试简单的程序 预备一个程序 调试 使用 break info list next print step 一些小技巧 在gdb ...

  5. 在winform中如何嵌入第三方软件窗体✨

    相关win32api的学习 SetParent [DllImport("user32.dll ", EntryPoint = "SetParent")] pri ...

  6. elementPlus使用el-icon

    按着文档来撒 yarn add @element-plus/icons-vue main.ts import * as ElementIcons from '@element-plus/icons-v ...

  7. [学习笔记]Linux + Nginx环境下部署 Web 站点

    ​部署后端程序,请阅读: [学习笔记]Linux环境下部署 .Net5 程序 - 林晓lx - 博客园 (cnblogs.com) 打包项目 以Vue项目为例,首先打包站点,前往项目的根目录并键入: ...

  8. 魅族16T屏幕尺子

  9. Git进阶命令-reset

    之前有关Git,写过一片文章: Git五个常见问题及解决方法 一.reset命令使用场景 有时候我们提交了一些错误的或者不完善的代码,需要回退到之前的某个稳定的版本,面对这种情况有两种解决方法: 解决 ...

  10. android使用StringRequest访问网站接收JSON时空指针报错

    報錯詳情: 代碼詳情: 报错原因:他丫变量名与JSON格式中的变量名不同