2012-06-22 45 views
0

我有一个python应用程序使用pika库将消息推入rabbitMQ队列。RabbitMQ - PHP/Python的消费者问题

message='hola' 
credentials = pika.PlainCredentials('guest', 'guest') 
connection = pika.BlockingConnection(pika.ConnectionParameters(credentials=credentials, host='localhost')) 
channel = connection.channel() 
channel.queue_declare(queue='myqueue') 
channel.basic_publish(exchange='',routing_key='myqueue',body=message) 
connection.close() 

这是行得通的。

我需要在php应用程序中使用这些消息。我想这是在这个页面中AQMP例子中提到 - http://www.php.net/manual/en/class.amqpqueue.php(检查功能reciever)

$cnn = new AMQPConnection((array("host"=>"ec2-xxx-xx-xx-xxx.ap-southeast-1.compute.amazonaws.com","login"=>"guest", "password"=>"guest"))); 
if ($cnn->connect()) { 
    echo "Established a connection to the broker"; 
} 
else { 
    echo "Cannot connect to the broker"; 
} 

    $queue = new AMQPQueue($cnn); 
    $queue->declare('myqueue'); 
    $queue->bind('', 'myqueue'); 

$msg=$queue->get(AMQP_AUTOACK); 
echo $msg->getBody(); 

抛出该异常 -

Fatal error: Uncaught exception 'Exception' with message 'Error parsing parameters.' in /home/webroot/amqptest.php:12 Stack trace: #0 /home/webroot/amqptest.php(12): AMQPQueue->declare('myqueue') #1 {main} thrown in /home/webroot/amqptest.php on line 12 
+0

什么时候出现错误?当你连接的时候吗?或者在你正在消费的部分? –

回答

1

这应该工作 - 但是记住有没有错误处理或任何循环来重复拾取消息。尽管如此,它确实成功出列/接收来自代理的单个消息(并且如果再次运行空队列则会崩溃)。

<?php 

$cnn = new AMQPConnection(); 
$cnn->setLogin("guest"); 
$cnn->setPassword("guest"); 
$cnn->setHost("localhost"); 

if ($cnn->connect()) { 
    echo "Established a connection to the broker\n"; 
} 
else { 
    echo "Cannot connect to the broker\n"; 
} 

$channel = new AMQPChannel($cnn); 
$queue = new AMQPQueue($channel); 
$queue->setName('myqueue'); 

// the default/nameless) exchange does not require a binding 
// as the broker declares a binding for each queue with key 
// identical to the queue name. error 403 if you try yourself. 
//$queue->bind('', 'myqueue'); 

$msg=$queue->get(AMQP_AUTOACK); 

echo $msg->getBody(); 
echo "\n"; 

?> 

具体来说,请注意AMQPConnection现在需要通过属性而不是数组进行配置;您必须使用AMQPChannel传递给AMQPQueue对象;并且对默认交换中的队列进行绑定将不起作用/是不必要的。要看到效果尝试取消注释显示queue- $>绑定行:-)

。我把剧本的拷贝最多在Github作为公共吉斯特 - https://gist.github.com/2988379

0

我认为该文件是错误的。您需要一个AMQPChannel来创建队列,而不是AMQPConnection。您可以找到队列的构造函数的定义:在AMQP包的源代码

AMQPQueue::__construct(AMQPChannel channel) 

amqp_queue.c