rabbitMQ定时关闭

PhpAmqpLib这个库wait 的时候,有时候会自己退出。为了保证接口稳定,可以建立一个定时任务,定时调用接收消息的接口。此时则需要rabbitMQ在一定时间内关闭通道。可以为wait参数设置第三个参数timeout。
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('wpjam.shop.notice', false, false, false, false);
$callback = function ($msg) {
    $res = $this->sendService()->sendNotice($msg->body);
    echo ' [x] Received ', json_encode($res), "\n";
};
$channel->basic_consume('wpjam.shop.notice', '', false, true, false, false, $callback);
$timeout = 55;
while (count($channel->callbacks)) {
    try{
        $channel->wait(null, false , $timeout);
    }catch(\PhpAmqpLib\Exception\AMQPTimeoutException $e){
        $channel->close();
        $connection->close();
        exit;
    }
}

猜你喜欢

转载自blog.csdn.net/xyy_forever/article/details/82012468
今日推荐