Php get error message from rabbitmq

My amqp extension version is 1.0.1 and the AMQP protocol version is 0-9-1

receive messages from the queue:

<?php try { $conn = new AMQPConnection() ; $conn->setLogin('guest') ; $conn->setPassword('guest') ; $conn->connect() ; if ($conn->isConnected()) { $channel = new AMQPChannel($conn) ; if ($channel->isConnected()) { $queue = new AMQPQueue($channel) ; $queue->setName('test_queue') ; $queue->setFlags(AMQP_DURABLE | AMQP_AUTODELETE) ; $queue->declare() ; $messages = $queue->get(AMQP_AUTOACK) ; print_r($messages->getBody()) ; } } else { echo "connect failure ... " ; } $conn->disconnect() ;} catch (Exception $e) { echo $e->getMessage() ;}?> 

and it does not work.

 Server channel error: 406, message: PRECONDITION_FAILED - parameters for queue 'test_queue' in vhost '/' not equivalent 
+4
source share
2 answers

It seems to me that the queue already exists and was previously declared (created) with different parameters in vhost. Queues must be declared exactly with the same parameters each time (or deleted and recreated with the required parameters). Try removing the queue through the management plugin (http://www.rabbitmq.com/management.html), and then run the script again

+8
source

If your queue has already been created, you do not need to create it (using the declare method) and contact Exchange again. IMHO, you should not do this, because :) these actions require administrative privileges; b) enough for this only once; c) you may not have administrative rights to the production, and your code will be violated. I believe that it’s better to create and link all the required queues to the management console or any other tool that you like, and then receive messages this way.

 // consider using connection more than once. that only for illustration purposes. $connection = new AMQPConnection([ put your credentials here ]); $connection->connect(); if(!$connection->isConnected()) { throw new Exception('Connection failed.'); } $chnlObj = new AMQPChannel($connection); $queObj = new AMQPQueue($chnlObj); $queObj->setName('yourQueueName'); echo $queObj->get(AMQP_AUTOACK)->getBody(); // consider using connection more than once. that only for illustration purposes. $connection->disconnect(); 
+1
source

Source: https://habr.com/ru/post/1401534/


All Articles