好了,这是Symfony2 Commandline脚本的一部分。当脚本正在等待时,它会异常终止。
[PhpAmqpLib'Exception'AMQPRuntimeException]
Error reading data. Received 0 instead of expected 1 bytes
我搜索了谷歌,发现提到心跳和set_time_out,但是当我试图在构造函数中设置时,而是当我从默认值更改它时,它死得更快。
这就是我的脚本设置。
// Get the configuration options for RabbitMQ
$queueConfig = $this->getApplication()->getKernel()->getContainer()->getParameter("rabbit_mq");
/**
* Callback function for RabbitMQ
* Everything in the callback function must remain in the call back function.
*/
$callback = function($msg)
{
$msgObj = json_decode($msg->body, true);
print_r($msgObj);
};
$connection = new AMQPConnection(
$queueConfig['response']['host'],
$queueConfig['response']['port'],
$queueConfig['response']['username'],
$queueConfig['response']['password'],
'/'
);
$channel = $connection->channel();
$queueName = 'myQueueName';
$channel->basic_consume($queueName, '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
这是来自AMQPStreamConnection.php的构造函数。AMQPConnection扩展了AMQPStreamConnection
public function __construct($host, $port,
$user, $password,
$vhost="/",$insist=false,
$login_method="AMQPLAIN",
$login_response=null,
$locale="en_US",
$connection_timeout = 3,
$read_write_timeout = 3,
$context = null)
关于如何摆脱错误的想法?
这个问题的原因是我们使用的是Amazon的负载平衡器,它在一定的时间间隔内终止了我们的连接。我所做的是黑了一个while循环,当它失败时重新启动消费者。