first commit
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cloud
|
||||
* @subpackage QueueService
|
||||
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
require_once 'Zend/Cloud/QueueService/Adapter.php';
|
||||
require_once 'Zend/Cloud/QueueService/Message.php';
|
||||
require_once 'Zend/Cloud/QueueService/MessageSet.php';
|
||||
|
||||
/**
|
||||
* Abstract queue adapter
|
||||
*
|
||||
* Provides functionality around setting message and message set classes.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cloud
|
||||
* @subpackage QueueService
|
||||
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_Cloud_QueueService_Adapter_AbstractAdapter
|
||||
implements Zend_Cloud_QueueService_Adapter
|
||||
{
|
||||
/**@+ option keys */
|
||||
const MESSAGE_CLASS = 'message_class';
|
||||
const MESSAGESET_CLASS = 'messageset_class';
|
||||
/**@-*/
|
||||
|
||||
/** @var string Class to use for queue messages */
|
||||
protected $_messageClass = 'Zend_Cloud_QueueService_Message';
|
||||
|
||||
/** @var string Class to use for collections of queue messages */
|
||||
protected $_messageSetClass = 'Zend_Cloud_QueueService_MessageSet';
|
||||
|
||||
/**
|
||||
* Set class to use for message objects
|
||||
*
|
||||
* @param string $class
|
||||
* @return Zend_Cloud_QueueService_Adapter_AbstractAdapter
|
||||
*/
|
||||
public function setMessageClass($class)
|
||||
{
|
||||
$this->_messageClass = (string) $class;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get class to use for message objects
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMessageClass()
|
||||
{
|
||||
return $this->_messageClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set class to use for message collection objects
|
||||
*
|
||||
* @param string $class
|
||||
* @return Zend_Cloud_QueueService_Adapter_AbstractAdapter
|
||||
*/
|
||||
public function setMessageSetClass($class)
|
||||
{
|
||||
$this->_messageSetClass = (string) $class;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get class to use for message collection objects
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMessageSetClass()
|
||||
{
|
||||
return $this->_messageSetClass;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
<?php
|
||||
/**
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cloud
|
||||
* @subpackage QueueService
|
||||
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Amazon/Sqs.php';
|
||||
require_once 'Zend/Cloud/QueueService/Adapter/AbstractAdapter.php';
|
||||
require_once 'Zend/Cloud/QueueService/Exception.php';
|
||||
require_once 'Zend/Cloud/QueueService/Message.php';
|
||||
|
||||
/**
|
||||
* SQS adapter for simple queue service.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cloud
|
||||
* @subpackage QueueService
|
||||
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cloud_QueueService_Adapter_Sqs
|
||||
extends Zend_Cloud_QueueService_Adapter_AbstractAdapter
|
||||
{
|
||||
/*
|
||||
* Options array keys for the SQS adapter.
|
||||
*/
|
||||
const AWS_ACCESS_KEY = 'aws_accesskey';
|
||||
const AWS_SECRET_KEY = 'aws_secretkey';
|
||||
|
||||
/**
|
||||
* Defaults
|
||||
*/
|
||||
const CREATE_TIMEOUT = 30;
|
||||
|
||||
/**
|
||||
* SQS service instance.
|
||||
* @var Zend_Service_Amazon_Sqs
|
||||
*/
|
||||
protected $_sqs;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array|Zend_Config $options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($options = array())
|
||||
{
|
||||
if ($options instanceof Zend_Config) {
|
||||
$options = $options->toArray();
|
||||
}
|
||||
|
||||
if (!is_array($options)) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Invalid options provided');
|
||||
}
|
||||
|
||||
if (isset($options[self::MESSAGE_CLASS])) {
|
||||
$this->setMessageClass($options[self::MESSAGE_CLASS]);
|
||||
}
|
||||
|
||||
if (isset($options[self::MESSAGESET_CLASS])) {
|
||||
$this->setMessageSetClass($options[self::MESSAGESET_CLASS]);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->_sqs = new Zend_Service_Amazon_Sqs(
|
||||
$options[self::AWS_ACCESS_KEY], $options[self::AWS_SECRET_KEY]
|
||||
);
|
||||
} catch(Zend_Service_Amazon_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
if(isset($options[self::HTTP_ADAPTER])) {
|
||||
$this->_sqs->getHttpClient()->setAdapter($options[self::HTTP_ADAPTER]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a queue. Returns the ID of the created queue (typically the URL).
|
||||
* It may take some time to create the queue. Check your vendor's
|
||||
* documentation for details.
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $options
|
||||
* @return string Queue ID (typically URL)
|
||||
*/
|
||||
public function createQueue($name, $options = null)
|
||||
{
|
||||
try {
|
||||
return $this->_sqs->create($name, $options[self::CREATE_TIMEOUT]);
|
||||
} catch(Zend_Service_Amazon_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on queue creation: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a queue. All messages in the queue will also be deleted.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param array $options
|
||||
* @return boolean true if successful, false otherwise
|
||||
*/
|
||||
public function deleteQueue($queueId, $options = null)
|
||||
{
|
||||
try {
|
||||
return $this->_sqs->delete($queueId);
|
||||
} catch(Zend_Service_Amazon_Exception $e) {
|
||||
throw Zend_Cloud_QueueService_Exception('Error on queue deletion: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List all queues.
|
||||
*
|
||||
* @param array $options
|
||||
* @return array Queue IDs
|
||||
*/
|
||||
public function listQueues($options = null)
|
||||
{
|
||||
try {
|
||||
return $this->_sqs->getQueues();
|
||||
} catch(Zend_Service_Amazon_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on listing queues: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a key/value array of metadata for the given queue.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param array $options
|
||||
* @return array
|
||||
*/
|
||||
public function fetchQueueMetadata($queueId, $options = null)
|
||||
{
|
||||
try {
|
||||
// TODO: ZF-9050 Fix the SQS client library in trunk to return all attribute values
|
||||
$attributes = $this->_sqs->getAttribute($queueId, 'All');
|
||||
if(is_array($attributes)) {
|
||||
return $attributes;
|
||||
} else {
|
||||
return array('All' => $this->_sqs->getAttribute($queueId, 'All'));
|
||||
}
|
||||
} catch(Zend_Service_Amazon_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on fetching queue metadata: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a key/value array of metadata for the specified queue.
|
||||
* WARNING: This operation overwrites any metadata that is located at
|
||||
* $destinationPath. Some adapters may not support this method.
|
||||
*
|
||||
* @param array $metadata
|
||||
* @param string $queueId
|
||||
* @param array $options
|
||||
* @return void
|
||||
*/
|
||||
public function storeQueueMetadata($queueId, $metadata, $options = null)
|
||||
{
|
||||
// TODO Add support for SetQueueAttributes to client library
|
||||
require_once 'Zend/Cloud/OperationNotAvailableException.php';
|
||||
throw new Zend_Cloud_OperationNotAvailableException('Amazon SQS doesn\'t currently support storing metadata');
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the specified queue.
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $queueId
|
||||
* @param array $options
|
||||
* @return string Message ID
|
||||
*/
|
||||
public function sendMessage($queueId, $message, $options = null)
|
||||
{
|
||||
try {
|
||||
return $this->_sqs->send($queueId, $message);
|
||||
} catch(Zend_Service_Amazon_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on sending message: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recieve at most $max messages from the specified queue and return the
|
||||
* message IDs for messages recieved.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param int $max
|
||||
* @param array $options
|
||||
* @return array
|
||||
*/
|
||||
public function receiveMessages($queueId, $max = 1, $options = null)
|
||||
{
|
||||
try {
|
||||
return $this->_makeMessages($this->_sqs->receive($queueId, $max, $options[self::VISIBILITY_TIMEOUT]));
|
||||
} catch(Zend_Service_Amazon_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on recieving messages: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Zend_Cloud_QueueService_Message array for
|
||||
* Sqs messages.
|
||||
*
|
||||
* @param array $messages
|
||||
* @return Zend_Cloud_QueueService_Message[]
|
||||
*/
|
||||
protected function _makeMessages($messages)
|
||||
{
|
||||
$messageClass = $this->getMessageClass();
|
||||
$setClass = $this->getMessageSetClass();
|
||||
$result = array();
|
||||
foreach($messages as $message) {
|
||||
$result[] = new $messageClass($message['body'], $message);
|
||||
}
|
||||
return new $setClass($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the specified message from the specified queue.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param Zend_Cloud_QueueService_Message $message
|
||||
* @param array $options
|
||||
* @return void
|
||||
*/
|
||||
public function deleteMessage($queueId, $message, $options = null)
|
||||
{
|
||||
try {
|
||||
if($message instanceof Zend_Cloud_QueueService_Message) {
|
||||
$message = $message->getMessage();
|
||||
}
|
||||
$messageId = $message['handle'];
|
||||
return $this->_sqs->deleteMessage($queueId, $messageId);
|
||||
} catch(Zend_Service_Amazon_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on deleting a message: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Peek at the messages from the specified queue without removing them.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param int $num How many messages
|
||||
* @param array $options
|
||||
* @return Zend_Cloud_QueueService_Message[]
|
||||
*/
|
||||
public function peekMessages($queueId, $num = 1, $options = null)
|
||||
{
|
||||
try {
|
||||
return $this->_makeMessages($this->_sqs->receive($queueId, $num, 0));
|
||||
} catch(Zend_Service_Amazon_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on peeking messages: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SQS implementation
|
||||
* @return Zend_Service_Amazon_Sqs
|
||||
*/
|
||||
public function getClient()
|
||||
{
|
||||
return $this->_sqs;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
<?php
|
||||
/**
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cloud
|
||||
* @subpackage QueueService
|
||||
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
require_once 'Zend/Cloud/QueueService/Adapter/AbstractAdapter.php';
|
||||
require_once 'Zend/Cloud/QueueService/Exception.php';
|
||||
require_once 'Zend/Service/WindowsAzure/Storage/Queue.php';
|
||||
|
||||
/**
|
||||
* WindowsAzure adapter for simple queue service.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cloud
|
||||
* @subpackage QueueService
|
||||
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cloud_QueueService_Adapter_WindowsAzure
|
||||
extends Zend_Cloud_QueueService_Adapter_AbstractAdapter
|
||||
{
|
||||
/**
|
||||
* Option array keys for the Windows Azure adapter.
|
||||
*/
|
||||
const ACCOUNT_NAME = 'storage_accountname';
|
||||
const ACCOUNT_KEY = 'storage_accountkey';
|
||||
const HOST = "storage_host";
|
||||
const PROXY_HOST = "storage_proxy_host";
|
||||
const PROXY_PORT = "storage_proxy_port";
|
||||
const PROXY_CREDENTIALS = "storage_proxy_credentials";
|
||||
|
||||
/** list options */
|
||||
const LIST_PREFIX = 'prefix';
|
||||
const LIST_MAX_RESULTS = 'max_results';
|
||||
|
||||
/** message options */
|
||||
const MESSAGE_TTL = 'ttl';
|
||||
|
||||
const DEFAULT_HOST = Zend_Service_WindowsAzure_Storage::URL_CLOUD_QUEUE;
|
||||
|
||||
/**
|
||||
* Storage client
|
||||
*
|
||||
* @var Zend_Service_WindowsAzure_Storage_Queue
|
||||
*/
|
||||
protected $_storageClient = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array|Zend_Config $options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($options = array())
|
||||
{
|
||||
if ($options instanceof Zend_Config) {
|
||||
$options = $options->toArray();
|
||||
}
|
||||
|
||||
if (!is_array($options)) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Invalid options provided');
|
||||
}
|
||||
|
||||
if (isset($options[self::MESSAGE_CLASS])) {
|
||||
$this->setMessageClass($options[self::MESSAGE_CLASS]);
|
||||
}
|
||||
|
||||
if (isset($options[self::MESSAGESET_CLASS])) {
|
||||
$this->setMessageSetClass($options[self::MESSAGESET_CLASS]);
|
||||
}
|
||||
|
||||
// Build Zend_Service_WindowsAzure_Storage_Blob instance
|
||||
if (!isset($options[self::HOST])) {
|
||||
$host = self::DEFAULT_HOST;
|
||||
} else {
|
||||
$host = $options[self::HOST];
|
||||
}
|
||||
if (! isset($options[self::ACCOUNT_NAME])) {
|
||||
throw new Zend_Cloud_Storage_Exception('No Windows Azure account name provided.');
|
||||
}
|
||||
if (! isset($options[self::ACCOUNT_KEY])) {
|
||||
throw new Zend_Cloud_Storage_Exception('No Windows Azure account key provided.');
|
||||
}
|
||||
try {
|
||||
// TODO: support $usePathStyleUri and $retryPolicy
|
||||
$this->_storageClient = new Zend_Service_WindowsAzure_Storage_Queue(
|
||||
$host, $options[self::ACCOUNT_NAME], $options[self::ACCOUNT_KEY]);
|
||||
// Parse other options
|
||||
if (! empty($options[self::PROXY_HOST])) {
|
||||
$proxyHost = $options[self::PROXY_HOST];
|
||||
$proxyPort = isset($options[self::PROXY_PORT]) ? $options[self::PROXY_PORT] : 8080;
|
||||
$proxyCredentials = isset($options[self::PROXY_CREDENTIALS]) ? $options[self::PROXY_CREDENTIALS] : '';
|
||||
$this->_storageClient->setProxy(true, $proxyHost, $proxyPort, $proxyCredentials);
|
||||
}
|
||||
if (isset($options[self::HTTP_ADAPTER])) {
|
||||
$this->_storageClient->setHttpClientChannel($httpAdapter);
|
||||
}
|
||||
} catch(Zend_Service_WindowsAzure_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a queue. Returns the ID of the created queue (typically the URL).
|
||||
* It may take some time to create the queue. Check your vendor's
|
||||
* documentation for details.
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $options
|
||||
* @return string Queue ID (typically URL)
|
||||
*/
|
||||
public function createQueue($name, $options = null)
|
||||
{
|
||||
try {
|
||||
$queue = $this->_storageClient->createQueue($name, $options);
|
||||
return $queue->Name;
|
||||
} catch (Zend_Service_WindowsAzure_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on queue creation: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a queue. All messages in the queue will also be deleted.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param array $options
|
||||
* @return boolean true if successful, false otherwise
|
||||
*/
|
||||
public function deleteQueue($queueId, $options = null)
|
||||
{
|
||||
try {
|
||||
if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
|
||||
$queueId = $queueId->Name;
|
||||
}
|
||||
return $this->_storageClient->deleteQueue($queueId);
|
||||
} catch (Zend_Service_WindowsAzure_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on queue deletion: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List all queues.
|
||||
*
|
||||
* @param array $options
|
||||
* @return array Queue IDs
|
||||
*/
|
||||
public function listQueues($options = null)
|
||||
{
|
||||
$prefix = $maxResults = null;
|
||||
if (is_array($options)) {
|
||||
isset($options[self::LIST_PREFIX]) ? $prefix = $options[self::LIST_PREFIX] : null;
|
||||
isset($options[self::LIST_MAX_RESULTS]) ? $maxResults = $options[self::LIST_MAX_RESULTS] : null;
|
||||
}
|
||||
try {
|
||||
$queues = $this->_storageClient->listQueues($prefix, $maxResults);
|
||||
$result = array();
|
||||
foreach ($queues as $queue) {
|
||||
$result[] = $queue->Name;
|
||||
}
|
||||
return $result;
|
||||
} catch (Zend_Service_WindowsAzure_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on listing queues: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a key/value array of metadata for the given queue.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param array $options
|
||||
* @return array
|
||||
*/
|
||||
public function fetchQueueMetadata($queueId, $options = null)
|
||||
{
|
||||
try {
|
||||
if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
|
||||
$queueId = $queueId->Name;
|
||||
}
|
||||
return $this->_storageClient->getQueueMetadata($queueId);
|
||||
} catch (Zend_Service_WindowsAzure_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on fetching queue metadata: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a key/value array of metadata for the specified queue.
|
||||
* WARNING: This operation overwrites any metadata that is located at
|
||||
* $destinationPath. Some adapters may not support this method.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param array $metadata
|
||||
* @param array $options
|
||||
* @return void
|
||||
*/
|
||||
public function storeQueueMetadata($queueId, $metadata, $options = null)
|
||||
{
|
||||
try {
|
||||
if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
|
||||
$queueId = $queueId->Name;
|
||||
}
|
||||
return $this->_storageClient->setQueueMetadata($queueId, $metadata);
|
||||
} catch (Zend_Service_WindowsAzure_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on setting queue metadata: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the specified queue.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param string $message
|
||||
* @param array $options
|
||||
* @return string Message ID
|
||||
*/
|
||||
public function sendMessage($queueId, $message, $options = null)
|
||||
{
|
||||
try {
|
||||
if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
|
||||
$queueId = $queueId->Name;
|
||||
}
|
||||
return $this->_storageClient->putMessage(
|
||||
$queueId, $message, $options[self::MESSAGE_TTL]
|
||||
);
|
||||
} catch (Zend_Service_WindowsAzure_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on sending message: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recieve at most $max messages from the specified queue and return the
|
||||
* message IDs for messages recieved.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param int $max
|
||||
* @param array $options
|
||||
* @return Zend_Cloud_QueueService_Message[]
|
||||
*/
|
||||
public function receiveMessages($queueId, $max = 1, $options = null)
|
||||
{
|
||||
try {
|
||||
if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
|
||||
$queueId = $queueId->Name;
|
||||
}
|
||||
if (isset($options[self::VISIBILITY_TIMEOUT])) {
|
||||
$visibility = $options[self::VISIBILITY_TIMEOUT];
|
||||
} else {
|
||||
$visibility = self::DEFAULT_TIMEOUT;
|
||||
}
|
||||
return $this->_makeMessages($this->_storageClient->getMessages($queueId, $max, $visibility, false));
|
||||
} catch (Zend_Service_WindowsAzure_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on recieving messages: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Zend_Cloud_QueueService_Message array for
|
||||
* Azure messages.
|
||||
*
|
||||
* @param array $messages
|
||||
* @return Zend_Cloud_QueueService_Message[]
|
||||
*/
|
||||
protected function _makeMessages($messages)
|
||||
{
|
||||
$messageClass = $this->getMessageClass();
|
||||
$setClass = $this->getMessageSetClass();
|
||||
$result = array();
|
||||
foreach ($messages as $message) {
|
||||
$result[] = new $messageClass($message->MessageText, $message);
|
||||
}
|
||||
return new $setClass($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the specified message from the specified queue.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param Zend_Cloud_QueueService_Message $message Message ID or message
|
||||
* @param array $options
|
||||
* @return void
|
||||
*/
|
||||
public function deleteMessage($queueId, $message, $options = null)
|
||||
{
|
||||
try {
|
||||
if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
|
||||
$queueId = $queueId->Name;
|
||||
}
|
||||
if ($message instanceof Zend_Cloud_QueueService_Message) {
|
||||
$message = $message->getMessage();
|
||||
}
|
||||
if ($message instanceof Zend_Service_WindowsAzure_Storage_QueueMessage) {
|
||||
return $this->_storageClient->deleteMessage($queueId, $message);
|
||||
} else {
|
||||
throw new Zend_Cloud_QueueService_Exception('Cannot delete the message: message object required');
|
||||
}
|
||||
} catch (Zend_Service_WindowsAzure_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on deleting a message: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Peek at the messages from the specified queue without removing them.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param int $num How many messages
|
||||
* @param array $options
|
||||
* @return Zend_Cloud_QueueService_Message[]
|
||||
*/
|
||||
public function peekMessages($queueId, $num = 1, $options = null)
|
||||
{
|
||||
try {
|
||||
if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
|
||||
$queueId = $queueId->Name;
|
||||
}
|
||||
return $this->_makeMessages($this->_storageClient->peekMessages($queueId, $num));
|
||||
} catch (Zend_Service_WindowsAzure_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on peeking messages: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Azure implementation
|
||||
* @return Zend_Service_Azure_Storage_Queue
|
||||
*/
|
||||
public function getClient()
|
||||
{
|
||||
return $this->_storageClient;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
<?php
|
||||
/**
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cloud
|
||||
* @subpackage QueueService
|
||||
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
require_once 'Zend/Cloud/QueueService/Adapter/AbstractAdapter.php';
|
||||
require_once 'Zend/Cloud/QueueService/Exception.php';
|
||||
require_once 'Zend/Queue.php';
|
||||
|
||||
/**
|
||||
* WindowsAzure adapter for simple queue service.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Cloud
|
||||
* @subpackage QueueService
|
||||
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Cloud_QueueService_Adapter_ZendQueue
|
||||
extends Zend_Cloud_QueueService_Adapter_AbstractAdapter
|
||||
{
|
||||
/**
|
||||
* Options array keys for the Zend_Queue adapter.
|
||||
*/
|
||||
const ADAPTER = 'adapter';
|
||||
|
||||
/**
|
||||
* Storage client
|
||||
*
|
||||
* @var Zend_Queue
|
||||
*/
|
||||
protected $_queue = null;
|
||||
|
||||
/**
|
||||
* @var array All queues
|
||||
*/
|
||||
protected $_queues = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array|Zend_Config $options
|
||||
* @return void
|
||||
*/
|
||||
public function __construct ($options = array())
|
||||
{
|
||||
if ($options instanceof Zend_Config) {
|
||||
$options = $options->toArray();
|
||||
}
|
||||
|
||||
if (!is_array($options)) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Invalid options provided');
|
||||
}
|
||||
|
||||
if (isset($options[self::MESSAGE_CLASS])) {
|
||||
$this->setMessageClass($options[self::MESSAGE_CLASS]);
|
||||
}
|
||||
|
||||
if (isset($options[self::MESSAGESET_CLASS])) {
|
||||
$this->setMessageSetClass($options[self::MESSAGESET_CLASS]);
|
||||
}
|
||||
|
||||
// Build Zend_Service_WindowsAzure_Storage_Blob instance
|
||||
if (!isset($options[self::ADAPTER])) {
|
||||
throw new Zend_Cloud_QueueService_Exception('No Zend_Queue adapter provided');
|
||||
} else {
|
||||
$adapter = $options[self::ADAPTER];
|
||||
unset($options[self::ADAPTER]);
|
||||
}
|
||||
try {
|
||||
$this->_queue = new Zend_Queue($adapter, $options);
|
||||
} catch (Zend_Queue_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a queue. Returns the ID of the created queue (typically the URL).
|
||||
* It may take some time to create the queue. Check your vendor's
|
||||
* documentation for details.
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $options
|
||||
* @return string Queue ID (typically URL)
|
||||
*/
|
||||
public function createQueue($name, $options = null)
|
||||
{
|
||||
try {
|
||||
$this->_queues[$name] = $this->_queue->createQueue($name, isset($options[Zend_Queue::TIMEOUT])?$options[Zend_Queue::TIMEOUT]:null);
|
||||
return $name;
|
||||
} catch (Zend_Queue_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on queue creation: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a queue. All messages in the queue will also be deleted.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param array $options
|
||||
* @return boolean true if successful, false otherwise
|
||||
*/
|
||||
public function deleteQueue($queueId, $options = null)
|
||||
{
|
||||
if (!isset($this->_queues[$queueId])) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if ($this->_queues[$queueId]->deleteQueue()) {
|
||||
unset($this->_queues[$queueId]);
|
||||
return true;
|
||||
}
|
||||
} catch (Zend_Queue_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on queue deletion: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List all queues.
|
||||
*
|
||||
* @param array $options
|
||||
* @return array Queue IDs
|
||||
*/
|
||||
public function listQueues($options = null)
|
||||
{
|
||||
try {
|
||||
return $this->_queue->getQueues();
|
||||
} catch (Zend_Queue_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on listing queues: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a key/value array of metadata for the given queue.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param array $options
|
||||
* @return array
|
||||
*/
|
||||
public function fetchQueueMetadata($queueId, $options = null)
|
||||
{
|
||||
if (!isset($this->_queues[$queueId])) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return $this->_queues[$queueId]->getOptions();
|
||||
} catch (Zend_Queue_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on fetching queue metadata: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a key/value array of metadata for the specified queue.
|
||||
* WARNING: This operation overwrites any metadata that is located at
|
||||
* $destinationPath. Some adapters may not support this method.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param array $metadata
|
||||
* @param array $options
|
||||
* @return void
|
||||
*/
|
||||
public function storeQueueMetadata($queueId, $metadata, $options = null)
|
||||
{
|
||||
if (!isset($this->_queues[$queueId])) {
|
||||
throw new Zend_Cloud_QueueService_Exception("No such queue: $queueId");
|
||||
}
|
||||
try {
|
||||
return $this->_queues[$queueId]->setOptions($metadata);
|
||||
} catch (Zend_Queue_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on setting queue metadata: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the specified queue.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param string $message
|
||||
* @param array $options
|
||||
* @return string Message ID
|
||||
*/
|
||||
public function sendMessage($queueId, $message, $options = null)
|
||||
{
|
||||
if (!isset($this->_queues[$queueId])) {
|
||||
throw new Zend_Cloud_QueueService_Exception("No such queue: $queueId");
|
||||
}
|
||||
try {
|
||||
return $this->_queues[$queueId]->send($message);
|
||||
} catch (Zend_Queue_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on sending message: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recieve at most $max messages from the specified queue and return the
|
||||
* message IDs for messages recieved.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param int $max
|
||||
* @param array $options
|
||||
* @return array
|
||||
*/
|
||||
public function receiveMessages($queueId, $max = 1, $options = null)
|
||||
{
|
||||
if (!isset($this->_queues[$queueId])) {
|
||||
throw new Zend_Cloud_QueueService_Exception("No such queue: $queueId");
|
||||
}
|
||||
try {
|
||||
$res = $this->_queues[$queueId]->receive($max, isset($options[Zend_Queue::TIMEOUT])?$options[Zend_Queue::TIMEOUT]:null);
|
||||
if ($res instanceof Iterator) {
|
||||
return $this->_makeMessages($res);
|
||||
} else {
|
||||
return $this->_makeMessages(array($res));
|
||||
}
|
||||
} catch (Zend_Queue_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on recieving messages: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Zend_Cloud_QueueService_Message array for
|
||||
* Azure messages.
|
||||
*
|
||||
* @param array $messages
|
||||
* @return Zend_Cloud_QueueService_Message[]
|
||||
*/
|
||||
protected function _makeMessages($messages)
|
||||
{
|
||||
$messageClass = $this->getMessageClass();
|
||||
$setClass = $this->getMessageSetClass();
|
||||
$result = array();
|
||||
foreach ($messages as $message) {
|
||||
$result[] = new $messageClass($message->body, $message);
|
||||
}
|
||||
return new $setClass($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the specified message from the specified queue.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param Zend_Cloud_QueueService_Message $message Message ID or message
|
||||
* @param array $options
|
||||
* @return void
|
||||
*/
|
||||
public function deleteMessage($queueId, $message, $options = null)
|
||||
{
|
||||
if (!isset($this->_queues[$queueId])) {
|
||||
throw new Zend_Cloud_QueueService_Exception("No such queue: $queueId");
|
||||
}
|
||||
try {
|
||||
if ($message instanceof Zend_Cloud_QueueService_Message) {
|
||||
$message = $message->getMessage();
|
||||
}
|
||||
if (!($message instanceof Zend_Queue_Message)) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Cannot delete the message: Zend_Queue_Message object required');
|
||||
}
|
||||
|
||||
return $this->_queues[$queueId]->deleteMessage($message);
|
||||
} catch (Zend_Queue_Exception $e) {
|
||||
throw new Zend_Cloud_QueueService_Exception('Error on deleting a message: '.$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Peek at the messages from the specified queue without removing them.
|
||||
*
|
||||
* @param string $queueId
|
||||
* @param int $num How many messages
|
||||
* @param array $options
|
||||
* @return Zend_Cloud_QueueService_Message[]
|
||||
*/
|
||||
public function peekMessages($queueId, $num = 1, $options = null)
|
||||
{
|
||||
require_once 'Zend/Cloud/OperationNotAvailableException.php';
|
||||
throw new Zend_Cloud_OperationNotAvailableException('ZendQueue doesn\'t currently support message peeking');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Azure implementation
|
||||
* @return Zend_Queue
|
||||
*/
|
||||
public function getClient()
|
||||
{
|
||||
return $this->_queue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user