php如何用libevent处理rabbitmq发来的消息,防止消息丢失或者人为的中断导致消息没有被处理完整

之前是用官方提供的方法来处理的

``
$channel->basic_consume('hello', '', false, true, false, false, $callback);

while(count($channel->callbacks)) {

$channel->wait();

}
``

发现中断程序的时候 每个消息没有被完全处理完,比如拿到消息还没有处理完入库就被中断了。

后来打算用libevent 据说在事件处理中,除非强制kill掉,不然事件会处理完再中断,但是以下代码还是不断报错;

``
$this->connection = new AMQPStreamConnection($config['host'], $config['port'], $config['user'], $config['password'], $config['vhost']);

        $this->channel = $this->connection->channel();;
        $this->channel->queue_declare($queue, false, true, false, false);

// $this->channel->basic_consume($queue, '', false, true, false, false, function ($msg) {
// set_trace_prefix(md5($msg->body . time()));
// trace('receive msg:' . $msg->body);
// trace('done');
// });

        $ch = $this->channel;
        $fd = $this->connection->getSocket();
        Worker::$globalEvent->add($fd, EventInterface::EV_READ, function ($fd) use ($queue, $ch){
            $msg = $ch->basic_get($queue, true);
            print_r($msg);
            print("get a messge:\n");

        });
        ``
阅读 2.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题