【问题标题】:Using Confirms (publisher acknowledgements) with php-amqplib将 Confirms(发布者确认)与 php-amqplib 一起使用
【发布时间】:2013-09-08 12:39:32
【问题描述】:

我正在尝试弄清楚如何使用 php-amqplib 库来使用 Confirms (Publisher Acknowledgements),这是在 http://rabbitmq.com/ 处推荐用于将 RabbitMQ 与 PHP 结合使用的库。

代码本身没有很好的文档记录,我找不到任何反映我找到的 Java 和 python 接口的接口。

Java 示例是here

我已经在 PHP 源代码中找到了我能想到的各种函数名称组合,但没有找到任何东西。我正在寻找类似的东西:

$channel->confirm_select();

...

$channel->wait();

看起来https://github.com/videlalvaro/php-amqplib/blob/master/PhpAmqpLib/Helper/Protocol/Protocol091.php 支持该功能,但我不知道它是如何暴露给用户的。

【问题讨论】:

  • 我想另一个注意事项是我可以使用两个消息队列来做到这一点,但如果库支持,我宁愿使用 RabbitMQ 内置的功能。

标签: php rabbitmq amqp


【解决方案1】:

我今天在找这个,发现php-amqplib已经实现了。

看看这个demo for details

这是一个sn-p:

$connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $connection->channel();
$channel->set_ack_handler(
    function (AMQPMessage $message) {
        echo "Message acked with content " . $message->body . PHP_EOL;
    }
);
$channel->set_nack_handler(
    function (AMQPMessage $message) {
        echo "Message nacked with content " . $message->body . PHP_EOL;
    }
);
/*
 * bring the channel into publish confirm mode.
 * if you would call $ch->tx_select() befor or after you brought the channel into this mode
 * the next call to $ch->wait() would result in an exception as the publish confirm mode and transactions
 * are mutually exclusive
 */
$channel->confirm_select();
/*
    name: $exchange
    type: fanout
    passive: false // don't check if an exchange with the same name exists
    durable: false // the exchange won't survive server restarts
    auto_delete: true //the exchange will be deleted once the channel is closed.
*/
$channel->exchange_declare($exchange, 'fanout', false, false, true);
$i = 1;
$msg = new AMQPMessage($i, array('content_type' => 'text/plain'));
$channel->basic_publish($msg, $exchange);
/*
 * watching the amqp debug output you can see that the server will ack the message with delivery tag 1 and the
 * multiple flag probably set to false
 */
$channel->wait_for_pending_acks();

【讨论】:

    【解决方案2】:

    显然,PECL AMQP 库和纯 php-amqplib 库都没有实现 Confirms 扩展。此时唯一真正的解决方案是实现两个队列来模拟对发布者的确认。

    计划在这两个库中实现 Confirms,但我没有看到任何进展。

    【讨论】:

    • 两个队列你会怎么做?
    猜你喜欢
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多