【问题标题】:Keep messages from ACKing on get() using pecl-amqp使用 pecl-amqp 防止消息在 get() 上 ACKing
【发布时间】:2011-06-11 18:13:18
【问题描述】:

我正在尝试将 pecl-amqp 用于我的项目。我在 ACK 过程中遇到了困难。我需要手动确认从队列中收到的每条消息,但在检索消息时这些消息似乎是自动确认的。

我已将队列设置为 AMQP_NOACK 并正在使用 AMQPQueue->get(AMQP_NOACK) 但似乎没有任何影响,消息仍然从队列中删除,而我没有发送 AMQPQueue->ack()。

如果有人对 pecl-amqp 有任何经验,我将不胜感激。

【问题讨论】:

    标签: php rabbitmq amqp


    【解决方案1】:

    我也遇到过同样的问题,但我设法让确认机制使用 consume 方法工作。使用 rabbitmqctl 列出队列中的条目似乎可以正常工作,尽管我似乎以与发送消息的顺序不同的顺序从队列中获取消息 - 这会破坏队列的对象。我的代码如下:

    // Create the queue to be:
    //      AMQP_DURABLE - messages will withstand a broker restart (i.e. they are written to disk).
    //      AMQP_NOACK - when consumed messages will not be marked as delivered until an explicit ACK is received.
    $q->declare($queueName, AMQP_DURABLE | AMQP_NOACK );
    
    // Bind it on the exchange to routing key.
    $q->bind($exchangeName, $routingKey);
    
    // Set the options for our consumption of the messages:
    //  Get a minimum of 0 msg.
    //  Get a maximum of 1 msg.
    //  Don't ACK the message on consumption i.e. explicitly acknoledge later.
    $options = array(
     'min' => 0,
     'max' => 1,
     'ack' => false
    );
    // Get the messages
    $results_array = $q->consume($options);
    
    // show the message
    print_r($results_array);
    $delivery_tag =  $results_array[0]['delivery_tag'];
    echo 'delivery_tag: [' . $delivery_tag . "].\r\n";
    
    // Acknowledge receipt of the message.
    $q->ack($delivery_tag);
    

    【讨论】:

    • 这看起来不对:$q->declare($queueName, AMQP_DURABLE + AMQP_NOACK ); - AMQP_* 常量是位掩码,如果你想同时设置它们,你必须使用 AMQP_DURABLE & AMQP_NOACK 代替。
    • 还解决了我遇到的乱序问题。这是由于在我处理完所有等待的消息之前关闭了连接。它似乎导致等待消息被重新传递(它们的重新传递计数上升)并且从我在这里阅读的内容 (lists.rabbitmq.com/pipermail/rabbitmq-discuss/2010-June/…) 重新传递消息时不会保留消息的顺序。
    • 传递位掩码时,您需要使用|(按位或)而不是&(按位与)。现在您同时传递AMQP_DURABLEAMQP_NOACK 中位的掩码。可能没有。
    【解决方案2】:

    作为参考,AMQP 扩展没有正确支持 AMQP_NOACK。你可以让它做你想做的事,但它并不漂亮。我现在正在努力解决这个问题。仅供参考,AMQP_NOACK 意味着您以后不必再回来确认消息,即服务器将消息发送给客户端后,服务器将消息标记为已确认。对此有些困惑,所以我想澄清一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-11
      • 2015-04-30
      • 2012-03-20
      • 1970-01-01
      • 2019-11-16
      • 2016-10-13
      • 2020-10-25
      • 1970-01-01
      相关资源
      最近更新 更多