rabbitmq基础-java-2、work模型

1、简介

工作队列模式(Work Queue Mode):在这个模型中,生产者同样将消息发送到队列,但多个消费者可以从队列中获取消息并发处理。这意味着不同的消费者可以独立地处理各自的任务,从而提高效率。

2、消息发送

循环发送,模拟大量消息堆积现象。 在publisher服务中的SpringAmqpTest类中添加一个测试方法:

java 复制代码
 @Test
    void testWorkQueue() throws InterruptedException {
        String queueName = "hello.queue2";
        for (int i = 1; i <= 50; i++) {
            String msg = "hello, worker, message_" + i;
            rabbitTemplate.convertAndSend(queueName, msg);
            Thread.sleep(20);
        }
    }

3、消息接收

java 复制代码
@RabbitListener(queues = "hello.queue2")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1 收到了 hello.queue2的消息---:【" + msg +"】");
        Thread.sleep(20);
    }

    @RabbitListener(queues = "hello.queue2")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2 收到了 hello.queue2的消息...... :【" + msg +"】");
        Thread.sleep(200);
    }

4、测试

java 复制代码
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_1】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_2】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_4】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_6】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_8】
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_3】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_10】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_12】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_14】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_16】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_18】
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_5】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_20】
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_7】
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_9】
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_11】
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_13】
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_15】
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_17】
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_19】

5、能者多劳

在spring中有一个简单的配置,可以解决这个问题。我们修改consumer服务的application.yml文件,添加配置:

spring:

rabbitmq:

listener:

simple:

prefetch: 1 # 每次只能获取一条消息,处理完成才能获取下一个消息

6、再测试

java 复制代码
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_1】
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_2】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_3】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_4】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_5】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_6】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_7】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_8】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_9】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_10】
消费者2 收到了 hello.queue2的消息...... :【hello, worker, message_11】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_12】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_13】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_14】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_15】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_16】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_17】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_18】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_19】
消费者1 收到了 hello.queue2的消息---:【hello, worker, message_20】

7、总结

Work模型的使用:

  • 多个消费者绑定到一个队列,同一条消息只会被一个消费者处理

  • 通过设的置prefetch来控制消费者预取消息数量

相关推荐
今***b10 分钟前
Python 操作 PPT 文件:从新手到高手的实战指南
java·python·powerpoint
David爱编程12 分钟前
volatile 关键字详解:轻量级同步工具的边界与误区
java·后端
fatfishccc2 小时前
Spring MVC 全解析:从核心原理到 SSM 整合实战 (附完整源码)
java·spring·ajax·mvc·ssm·过滤器·拦截器interceptor
没有bug.的程序员3 小时前
MyBatis 初识:框架定位与核心原理——SQL 自由掌控的艺术
java·数据库·sql·mybatis
执键行天涯3 小时前
从双重检查锁定的设计意图、锁的作用、第一次检查提升性能的原理三个角度,详细拆解单例模式的逻辑
java·前端·github
程序员江鸟3 小时前
Java面试实战系列【JVM篇】- JVM内存结构与运行时数据区详解(私有区域)
java·jvm·面试
架构师沉默3 小时前
Java 状态机设计:替代 if-else 的优雅架构
java·程序员·架构
java亮小白19973 小时前
Spring Cloud 快速通关之Sentinel
java·spring cloud·sentinel
atwednesday3 小时前
大规模文档预览的架构设计与实现策略
java
Dioass4 小时前
Java面向对象中你大概率会踩的五大隐形陷阱
java