欢迎大家关注我公众号“从零开始的it转行生”

rabbitmq的优先消费者

原文地址:https://www.jianshu.com/writer#/notebooks/48316575/notes/79639038

rabbitmq的优先消费者

有些时候我们会开发调试本地连接开发环境的数据库。
这个时候有关mq的调试就很麻烦,因为dev环境的消费者也会消费rabbitmq的消息,一般的办法是多发几次消息,让消息轮训到自己的消费者。

优先消费者

rabbitmq3.2以上可以对消费者进行排序。

springboot方案

对于springboot工程@RabbitListener,里面有一个参数priority,这个是配置消费者的优先级,默认为0,数值越大优先级越高。

原生api方案

如果不是使用springboot工程,用原生的api,可以在创建消费者的时候传入参数x-priority

        Map<String, Object> map = new HashMap<>();
        map.put("x-priority",5);
        channel.basicConsume("queueName",true,map,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body)
                    throws IOException {
                // no work to do
            }
        });
posted @ 2020-11-09 10:34  大佬健  阅读(424)  评论(0编辑  收藏  举报

欢迎大家关注我公众号“从零开始的it转行生”