Search code examples
phprabbitmqamqp

php rabbitmq how to check if the queue exists


I need to create a destructor for queue. So in any case if the system suddenly crash, destructor will be called. Destructor will close all the queue and connection ONLY IF the queue hasn't been close yet.

Is there anyway to check if queue is exists or something similar? So far I couldn't find it.

currently this is my code

public function __destruct(){
      # add if queue exists here
		$this->channels->close();
		$this->connection->close();
		echo 'Connection closed.';
	}


Solution

  • you can't "close" a queue in the same way you close a channel or connection.

    if you want the queue to be removed when the consumer disconnects from RabbitMQ, declare the queue with the $auto_delete parameter set to true.

    RabbitMQ will delete the queue for you, when there are no more consumers reading from that queue.

    Looks like this is the default, by the way, so your queue should be removed automatically if you don't override $auto_delete: https://github.com/videlalvaro/php-amqplib/blob/master/PhpAmqpLib/Channel/AMQPChannel.php#L339