Source for file Queue.php
Documentation is available at Queue.php
* Copyright (c) 2009 - 2011, RealDolmen
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of RealDolmen nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @package Microsoft_WindowsAzure
* @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
* @license http://todo name_todo
* @version $Id: Blob.php 24241 2009-07-22 09:43:13Z unknown $
* @see Microsoft_AutoLoader
require_once dirname(__FILE__ ) . '/../../AutoLoader.php';
* @package Microsoft_WindowsAzure
* @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
* @license http://phpazure.codeplex.com/license
* Maximal message size (in bytes)
* Maximal message ttl (in seconds)
* Creates a new Microsoft_WindowsAzure_Storage_Queue instance
* @param string $host Storage host name
* @param string $accountName Account name for Windows Azure
* @param string $accountKey Account key for Windows Azure
* @param boolean $usePathStyleUri Use path-style URI's
* @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
public function __construct($host = Microsoft_WindowsAzure_Storage::URL_DEV_QUEUE, $accountName = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT, $accountKey = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY, $usePathStyleUri = false, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
parent::__construct($host, $accountName, $accountKey, $usePathStyleUri, $retryPolicy);
* Check if a queue exists
* @param string $queueName Queue name
if (!self::isValidQueueName($queueName)) {
throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
foreach ($queues as $queue) {
if ($queue->Name == $queueName) {
* @param string $queueName Queue name
* @param array $metadata Key/value pairs of meta data
* @return object Queue properties
* @throws Microsoft_WindowsAzure_Exception
public function createQueue($queueName = '', $metadata = array())
if (!self::isValidQueueName($queueName)) {
throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
// Create metadata headers
if ($response->isSuccessful()) {
* Create queue if it does not exist
* @param string $queueName Queue name
* @param array $metadata Key/value pairs of meta data
* @throws Microsoft_WindowsAzure_Exception
* @param string $queueName Queue name
* @return Microsoft_WindowsAzure_Storage_QueueInstance
* @throws Microsoft_WindowsAzure_Exception
public function getQueue($queueName = '')
if (!self::isValidQueueName($queueName)) {
throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
if ($response->isSuccessful()) {
$queue->ApproximateMessageCount = intval($response->getHeader('x-ms-approximate-message-count'));
* @param string $queueName Queue name
* @return array Key/value pairs of meta data
* @throws Microsoft_WindowsAzure_Exception
if (!self::isValidQueueName($queueName)) {
throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
return $this->getQueue($queueName)->Metadata;
* Calling the Set Queue Metadata operation overwrites all existing metadata that is associated with the queue. It's not possible to modify an individual name/value pair.
* @param string $queueName Queue name
* @param array $metadata Key/value pairs of meta data
* @throws Microsoft_WindowsAzure_Exception
if (!self::isValidQueueName($queueName)) {
throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
if (count($metadata) == 0) {
// Create metadata headers
if (!$response->isSuccessful()) {
* @param string $queueName Queue name
* @throws Microsoft_WindowsAzure_Exception
if (!self::isValidQueueName($queueName)) {
throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
if (!$response->isSuccessful()) {
* @param string $prefix Optional. Filters the results to return only queues whose name begins with the specified prefix.
* @param int $maxResults Optional. Specifies the maximum number of queues to return per call to Azure storage. This does NOT affect list size returned by this function. (maximum: 5000)
* @param string $marker Optional string value that identifies the portion of the list to be returned with the next list operation.
* @param string $include Optional. Include this parameter to specify that the queue's metadata be returned as part of the response body. (allowed values: '', 'metadata')
* @param int $currentResultCount Current result count (internal use)
* @throws Microsoft_WindowsAzure_Exception
public function listQueues($prefix = null, $maxResults = null, $marker = null, $include = null, $currentResultCount = 0)
$queryString = array('comp=list');
$queryString[] = 'prefix=' . $prefix;
$queryString[] = 'maxresults=' . $maxResults;
$queryString[] = 'marker=' . $marker;
$queryString[] = 'include=' . $include;
$queryString = self::createQueryStringFromArray($queryString);
if ($response->isSuccessful()) {
for ($i = 0; $i < count($xmlQueues); $i++ ) {
(string) $xmlQueues[$i]->Name,
$currentResultCount = $currentResultCount + count($queues);
if (!is_null($maxResults) && $currentResultCount < $maxResults) {
if (!is_null($xmlMarker) && $xmlMarker != '') {
$queues = array_merge($queues, $this->listQueues($prefix, $maxResults, $xmlMarker, $include, $currentResultCount));
if (!is_null($maxResults) && count($queues) > $maxResults) {
* @param string $queueName Queue name
* @param string $message Message
* @param int $ttl Message Time-To-Live (in seconds). Defaults to 7 days if the parameter is omitted.
* @throws Microsoft_WindowsAzure_Exception
public function putMessage($queueName = '', $message = '', $ttl = null)
if (!self::isValidQueueName($queueName)) {
throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
if (strlen($message) > self::MAX_MESSAGE_SIZE) {
if (!is_null($ttl) && ($ttl <= 0 || $ttl > self::MAX_MESSAGE_SIZE)) {
throw new Microsoft_WindowsAzure_Exception('Message TTL is invalid. Maximal TTL is 7 days (' . self::MAX_MESSAGE_SIZE . ' seconds) and should be greater than zero.');
$queryString[] = 'messagettl=' . $ttl;
$queryString = self::createQueryStringFromArray($queryString);
$rawData .= '<QueueMessage>';
$rawData .= ' <MessageText>' . base64_encode($message) . '</MessageText>';
$rawData .= '</QueueMessage>';
if (!$response->isSuccessful()) {
* @param string $queueName Queue name
* @param string $numOfMessages Optional. A nonzero integer value that specifies the number of messages to retrieve from the queue, up to a maximum of 32. By default, a single message is retrieved from the queue with this operation.
* @param int $visibilityTimeout Optional. An integer value that specifies the message's visibility timeout in seconds. The maximum value is 2 hours. The default message visibility timeout is 30 seconds.
* @param string $peek Peek only?
* @throws Microsoft_WindowsAzure_Exception
public function getMessages($queueName = '', $numOfMessages = 1, $visibilityTimeout = null, $peek = false)
if (!self::isValidQueueName($queueName)) {
throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
if ($numOfMessages < 1 || $numOfMessages > 32 || intval($numOfMessages) != $numOfMessages) {
if (!is_null($visibilityTimeout) && ($visibilityTimeout <= 0 || $visibilityTimeout > 7200)) {
$queryString[] = 'peekonly=true';
if ($numOfMessages > 1) {
$queryString[] = 'numofmessages=' . $numOfMessages;
if (!$peek && !is_null($visibilityTimeout)) {
$queryString[] = 'visibilitytimeout=' . $visibilityTimeout;
$queryString = self::createQueryStringFromArray($queryString);
if ($response->isSuccessful()) {
if (count($result->QueueMessage) > 1) {
$xmlMessages = $result->QueueMessage;
$xmlMessages = array($result->QueueMessage);
for ($i = 0; $i < count($xmlMessages); $i++ ) {
(string) $xmlMessages[$i]->MessageId,
(string) $xmlMessages[$i]->InsertionTime,
(string) $xmlMessages[$i]->ExpirationTime,
($peek ? '' : (string) $xmlMessages[$i]->PopReceipt),
($peek ? '' : (string) $xmlMessages[$i]->TimeNextVisible),
(string) $xmlMessages[$i]->DequeueCount,
* @param string $queueName Queue name
* @param string $numOfMessages Optional. A nonzero integer value that specifies the number of messages to retrieve from the queue, up to a maximum of 32. By default, a single message is retrieved from the queue with this operation.
* @throws Microsoft_WindowsAzure_Exception
public function peekMessages($queueName = '', $numOfMessages = 1)
return $this->getMessages($queueName, $numOfMessages, null, true);
* Checks to see if a given queue has messages
* @param string $queueName Queue name
* @throws Microsoft_WindowsAzure_Exception
* @param string $queueName Queue name
* @throws Microsoft_WindowsAzure_Exception
if (!self::isValidQueueName($queueName)) {
throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
if (!$response->isSuccessful()) {
* @param string $queueName Queue name
* @param Microsoft_WindowsAzure_Storage_QueueMessage $message Message to delete from queue. A message retrieved using "peekMessages" can NOT be deleted!
* @throws Microsoft_WindowsAzure_Exception
public function deleteMessage($queueName = '', Microsoft_WindowsAzure_Storage_QueueMessage $message)
if (!self::isValidQueueName($queueName)) {
throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
if ($message->PopReceipt == '') {
if (!$response->isSuccessful()) {
* @param string $queueName Queue name
if (preg_match("/^[a-z0-9][a-z0-9-]*$/", $queueName) === 0) {
if (strpos($queueName, '--') !== false) {
if (substr($queueName, - 1) == '-') {
* Get error message from Microsoft_Http_Response
* @param Microsoft_Http_Response $response Repsonse
* @param string $alternativeError Alternative error message
protected function _getErrorMessage(Microsoft_Http_Response $response, $alternativeError = 'Unknown error.')
if ($response && $response->Message) {
return (string) $response->Message;
return $alternativeError;
|