0

pika ライブラリを使用して rabbitMQ キューにメッセージをプッシュする 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 (関数レシーバーを確認してください

$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
4

2 に答える 2

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 は、配列ではなくプロパティを介して構成する必要があることに注意してください。AMQPQueue オブジェクトに渡すには AMQPChannel を使用する必要があります。デフォルト交換のキューに対するバインドは機能しません/不要です。効果を確認するには、$queue->bind を示す行のコメントを外してみてください:-)

両方のスクリプトのコピーを公開 Gist として Github に配置しました - https://gist.github.com/2988379

于 2012-06-25T11:59:04.460 に答える
0

文書が間違っていると思います。キューを作成するには、AMQPConnection ではなく、AMQPChannel が必要です。キューのコンストラクターの定義を見つけることができます。

AMQPQueue::__construct(AMQPChannel channel)

AMQP パッケージのソース コード: amqp_queue.c

于 2012-06-28T10:14:25.757 に答える