dqmhgz5848 2019-06-20 15:03
浏览 2304

如何检查提供给AMQPStreamConnection的信息在RabbitMQ中是否有效?

I wrote a custom error logging method and I'm implementing it where things could go wrong. For RabbitMQ however, it's not clear for me how to check for an error. This is my code

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$errorServiceObject = new ErrorService();
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
if(!isset($channel))
{
    $errorServiceObject->LogError('RabbitMQ error.', 'Could not initiate a RabbitMQ connection.');
    exit();
}

The way I do it at the moment is bad because it only checks whether the variable $channel is set or when the connection is simply not initiated correctly. I would like to check whether the new AMQPStreamConnection information are valid or not. Consider this example where I do this

$connection = new AMQPStreamConnection('localhostWithATypo', 5444, 'guest', 'guest'); //wrong hostname and wrong port number. What would I need to put in the if statement to trigger the error handeling function?

  • 写回答

1条回答 默认 最新

  • doutang1873 2019-06-20 16:35
    关注

    If you use php-amqplib, take a look at the documentation. There are some code snippets that illustrate how new AMQPStreamConnection throws exceptions. See https://github.com/php-amqplib/php-amqplib#connection-recovery.

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器