I've posted an answer below for completeness and to make it easy for people to see the answer at a glance.
Should I simply include the queue creation code prior to creating a message or is there a more efficient way to ascertain whether the queue exists before interacting with it?
There are two ways to approach this...
Include the
createQueue
statement prior to creating a message, but wrap this statement in atry-catch
block as directed by Guarav Mantri's answer i.e. ignore 409 errors, but throw an exception for any other types of error.For information, when you include a
createQueue
statement prior to creating a message...if a queue of the same name already exists and the metadata associated with the existing queue is the same as that passed to the
createQueue
statement then the queue will not be created and the Queue REST Proxy will internally receive a 204 (No Content) status code, but this response code is not made available to the programmer. So, essentially, thecreateQueue
statement doesn't cause an error/exception to be raised in this scenario.if a queue of the same name already exists and the metadata associated with the existing queue isn't the same as that passed to the
createQueue
statement then the queue will not be created and the Queue REST Proxy will receive a 409 (Conflict) status code and will raise an exception which allows the programmer to access this response code and the associatedQueueAlreadyExists
message.
Source: Create Queue (REST API) - see Remarks section
Create a
queueExists
function and call it to decide whether or not queue creation is necessary. Here is one way to implement such a function...public function queueExists($queueRestProxy, $queueName) { $result = FALSE; $listQueuesResult = $queueRestProxy->listQueues(); $queues = $listQueuesResult->getQueues(); foreach($queues as $queue) { if ($queue->getName() === $queueName) { $result = TRUE; break; } } return $result; }
Hope this helps someone!