RabbitMQ work模式

此博客探讨了如何在RabbitMQ中实现消费者根据自身处理能力按需消费消息的场景。通过设置消费者端的QoS(Quality of Service)和手动确认(ack),可以避免RabbitMQ默认的平均分配消息行为。在生产者发送10条消息到默认交换机和队列后,两个消费者通过调整QoS为1并启用手动ack,确保每个消费者按自己的节奏处理消息,而非同时接收所有消息。
摘要由CSDN通过智能技术生成

一个生产者,一个默认的交换机,一个队列,两个消费者

 

public class WorkTest {

    @Test
    public void pubTest() throws IOException {//发送消息
        Connection connection = MQFactory.getConnection();//建立连接

        Channel channel = connection.createChannel();//创建信道对象



        for (int i = 0; i < 10; i++) {
            channel.basicPublish("","work_queue",null,("hello work:"+i).getBytes());//发送消息
        }


    }

    @Test
    public void conTest1() throws IOException {//接收消息
        Connection connection = MQFactory.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare("work_queue",true,false,false,null);//创建队列

        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {//处理消息的一个回调
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                String mag = new String(body, "utf-8");
                System.out.println("消费者1:"+mag);

            }
        };

        channel.basicConsume("work_queue",true,defaultConsumer);//消费者的消费方法

        System.in.read();

    }

    @Test
    public void conTest2() throws IOException {//接收消息
        Connection connection = MQFactory.getConnection();

        Channel channel = connection.createChannel();


        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {//处理消息的一个回调
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                String mag = new String(body, "utf-8");
                System.out.println("消费者2:"+mag);

            }
        };

        channel.basicConsume("work_queue",true,defaultConsumer);//消费者的消费方法

        System.in.read();


    }
}

 

 

这时 两台消费者拿到的是均分的

只需要在消费者端,添加Qos能力以及更改为手动ack即可让消费者,根据自己的能力去消费指定的消息,而不是默认情况下由RabbitMQ平均分配了,生产者不变,正常发布消息到默认的exchange,并指定routing消费者指定Qoa和手动ack

public class WorkTest {

    @Test
    public void pubTest() throws IOException {//发送消息
        Connection connection = MQFactory.getConnection();//建立连接

        Channel channel = connection.createChannel();//创建信道对象



        for (int i = 0; i < 10; i++) {
            channel.basicPublish("","work_queue",null,("hello work:"+i).getBytes());//发送消息
        }


    }

    @Test
    public void conTest1() throws IOException {//接收消息
        Connection connection = MQFactory.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare("work_queue",true,false,false,null);//创建队列

        channel.basicQos(1);

        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {//处理消息的一个回调
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                String mag = new String(body, "utf-8");
                System.out.println("消费者1:"+mag);

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                channel.basicAck(envelope.getDeliveryTag(),false);

            }
        };

        channel.basicConsume("work_queue",false,defaultConsumer);//消费者的消费方法

        System.in.read();

    }

    @Test
    public void conTest2() throws IOException {//接收消息
        Connection connection = MQFactory.getConnection();

        Channel channel = connection.createChannel();

         // 指定当前消费者,一次消费多少个消息
        channel.basicQos(1);
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {//处理消息的一个回调
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                String mag = new String(body, "utf-8");
                System.out.println("消费者2:"+mag);
                  //手动ack
                //envelope.getDeliveryTag 消息的标识  false 是否批量操作
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };

        channel.basicConsume("work_queue",false,defaultConsumer);//消费者的消费方法

        System.in.read();


    }
}

 

手动ack  跟设置当前消费者一次消费多少消息  就可以看自己性能接收消息

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值