Search code examples
phpazureazure-storageazure-storage-queues

Check if Azure Queue exists using REST proxy (PHP)


I'm trying to interact with an Azure Queue using a REST proxy courtesy of the Windows Azure SDK for PHP. Whilst there are plenty of code samples here, I want to check whether a queue exists so that I can create it if necessary, before adding a message to it.

try {
  // setup connection string for accessing queue storage
  $connectionString = 'DefaultEndpointsProtocol=' . PROTOCOL . ';AccountName=' . ACCOUNT_NAME . ';AccountKey=' . ACCOUNT_KEY;

  // create queue REST proxy
  $queueRestProxy = ServicesBuilder::getInstance()->createQueueService($connectionString);

  // create message
  $queueRestProxy->createMessage(QUEUE_NAME, 'Hello World!');

} catch(ServiceException $e){
  // Handle exception based on error codes and messages.
  // Error codes and messages are here: 
  // http://msdn.microsoft.com/en-us/library/windowsazure/dd179446.aspx
  $code = $e->getCode();
  $error_message = $e->getMessage();
  echo $code.": ".$error_message."<br />";
}

Creating a queue is as simple as this...

$queueRestProxy->createQueue(QUEUE_NAME);

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?


Solution

  • 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...

    1. Include the createQueue statement prior to creating a message, but wrap this statement in a try-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, the createQueue 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 associated QueueAlreadyExists message.

      Source: Create Queue (REST API) - see Remarks section

    2. 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!