pringAMQP 的WorkQueue队列

SpringAMQP 的WorkQueue队列

在这里插入图片描述
工作队列,用来提高消息处理速度,避免队列消息堆积,一条通知由多个consumer监听
发送消息给队列,测试work工作模式

 @Test
    public void testSend2WorkQueue(){
        // 队列名
        String queueName = "simple.queue";
        // 消息
        String message = "Hello,world and SpringAMQP!!!——————";
        // 发送50条消息给队列
        for (int i = 1; i <= 50; i++) {
            rabbitTemplate.convertAndSend(queueName,message+i);
			Thread.sleep(20);
        }
    }

指定俩个队列

// 指定队列
    @RabbitListener(queues = "simple.queue")
    public void getWork1(String msg) throws InterruptedException {
        System.out.println("1==监听到的消息:--->"+msg);
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void getWork2(String msg) throws InterruptedException {
        System.err.println("2==监听到的消息:--->"+msg);
        Thread.sleep(200);
    }

结果是两个队列监听的数据平均分,这不符合我们需要的要求,我们要求的是能力强的服务可以多处理,能力低的少处理

原因是这个队列默认有个预取数据,默认就把数据平分了,取到的消息要一一处理完才行,

修改方法改yaml配置文件
spring:
  rabbitmq:
    host: 192.168.88.135
    port: 5672
    username: itcast
    password: 123321
    virtual-host: /
    listener:
      simple:
        prefetch: 1 # 修改的预取消息控制,能先拿到的队列就优先处理
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值