web/lib/Zend/Cloud/QueueService/Adapter/Sqs.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    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/Service/Amazon/Sqs.php';
    20 require_once 'Zend/Service/Amazon/Sqs.php';
    21 require_once 'Zend/Cloud/QueueService/Adapter/AbstractAdapter.php';
    21 require_once 'Zend/Cloud/QueueService/Adapter/AbstractAdapter.php';
    26  * SQS adapter for simple queue service.
    26  * SQS adapter for simple queue service.
    27  *
    27  *
    28  * @category   Zend
    28  * @category   Zend
    29  * @package    Zend_Cloud
    29  * @package    Zend_Cloud
    30  * @subpackage QueueService
    30  * @subpackage QueueService
    31  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    31  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    33  */
    33  */
    34 class Zend_Cloud_QueueService_Adapter_Sqs 
    34 class Zend_Cloud_QueueService_Adapter_Sqs
    35     extends Zend_Cloud_QueueService_Adapter_AbstractAdapter
    35     extends Zend_Cloud_QueueService_Adapter_AbstractAdapter
    36 {
    36 {
    37     /*
    37     /*
    38      * Options array keys for the SQS adapter.
    38      * Options array keys for the SQS adapter.
    39      */
    39      */
    51      */
    51      */
    52     protected $_sqs;
    52     protected $_sqs;
    53 
    53 
    54     /**
    54     /**
    55      * Constructor
    55      * Constructor
    56      * 
    56      *
    57      * @param  array|Zend_Config $options 
    57      * @param  array|Zend_Config $options
    58      * @return void
    58      * @return void
    59      */
    59      */
    60     public function __construct($options = array()) 
    60     public function __construct($options = array())
    61     {
    61     {
    62         if ($options instanceof Zend_Config) {
    62         if ($options instanceof Zend_Config) {
    63             $options = $options->toArray();
    63             $options = $options->toArray();
    64         }
    64         }
    65 
    65 
    83             throw new Zend_Cloud_QueueService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
    83             throw new Zend_Cloud_QueueService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
    84         }
    84         }
    85 
    85 
    86         if(isset($options[self::HTTP_ADAPTER])) {
    86         if(isset($options[self::HTTP_ADAPTER])) {
    87             $this->_sqs->getHttpClient()->setAdapter($options[self::HTTP_ADAPTER]);
    87             $this->_sqs->getHttpClient()->setAdapter($options[self::HTTP_ADAPTER]);
    88         } 
    88         }
    89     }
    89     }
    90 
    90 
    91      /**
    91      /**
    92      * Create a queue. Returns the ID of the created queue (typically the URL).
    92      * Create a queue. Returns the ID of the created queue (typically the URL).
    93      * It may take some time to create the queue. Check your vendor's
    93      * It may take some time to create the queue. Check your vendor's
    95      *
    95      *
    96      * @param  string $name
    96      * @param  string $name
    97      * @param  array  $options
    97      * @param  array  $options
    98      * @return string Queue ID (typically URL)
    98      * @return string Queue ID (typically URL)
    99      */
    99      */
   100     public function createQueue($name, $options = null) 
   100     public function createQueue($name, $options = null)
   101     {
   101     {
   102         try {
   102         try {
   103             return $this->_sqs->create($name, $options[self::CREATE_TIMEOUT]);
   103             return $this->_sqs->create($name, $options[self::CREATE_TIMEOUT]);
   104         } catch(Zend_Service_Amazon_Exception $e) {
   104         } catch(Zend_Service_Amazon_Exception $e) {
   105             throw new Zend_Cloud_QueueService_Exception('Error on queue creation: '.$e->getMessage(), $e->getCode(), $e);
   105             throw new Zend_Cloud_QueueService_Exception('Error on queue creation: '.$e->getMessage(), $e->getCode(), $e);
   111      *
   111      *
   112      * @param  string $queueId
   112      * @param  string $queueId
   113      * @param  array  $options
   113      * @param  array  $options
   114      * @return boolean true if successful, false otherwise
   114      * @return boolean true if successful, false otherwise
   115      */
   115      */
   116     public function deleteQueue($queueId, $options = null) 
   116     public function deleteQueue($queueId, $options = null)
   117 {
   117 {
   118         try {
   118         try {
   119             return $this->_sqs->delete($queueId);
   119             return $this->_sqs->delete($queueId);
   120         } catch(Zend_Service_Amazon_Exception $e) {
   120         } catch(Zend_Service_Amazon_Exception $e) {
   121             throw Zend_Cloud_QueueService_Exception('Error on queue deletion: '.$e->getMessage(), $e->getCode(), $e);
   121             throw Zend_Cloud_QueueService_Exception('Error on queue deletion: '.$e->getMessage(), $e->getCode(), $e);
   126      * List all queues.
   126      * List all queues.
   127      *
   127      *
   128      * @param  array $options
   128      * @param  array $options
   129      * @return array Queue IDs
   129      * @return array Queue IDs
   130      */
   130      */
   131     public function listQueues($options = null) 
   131     public function listQueues($options = null)
   132     {
   132     {
   133         try {
   133         try {
   134             return $this->_sqs->getQueues();
   134             return $this->_sqs->getQueues();
   135         } catch(Zend_Service_Amazon_Exception $e) {
   135         } catch(Zend_Service_Amazon_Exception $e) {
   136             throw new Zend_Cloud_QueueService_Exception('Error on listing queues: '.$e->getMessage(), $e->getCode(), $e);
   136             throw new Zend_Cloud_QueueService_Exception('Error on listing queues: '.$e->getMessage(), $e->getCode(), $e);
   142      *
   142      *
   143      * @param  string $queueId
   143      * @param  string $queueId
   144      * @param  array  $options
   144      * @param  array  $options
   145      * @return array
   145      * @return array
   146      */
   146      */
   147     public function fetchQueueMetadata($queueId, $options = null) 
   147     public function fetchQueueMetadata($queueId, $options = null)
   148     {
   148     {
   149         try {
   149         try {
   150             // TODO: ZF-9050 Fix the SQS client library in trunk to return all attribute values
   150             // TODO: ZF-9050 Fix the SQS client library in trunk to return all attribute values
   151             $attributes = $this->_sqs->getAttribute($queueId, 'All');
   151             $attributes = $this->_sqs->getAttribute($queueId, 'All');
   152             if(is_array($attributes)) {
   152             if(is_array($attributes)) {
   167      * @param  array  $metadata
   167      * @param  array  $metadata
   168      * @param  string $queueId
   168      * @param  string $queueId
   169      * @param  array  $options
   169      * @param  array  $options
   170      * @return void
   170      * @return void
   171      */
   171      */
   172     public function storeQueueMetadata($queueId, $metadata, $options = null) 
   172     public function storeQueueMetadata($queueId, $metadata, $options = null)
   173     {
   173     {
   174         // TODO Add support for SetQueueAttributes to client library
   174         // TODO Add support for SetQueueAttributes to client library
   175         require_once 'Zend/Cloud/OperationNotAvailableException.php';
   175         require_once 'Zend/Cloud/OperationNotAvailableException.php';
   176         throw new Zend_Cloud_OperationNotAvailableException('Amazon SQS doesn\'t currently support storing metadata');
   176         throw new Zend_Cloud_OperationNotAvailableException('Amazon SQS doesn\'t currently support storing metadata');
   177     }
   177     }
   182      * @param  string $message
   182      * @param  string $message
   183      * @param  string $queueId
   183      * @param  string $queueId
   184      * @param  array  $options
   184      * @param  array  $options
   185      * @return string Message ID
   185      * @return string Message ID
   186      */
   186      */
   187     public function sendMessage($queueId, $message, $options = null) 
   187     public function sendMessage($queueId, $message, $options = null)
   188     {
   188     {
   189         try {
   189         try {
   190             return $this->_sqs->send($queueId, $message);
   190             return $this->_sqs->send($queueId, $message);
   191         } catch(Zend_Service_Amazon_Exception $e) {
   191         } catch(Zend_Service_Amazon_Exception $e) {
   192             throw new Zend_Cloud_QueueService_Exception('Error on sending message: '.$e->getMessage(), $e->getCode(), $e);
   192             throw new Zend_Cloud_QueueService_Exception('Error on sending message: '.$e->getMessage(), $e->getCode(), $e);
   200      * @param  string $queueId
   200      * @param  string $queueId
   201      * @param  int    $max
   201      * @param  int    $max
   202      * @param  array  $options
   202      * @param  array  $options
   203      * @return array
   203      * @return array
   204      */
   204      */
   205     public function receiveMessages($queueId, $max = 1, $options = null) 
   205     public function receiveMessages($queueId, $max = 1, $options = null)
   206     {
   206     {
   207         try {
   207         try {
   208             return $this->_makeMessages($this->_sqs->receive($queueId, $max, $options[self::VISIBILITY_TIMEOUT]));
   208             return $this->_makeMessages($this->_sqs->receive($queueId, $max, $options[self::VISIBILITY_TIMEOUT]));
   209         } catch(Zend_Service_Amazon_Exception $e) {
   209         } catch(Zend_Service_Amazon_Exception $e) {
   210             throw new Zend_Cloud_QueueService_Exception('Error on recieving messages: '.$e->getMessage(), $e->getCode(), $e);
   210             throw new Zend_Cloud_QueueService_Exception('Error on recieving messages: '.$e->getMessage(), $e->getCode(), $e);
   211         }
   211         }
   212     }
   212     }
   213     
   213 
   214     /**
   214     /**
   215      * Create Zend_Cloud_QueueService_Message array for
   215      * Create Zend_Cloud_QueueService_Message array for
   216      * Sqs messages.
   216      * Sqs messages.
   217      *  
   217      *
   218      * @param array $messages
   218      * @param array $messages
   219      * @return Zend_Cloud_QueueService_Message[]
   219      * @return Zend_Cloud_QueueService_Message[]
   220      */
   220      */
   221     protected function _makeMessages($messages)
   221     protected function _makeMessages($messages)
   222     {
   222     {
   235      * @param  string $queueId
   235      * @param  string $queueId
   236      * @param  Zend_Cloud_QueueService_Message $message
   236      * @param  Zend_Cloud_QueueService_Message $message
   237      * @param  array  $options
   237      * @param  array  $options
   238      * @return void
   238      * @return void
   239      */
   239      */
   240     public function deleteMessage($queueId, $message, $options = null) 
   240     public function deleteMessage($queueId, $message, $options = null)
   241     {
   241     {
   242         try {
   242         try {
   243             if($message instanceof Zend_Cloud_QueueService_Message) {
   243             if($message instanceof Zend_Cloud_QueueService_Message) {
   244                 $message = $message->getMessage();
   244                 $message = $message->getMessage();
   245             }
   245             }
   247             return $this->_sqs->deleteMessage($queueId, $messageId);
   247             return $this->_sqs->deleteMessage($queueId, $messageId);
   248         } catch(Zend_Service_Amazon_Exception $e) {
   248         } catch(Zend_Service_Amazon_Exception $e) {
   249             throw new Zend_Cloud_QueueService_Exception('Error on deleting a message: '.$e->getMessage(), $e->getCode(), $e);
   249             throw new Zend_Cloud_QueueService_Exception('Error on deleting a message: '.$e->getMessage(), $e->getCode(), $e);
   250         }
   250         }
   251     }
   251     }
   252     
   252 
   253     /**
   253     /**
   254      * Peek at the messages from the specified queue without removing them.
   254      * Peek at the messages from the specified queue without removing them.
   255      *
   255      *
   256      * @param  string $queueId
   256      * @param  string $queueId
   257      * @param  int $num How many messages
   257      * @param  int $num How many messages
   267         }
   267         }
   268     }
   268     }
   269 
   269 
   270     /**
   270     /**
   271      * Get SQS implementation
   271      * Get SQS implementation
   272      * @return Zend_Service_Amazon_Sqs 
   272      * @return Zend_Service_Amazon_Sqs
   273      */
   273      */
   274     public function getClient()
   274     public function getClient()
   275     {
   275     {
   276         return $this->_sqs;
   276         return $this->_sqs;
   277     }
   277     }