rabbitmq基础-java-2、work模型

本文介绍了SpringAMQP中的WorkQueue模式,通过生产者将消息发送到队列,多个消费者并发处理。实验中通过设置`prefetch`值控制消费者同时处理的消息数量,展示了WorkQueue模式如何提高效率和避免竞争冲突。
摘要由CSDN通过智能技术生成

1、简介

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

2、消息发送

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

 @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、消息接收

@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、测试

消费者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、再测试

消费者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来控制消费者预取消息数量

  • 20
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清爽的暗之妖刀0369

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值