php从rabbitmq错误中获取消息

时间:2012-03-15 07:13:58

标签: php rabbitmq

我的amqp扩展版本是1.0.1& AMQP协议版本为0-9-1

从队列中获取消息:

<?php
try {
$conn = new AMQPConnection() ;
$conn->setLogin('guest') ;
$conn->setPassword('guest') ;
$conn->connect() ;
if ($conn->isConnected()) {
    $channel = new AMQPChannel($conn) ;
    if ($channel->isConnected())
    {
        $queue = new AMQPQueue($channel) ;
        $queue->setName('test_queue') ;
        $queue->setFlags(AMQP_DURABLE | AMQP_AUTODELETE) ;
        $queue->declare() ;
        $messages = $queue->get(AMQP_AUTOACK) ;
        print_r($messages->getBody()) ;
    }
} else {
    echo "connect failure ... " ;
}
$conn->disconnect() ;} catch (Exception $e) {
echo $e->getMessage() ;}?>

它不起作用..

Server channel error: 406, message: PRECONDITION_FAILED - parameters for queue 'test_queue' in vhost '/' not equivalent

2 个答案:

答案 0 :(得分:8)

在我看来,队列已经存在,并且之前在vhost中使用不同的参数声明(创建)了。每次都需要使用相同的参数准确地声明队列(或者删除并使用所需的参数重新创建)。尝试通过管理插件(http://www.rabbitmq.com/management.html)删除队列,然后再次运行脚本

答案 1 :(得分:1)

如果您的队列已经创建,则不需要创建它(使用&#39;声明&#39;方法)并再次与Exchange绑定。恕我直言,你不应该这样做:a)这些行动需要管理特权b)它只够一次c)你可能没有生产的管理权限,你的代码将被打破。 我相信用管理控制台或任何其他你喜欢的工具创建和绑定所有必需的队列然后以这种方式接收消息会更好

// consider using connection more than once. that's only for illustration purposes.
$connection = new AMQPConnection([ put your credentials here ]);
$connection->connect();
if(!$connection->isConnected()) {
    throw new Exception('Connection failed.');
}

$chnlObj = new AMQPChannel($connection);
$queObj  = new AMQPQueue($chnlObj);
$queObj->setName('yourQueueName');
echo $queObj->get(AMQP_AUTOACK)->getBody();

// consider using connection more than once. that's only for illustration purposes.
$connection->disconnect();