web/lib/Zend/Cloud/QueueService/Adapter/WindowsAzure.php
changeset 886 1e110b03ae96
parent 807 877f952ae2bd
child 1230 68c69c656a2c
equal deleted inserted replaced
885:2251fb41dbc7 886:1e110b03ae96
    11  * to license@zend.com so we can send you a copy immediately.
    11  * to license@zend.com so we can send you a copy immediately.
    12  *
    12  *
    13  * @category   Zend
    13  * @category   Zend
    14  * @package    Zend_Cloud
    14  * @package    Zend_Cloud
    15  * @subpackage QueueService
    15  * @subpackage QueueService
    16  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    16  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    17  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  */
    18  */
    19 
    19 
    20 require_once 'Zend/Cloud/QueueService/Adapter/AbstractAdapter.php';
    20 require_once 'Zend/Cloud/QueueService/Adapter/AbstractAdapter.php';
    21 require_once 'Zend/Cloud/QueueService/Exception.php';
    21 require_once 'Zend/Cloud/QueueService/Exception.php';
    25  * WindowsAzure adapter for simple queue service.
    25  * WindowsAzure adapter for simple queue service.
    26  *
    26  *
    27  * @category   Zend
    27  * @category   Zend
    28  * @package    Zend_Cloud
    28  * @package    Zend_Cloud
    29  * @subpackage QueueService
    29  * @subpackage QueueService
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    30  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    32  */
    32  */
    33 class Zend_Cloud_QueueService_Adapter_WindowsAzure 
    33 class Zend_Cloud_QueueService_Adapter_WindowsAzure
    34     extends Zend_Cloud_QueueService_Adapter_AbstractAdapter
    34     extends Zend_Cloud_QueueService_Adapter_AbstractAdapter
    35 {
    35 {
    36     /**
    36     /**
    37      * Option array keys for the Windows Azure adapter.
    37      * Option array keys for the Windows Azure adapter.
    38      */
    38      */
    47     const LIST_PREFIX      = 'prefix';
    47     const LIST_PREFIX      = 'prefix';
    48     const LIST_MAX_RESULTS = 'max_results';
    48     const LIST_MAX_RESULTS = 'max_results';
    49 
    49 
    50     /** message options */
    50     /** message options */
    51     const MESSAGE_TTL = 'ttl';
    51     const MESSAGE_TTL = 'ttl';
    52     
    52 
    53     const DEFAULT_HOST = Zend_Service_WindowsAzure_Storage::URL_CLOUD_QUEUE;
    53     const DEFAULT_HOST = Zend_Service_WindowsAzure_Storage::URL_CLOUD_QUEUE;
    54 
    54 
    55     /**
    55     /**
    56      * Storage client
    56      * Storage client
    57      * 
    57      *
    58      * @var Zend_Service_WindowsAzure_Storage_Queue
    58      * @var Zend_Service_WindowsAzure_Storage_Queue
    59      */
    59      */
    60     protected $_storageClient = null;
    60     protected $_storageClient = null;
    61     
    61 
    62     /**
    62     /**
    63      * Constructor
    63      * Constructor
    64      * 
    64      *
    65      * @param  array|Zend_Config $options 
    65      * @param  array|Zend_Config $options
    66      * @return void
    66      * @return void
    67      */
    67      */
    68     public function __construct($options = array())
    68     public function __construct($options = array())
    69     {
    69     {
    70         if ($options instanceof Zend_Config) {
    70         if ($options instanceof Zend_Config) {
   110                 $this->_storageClient->setHttpClientChannel($httpAdapter);
   110                 $this->_storageClient->setHttpClientChannel($httpAdapter);
   111             }
   111             }
   112         } catch(Zend_Service_WindowsAzure_Exception $e) {
   112         } catch(Zend_Service_WindowsAzure_Exception $e) {
   113             throw new Zend_Cloud_QueueService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
   113             throw new Zend_Cloud_QueueService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
   114         }
   114         }
   115             
   115 
   116     }
   116     }
   117     
   117 
   118     /**
   118     /**
   119      * Create a queue. Returns the ID of the created queue (typically the URL).
   119      * Create a queue. Returns the ID of the created queue (typically the URL).
   120      * It may take some time to create the queue. Check your vendor's
   120      * It may take some time to create the queue. Check your vendor's
   121      * documentation for details.
   121      * documentation for details.
   122      *
   122      *
   131             return $queue->Name;
   131             return $queue->Name;
   132         } catch (Zend_Service_WindowsAzure_Exception $e) {
   132         } catch (Zend_Service_WindowsAzure_Exception $e) {
   133             throw new Zend_Cloud_QueueService_Exception('Error on queue creation: '.$e->getMessage(), $e->getCode(), $e);
   133             throw new Zend_Cloud_QueueService_Exception('Error on queue creation: '.$e->getMessage(), $e->getCode(), $e);
   134         }
   134         }
   135     }
   135     }
   136     
   136 
   137     /**
   137     /**
   138      * Delete a queue. All messages in the queue will also be deleted.
   138      * Delete a queue. All messages in the queue will also be deleted.
   139      *
   139      *
   140      * @param  string $queueId
   140      * @param  string $queueId
   141      * @param  array  $options
   141      * @param  array  $options
   150             return $this->_storageClient->deleteQueue($queueId);
   150             return $this->_storageClient->deleteQueue($queueId);
   151         } catch (Zend_Service_WindowsAzure_Exception $e) {
   151         } catch (Zend_Service_WindowsAzure_Exception $e) {
   152             throw new Zend_Cloud_QueueService_Exception('Error on queue deletion: '.$e->getMessage(), $e->getCode(), $e);
   152             throw new Zend_Cloud_QueueService_Exception('Error on queue deletion: '.$e->getMessage(), $e->getCode(), $e);
   153         }
   153         }
   154     }
   154     }
   155     
   155 
   156     /**
   156     /**
   157      * List all queues.
   157      * List all queues.
   158      *
   158      *
   159      * @param  array $options
   159      * @param  array $options
   160      * @return array Queue IDs
   160      * @return array Queue IDs
   175             return $result;
   175             return $result;
   176         } catch (Zend_Service_WindowsAzure_Exception $e) {
   176         } catch (Zend_Service_WindowsAzure_Exception $e) {
   177             throw new Zend_Cloud_QueueService_Exception('Error on listing queues: '.$e->getMessage(), $e->getCode(), $e);
   177             throw new Zend_Cloud_QueueService_Exception('Error on listing queues: '.$e->getMessage(), $e->getCode(), $e);
   178         }
   178         }
   179     }
   179     }
   180     
   180 
   181     /**
   181     /**
   182      * Get a key/value array of metadata for the given queue.
   182      * Get a key/value array of metadata for the given queue.
   183      *
   183      *
   184      * @param  string $queueId
   184      * @param  string $queueId
   185      * @param  array  $options
   185      * @param  array  $options
   194             return $this->_storageClient->getQueueMetadata($queueId);
   194             return $this->_storageClient->getQueueMetadata($queueId);
   195         } catch (Zend_Service_WindowsAzure_Exception $e) {
   195         } catch (Zend_Service_WindowsAzure_Exception $e) {
   196             throw new Zend_Cloud_QueueService_Exception('Error on fetching queue metadata: '.$e->getMessage(), $e->getCode(), $e);
   196             throw new Zend_Cloud_QueueService_Exception('Error on fetching queue metadata: '.$e->getMessage(), $e->getCode(), $e);
   197         }
   197         }
   198     }
   198     }
   199     
   199 
   200     /**
   200     /**
   201      * Store a key/value array of metadata for the specified queue.
   201      * Store a key/value array of metadata for the specified queue.
   202      * WARNING: This operation overwrites any metadata that is located at 
   202      * WARNING: This operation overwrites any metadata that is located at
   203      * $destinationPath. Some adapters may not support this method.
   203      * $destinationPath. Some adapters may not support this method.
   204      * 
   204      *
   205      * @param  string $queueId
   205      * @param  string $queueId
   206      * @param  array  $metadata
   206      * @param  array  $metadata
   207      * @param  array  $options
   207      * @param  array  $options
   208      * @return void
   208      * @return void
   209      */
   209      */
   216             return $this->_storageClient->setQueueMetadata($queueId, $metadata);
   216             return $this->_storageClient->setQueueMetadata($queueId, $metadata);
   217         } catch (Zend_Service_WindowsAzure_Exception $e) {
   217         } catch (Zend_Service_WindowsAzure_Exception $e) {
   218             throw new Zend_Cloud_QueueService_Exception('Error on setting queue metadata: '.$e->getMessage(), $e->getCode(), $e);
   218             throw new Zend_Cloud_QueueService_Exception('Error on setting queue metadata: '.$e->getMessage(), $e->getCode(), $e);
   219         }
   219         }
   220     }
   220     }
   221     
   221 
   222     /**
   222     /**
   223      * Send a message to the specified queue.
   223      * Send a message to the specified queue.
   224      * 
   224      *
   225      * @param  string $queueId
   225      * @param  string $queueId
   226      * @param  string $message
   226      * @param  string $message
   227      * @param  array  $options
   227      * @param  array  $options
   228      * @return string Message ID
   228      * @return string Message ID
   229      */
   229      */
   238             );
   238             );
   239         } catch (Zend_Service_WindowsAzure_Exception $e) {
   239         } catch (Zend_Service_WindowsAzure_Exception $e) {
   240             throw new Zend_Cloud_QueueService_Exception('Error on sending message: '.$e->getMessage(), $e->getCode(), $e);
   240             throw new Zend_Cloud_QueueService_Exception('Error on sending message: '.$e->getMessage(), $e->getCode(), $e);
   241         }
   241         }
   242     }
   242     }
   243     
   243 
   244     /**
   244     /**
   245      * Recieve at most $max messages from the specified queue and return the
   245      * Recieve at most $max messages from the specified queue and return the
   246      * message IDs for messages recieved.
   246      * message IDs for messages recieved.
   247      * 
   247      *
   248      * @param  string $queueId
   248      * @param  string $queueId
   249      * @param  int    $max
   249      * @param  int    $max
   250      * @param  array  $options
   250      * @param  array  $options
   251      * @return Zend_Cloud_QueueService_Message[]
   251      * @return Zend_Cloud_QueueService_Message[]
   252      */
   252      */
   264             return $this->_makeMessages($this->_storageClient->getMessages($queueId, $max, $visibility, false));
   264             return $this->_makeMessages($this->_storageClient->getMessages($queueId, $max, $visibility, false));
   265         } catch (Zend_Service_WindowsAzure_Exception $e) {
   265         } catch (Zend_Service_WindowsAzure_Exception $e) {
   266             throw new Zend_Cloud_QueueService_Exception('Error on recieving messages: '.$e->getMessage(), $e->getCode(), $e);
   266             throw new Zend_Cloud_QueueService_Exception('Error on recieving messages: '.$e->getMessage(), $e->getCode(), $e);
   267         }
   267         }
   268     }
   268     }
   269     
   269 
   270     /**
   270     /**
   271      * Create Zend_Cloud_QueueService_Message array for
   271      * Create Zend_Cloud_QueueService_Message array for
   272      * Azure messages.
   272      * Azure messages.
   273      *  
   273      *
   274      * @param array $messages
   274      * @param array $messages
   275      * @return Zend_Cloud_QueueService_Message[]
   275      * @return Zend_Cloud_QueueService_Message[]
   276      */
   276      */
   277     protected function _makeMessages($messages)
   277     protected function _makeMessages($messages)
   278     {
   278     {
   285         return new $setClass($result);
   285         return new $setClass($result);
   286     }
   286     }
   287 
   287 
   288     /**
   288     /**
   289      * Delete the specified message from the specified queue.
   289      * Delete the specified message from the specified queue.
   290      * 
   290      *
   291      * @param  string $queueId
   291      * @param  string $queueId
   292      * @param  Zend_Cloud_QueueService_Message $message Message ID or message 
   292      * @param  Zend_Cloud_QueueService_Message $message Message ID or message
   293      * @param  array  $options
   293      * @param  array  $options
   294      * @return void
   294      * @return void
   295      */
   295      */
   296     public function deleteMessage($queueId, $message, $options = null)
   296     public function deleteMessage($queueId, $message, $options = null)
   297     {
   297     {
   329             return $this->_makeMessages($this->_storageClient->peekMessages($queueId, $num));
   329             return $this->_makeMessages($this->_storageClient->peekMessages($queueId, $num));
   330         } catch (Zend_Service_WindowsAzure_Exception $e) {
   330         } catch (Zend_Service_WindowsAzure_Exception $e) {
   331             throw new Zend_Cloud_QueueService_Exception('Error on peeking messages: '.$e->getMessage(), $e->getCode(), $e);
   331             throw new Zend_Cloud_QueueService_Exception('Error on peeking messages: '.$e->getMessage(), $e->getCode(), $e);
   332         }
   332         }
   333    }
   333    }
   334     
   334 
   335     /**
   335     /**
   336      * Get Azure implementation
   336      * Get Azure implementation
   337      * @return Zend_Service_Azure_Storage_Queue 
   337      * @return Zend_Service_Azure_Storage_Queue
   338      */
   338      */
   339     public function getClient()
   339     public function getClient()
   340     {
   340     {
   341         return $this->_storageClient;
   341         return $this->_storageClient;
   342     }
   342     }