2012-06-22 122 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