我正在处理一个巨大的 XML 文档(其中包含约一百万条记录),然后使用 RabbitMQ 导入格式化后的版本到数据库。每次发布约 200,000 条记录后,我都会收到一个“broken pipe”错误,并且 RabbitMQ 无法从中恢复。
以下是我的类方法:
这随后会导致节点故障错误,需要手动杀死进程才能从中恢复。注意错误:fwrite():发送 2651 字节失败,errno=11,资源暂时不可用于[/var/www/ribbon/app/Console/Command/lib/php_amqplib/amqp.inc,第 439 行]
注意错误:fwrite():发送 33 字节失败,errno=104,连接被对等方重置于[/var/www/ribbon/app/Console/Command/lib/php_amqplib/amqp.inc,第 439 行]
注意错误:fwrite():发送 19 字节失败,errno=32,管道已断开于[/var/www/ribbon/app/Console/Command/lib/php_amqplib/amqp.inc,第 439 行]
以下是我的类方法:
public function publishMessage($message) {
if (!isset($this->conn)) {
$this->_createNewConnectionAndChannel();
}
try {
$this->ch->basic_publish(
new AMQPMessage($message, array('content_type' => 'text/plain')),
$this->defaults['exchange']['name'],
$this->defaults['binding']['routing_key']
);
} catch (Exception $e) {
echo "Caught exception : " . $e->getMessage();
echo "Creating new connection.";
$this->_createNewConnectionAndChannel();
$this->publishMessage($message); // try again
}
}
protected function _createNewConnectionAndChannel() {
if (isset($this->conn)) {
$this->conn->close();
}
if(isset($this->ch)) {
$this->ch->close();
}
$this->conn = new AMQPConnection(
$this->defaults['connection']['host'],
$this->defaults['connection']['port'],
$this->defaults['connection']['user'],
$this->defaults['connection']['pass']
);
$this->ch = $this->conn->channel();
$this->ch->access_request($this->defaults['channel']['vhost'], false, false, true, true);
$this->ch->basic_qos(0 , 20 , 0); // fair dispatching
$this->ch->queue_declare(
$this->defaults['queue']['name'],
$this->defaults['queue']['passive'],
$this->defaults['queue']['durable'],
$this->defaults['queue']['exclusive'],
$this->defaults['queue']['auto_delete']
);
$this->ch->exchange_declare(
$this->defaults['exchange']['name'],
$this->defaults['exchange']['type'],
$this->defaults['exchange']['passive'],
$this->defaults['exchange']['durable'],
$this->defaults['exchange']['auto_delete']
);
$this->ch->queue_bind(
$this->defaults['queue']['name'],
$this->defaults['exchange']['name'],
$this->defaults['binding']['routing_key']
);
}
非常感谢您的帮助。