2012-07-15 82 views
1

我在pecl 1.0.3中使用amqp擴展,使用2.7.1 rabbitmq編譯。PHP AMQP消費者:服務器通道錯誤:404,消息:NOT_FOUND

我試圖得到一個基本的生產者/消費者例子的工作,但我不斷收到錯誤。在這個擴展中有很少的php文檔,並且很多它似乎過時或者錯誤。

我使用的用戶發佈的代碼,但似乎並沒有得到消費者的部分工作

連接:

function amqp_connection() { 
    $amqpConnection = new AMQPConnection(); 
    $amqpConnection->setLogin("guest"); 
    $amqpConnection->setPassword("guest"); 
    $amqpConnection->connect(); 

    if(!$amqpConnection->isConnected()) { 
     die("Cannot connect to the broker, exiting !\n"); 
    } 

    return $amqpConnection; 
} 

發件人:

function amqp_send($text, $routingKey, $exchangeName){ 
    $amqpConnection = amqp_connection(); 

    $channel = new AMQPChannel($amqpConnection); 
    $exchange = new AMQPExchange($channel); 

    $exchange->setName($exchangeName); 
    $exchange->setType("fanout"); 


    if($message = $exchange->publish($text, $routingKey)){ 
     echo "sent"; 
    } 

    if (!$amqpConnection->disconnect()) { 
     throw new Exception("Could not disconnect !"); 
    } 
} 

接收機:

function amqp_receive($exchangeName, $routingKey, $queueName) { 
    $amqpConnection = amqp_connection(); 

    $channel = new AMQPChannel($amqpConnection); 
    $queue = new AMQPQueue($channel); 
    $queue->setName($queueName); 
    $queue->bind($exchangeName, $routingKey); 

    //Grab the info 
    //... 
} 

Th EN發送它:

amqp_send("Abcdefg", "action", "amq.fanout"); 

和接受它:

amqp_receive("amq.fanout","action","action"); 

我不斷收到一個問題運行腳本,並指向AMQP接受:

PHP Fatal error: Uncaught exception 'AMQPQueueException' with message 'Server channel error: 404, message: NOT_FOUND - no queue 'action' in vhost '/'' in /home/jamescowhen/test.php:21

任何人都可以點我的正確的方向?整個樣本是從這裏用戶注意: http://www.php.net/manual/en/amqp.examples.php#109024

回答

3

唯一的例外似乎是由您的隊列沒有被宣佈引起(如錯誤消息說明404 - 未找到隊列「行動」)。這個例子之所以能夠用於原始海報,可能是因爲他已經在早些時候宣佈了隊列,卻沒有意識到他的例子中缺少了這個例子。

您可以通過在隊列對象上調用 - > declare()來聲明隊列。您還必須對交換對象執行此操作,除非您確定在嘗試將隊列掛接到該對象時它已經存在。

+0

你是對的,謝謝。我最初使用的是一個php庫,而不是本機擴展,默認情況下,如果該庫沒有被聲明,則該庫創建隊列。 – Jamescowhen 2012-07-20 03:48:29