如何捕获失败的消息发布(RabbitMQ)?

时间:2017-03-04 01:59:55

标签: php rabbitmq

basic_publish()方法上捕获错误的最佳方式是什么?

<?php
require_once __DIR__ . '/vendor/.   autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'hello');
echo " [x] Sent 'Hello World!'\n";
$channel->close();
$connection->close();
?>

1 个答案:

答案 0 :(得分:0)

您可以简单地使用try-catch方法


    ...
    try {
        $channel->basic_publish($msg, '', 'hello');
    } catch (Error $e) {
        // do something
    }