简体   繁体   中英

RabbitMQ - PHP / Python consumer problems

I have a python application which pushes messages into a rabbitMQ queue using pika library.

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()

That works.

I need to consume these messages in a php application. I tried this as mentioned in a AQMP example at this page - http://www.php.net/manual/en/class.amqpqueue.php (check function 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();

Throws this exception -

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

This should work - but bear in mind there's no error handling or any loop to repeatedly pick up messages. It does successfully dequeue / receive individual messages from a broker for me, though (and crashes if run agains an empty queue).

<?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";

?>

Specifically, note that the AMQPConnection now needs to be configured via properties not an array; you must use an AMQPChannel to pass to the AMQPQueue object; and binding against a queue in the default exchange will not work / is unnecessary. To see the effect try uncommenting the line showing $queue->bind :-)

I put copies of both scripts up on Github as a public Gist - https://gist.github.com/2988379

I think the document is wrong. You need a AMQPChannel to create the queue, not a AMQPConnection. You can find the definition of the constructor of the queue:

AMQPQueue::__construct(AMQPChannel channel)

in the source code of the AMQP package: amqp_queue.c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM