rabbitmq设置消费者优先


1、多消费者的时候,mq的调试很麻烦!因为dev,local环境的消费者也会消费rabbitmq的消息,一般的办法是多发几次

优先消费者

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

springboot方案

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

@RabbitListener(queues="${xxQueueName}",containerFactory = "xxxContainerFactory",priority = "3")
public void onMessage(Message message,Channel channel) throws Exception {
	try catch finally
}

原生api方案

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


 Map 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 {
            }
        });