3

http://rabbitmq.com/で PHP で RabbitMQ を使用するために推奨されるライブラリである php-amqplib ライブラリを使用して、 Confirms ( Publisher Acknowledgments ) を使用する方法を理解しようとしています。

コード自体は十分に文書化されておらず、私が見つけた Java および python インターフェースのインターフェースを反映するものは何も見つかりません。

Java の例はここにあります。

PHP ソースで考えられる関数名のさまざまな組み合わせを調べましたが、何も見つかりませんでした。私は次のようなものを探しています:

$channel->confirm_select();

...

$channel->wait();

https://github.com/videlvaro/php-amqplib/blob/master/PhpAmqpLib/Helper/Protocol/Protocol091.phpは機能をサポートしているようですが、ユーザーにどのように公開されているかわかりません。

4

2 に答える 2

3

今日これを探していたところ、php-amqplibが実装されていることがわかりました。

詳細については、このデモをご覧ください

ここにスニペットがあります:

$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();
于 2016-12-15T08:16:31.507 に答える