RabbitMQ消费者回调函数handleDelivery无法执行

RabbitMQ消费者回调函数handleDelivery无法执行,控制台无输出

背景

在一次学习rabbitmq时,创建了生产者、消费者,在消费者消费消息后在回调函数中打印相关信息,无法执行
代码如下

Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumerTag:" + consumerTag);
                System.out.println("exchange:" + envelope.getExchange());
                System.out.println("routingkey:" + envelope.getRoutingKey());
                System.out.println("properties:" + properties);
                System.out.println("body" + new String(body));
            }
        };

运行之后控制台无输出

解决方案

方法一 排查方法参数类型

@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body){}

第三个参数应为AMQP.BasicProperties,而不是BasicProperties

方法二 程序提前结束,导致回调函数无法执行

笔者是直接在main()方法运行消费者代码,当main方法线程结束时,回调函数没来得及执行,程序就结束了,可以方法最后面写个死循环while(ture){}来保证程序一直处于运行状态,实际生产中运行在服务器中不会有类似问题


版权声明:本文为qq_38668544原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。