RabbitMQ:setReturnListner handleBasicReturn nt被调用未传递的消息

时间:2011-01-06 09:20:11

标签: rabbitmq

对于其中一项要求,我们需要跟踪队列深度和成功处理的消息。我们的想法是发布消息并获取成功和失败消息的列表。为了模拟要求,我做了以下

  1. 发送包含Mandatory和Immediate标志的邮件channel.basicPublish'exchange','rKey',true,false,props,“Hello World”.bytes
  2. 消费者甚至消费标记(我已将1..10中的数字作为每条消息标题中的标记值)并且不会确认奇数消息。
  3. 我在发布商中实现了setReturnListnere来捕获未传递的邮件。
  4. 虽然能够通过Rabbmitmqctl list_queues messages_unacknowledged获取unack消息的数量,但我的handleBasicReturn方法不会被调用。我错过了什么。

    代码段:

    制片:

    channel.setReturnListener(new ReturnListener() {
        public void handleBasicReturn(int replyCode, String replyText, String exchange,
                                      String routingKey, AMQP.BasicProperties properties,
                                      byte[] body) 
                throws IOException {
            println "Debugging messages!!!!"
            println "The details of returned messages are ${replyText} from  ${exchange} with routingKey as ${routingKey} with properties"
        }
    });
    
    println " queuename is ${dec.queue} and consumerCount is ${dec.consumerCount} messageCount is ${dec.messageCount}"
    (1..10).each {
        println "Sending file ${i}....."
        def headers = new HashMap<String,Object>()
        headers.put "operatiion","scp"
        headers.put "dest","joker.dk.mach.com"
        headers.put "id", i
        println headers
    
        BasicProperties props = new BasicProperties(null, null, headers, null, null, null, null, null,null, null, null, null,null, null)
                        channel.basicPublish 'exchange' ,'rKey',true,false, props,"Hello Worls".bytes                                
        i++                         
    }
    channel.close()
    

    消费者:

    while (true) {
        def delivery = consumer.nextDelivery()
        def headers = delivery?.properties?.headers
        def id = headers.get("id")
        println "Received message:"
        println " ${id.toString()}"
    
        if( id % 2 == 0){
            channel.basicAck delivery.envelope.deliveryTag, false                
        }    
    }
    

1 个答案:

答案 0 :(得分:0)

阅读this explanation,了解即时标志和强制标志如何影响RabbitMQ中的消息传递。

在您的情况下,由于您有一个消费者接收消息,即使消费者从不回复消息,也不会返回消息。

d