View Source

<ac:macro ac:name="note"><ac:rich-text-body>
<p>This proposal has been dropped in favor of <a href="http://framework.zend.com/wiki/display/ZFPROP/Zend_Queue+-+Justin+Plock">Zend_Queue</a></p></ac:rich-text-body></ac:macro>

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{composition-setup}{composition-setup}]]></ac:plain-text-body></ac:macro>
<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFPROP:Proposal Zone Template}

{zone-data:component-name}
Zend_Service_Amazon_Sqs
{zone-data}

{zone-data:proposer-list}
[Jon Whitcraft|mailto:jon.zf@mac.com]
{zone-data}

{zone-data:liaison}
TBD
{zone-data}

{zone-data:revision}
1.0 - Feb 13, 2009: Initial Draft.
1.1 - Feb 15, 2008: Added Class Skeletons and some basic us cases.
{zone-data}

{zone-data:overview}
Zend_Service_Amazon_SQS is an implementation of [Amazon Simple Queue Service|http://aws.amazon.com/sqs/]
{zone-data}

{zone-data:references}
* [SQS Developer Information|http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/]
* [SQS Getting Started Guide|http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/]
{zone-data}

{zone-data:requirements}

* This component *will* implement the Amazon SQS API
* This component *will* use Zend_Http_Client to comunicate with SQS
* This component *will* implement a CLI interface
{zone-data}

{zone-data:dependencies}
* [Zend_Service_Amazon_Abstract|http://framework.zend.com/issues/browse/ZF-5791]
* [Zend_Crypt_Hmac|http://framework.zend.com/wiki/pages/viewpage.action?pageId=35309]
{zone-data}

{zone-data:operation}
This component will created a CURD interface for Messages and Message Queues. This functionality will be provided in two ways:
* CLI interface
* PHP Interface
{zone-data}

{zone-data:milestones}
* Milestone 1: [design notes will be published here|http://framework.zend.com/wiki/x/sg]
* Milestone 2: Working prototype checked into the incubator supporting use cases
* Milestone 3: Unit tests exist, work, and are checked into SVN.
* Milestone 4: Initial documentation exists.
{zone-data}

{zone-data:class-list}
* Zend_Service_Amazon_Sqs
* Zend_Service_Amazon_Sqs_Cli
* Zend_Service_Amazon_Sqs_Exception
* Zend_Service_Amazon_Sqs_QueueManager
* Zend_Service_Amazon_Sqs_Queue
* Zend_Service_Amazon_Sqs_Response
{zone-data}

{zone-data:use-cases}

{deck:id=Use Cases}
{card:label=UC-01: Queue Management}
Queue Management
{code:php}

$sqs = new Zend_Service_Amazon_Sqs_QueueManager('access_key', 'secret_key');

// fetch an array of your current queues
$queues = $sqs->listQueues();

// add a new queue
$sqs->addQueue('myawesomequeue');

// delete an old queue
$sqs->deleteQueue('myoldqueue');

{code}
{card}

{card:label=UC-02: Queues}
Send/Receive/Delete Queue Messages:
{code:php}
$queue = new Zend_Service_Amazon_Sqs_QueueManager('myawesomequeue', 'access_key', 'secret_key');

// send the message
$queue->send('my_message');

// retrieve the message from the queue server
$msg = $queue->receive(1);

// print out the message body.
print $msg[0]['body'];

// delete the message from the queue.
$queue->delete($msg[0]['handle']);

{code}
{card}

{deck}

{zone-data}


{zone-data:skeletons}
{deck:id=Class Skeletions}
{card:label=Zend_Service_Amazon_Sqs}
{code:php}
<?php
/**
* Zend Framework
*
* 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_Service_Amazon
* @subpackage Sqs
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Exception.php 8064 2008-02-16 10:58:39Z thomas $
*/

require_once 'Zend/Service/Amazon/Abstract.php';

require_once 'Zend/Service/Amazon/Sqs/Response.php';

/**
* @category Zend
* @package Zend_Service_Amazon
* @subpackage Sqs
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Zend_Service_Amazon_Sqs extends Zend_Service_Amazon_Abstract
{
/**
* The HTTP query server
*/
const SQS_ENDPOINT = 'queue.amazonaws.com';

/**
* The API version to use
*/
const SQS_API_VERSION = '2008-01-01';

/**
* Legacy parameter required by SQS
*/
const SQS_SIGNATURE_VERSION = '1';

/**
* Period after which HTTP request will timeout in seconds
*/
const HTTP_TIMEOUT = 10;

/**
* Sends a HTTP request to the queue service using Zend_Http_Client
*
* The supplied <tt>$params</tt> array should contain only the specific
* parameters for the request type and should not include account,
* signature, or timestamp related parameters. These parameters are added
* automatically.
*
* @param array $params optional. Array of request parameters for the
* API call.
* @param string $queueUrl optional. The specific queue URL for which
* the request is made. Does not need to be
* specified for general actions like listing
* queues.
*
* @return mixed Zend_Service_Amazon_Sqs_Response object or false if the
* request failed.
*
* @throws Zend_Service_Amazon_Sqs_Exception if one or more errors are
* returned from Amazon.
*/
protected function sendRequest(array $params = array(), $queueUrl = null)
{
$url = ($queueUrl) ? $queueUrl : 'http://' . self::SQS_ENDPOINT . '/';

$params = $this->addRequiredParameters($params);

try {
/* @var $request Zend_Http_Client */
$request = self::getHttpClient();

$request->setConfig(array(
'timeout' => self::HTTP_TIMEOUT
));

$request->setUri($url);
$request->setMethod(Zend_Http_Client::POST);
$request->setParameterPost($params);

$httpResponse = $request->request();


} catch (Zend_Http_Client_Exception $zhce) {
$message = 'Error in request to AWS service: ' . $zhce->getMessage();
throw new Zend_Service_Amazon_Sqs_Exception($message, $zhce->getCode());
}

$response = new Zend_Service_Amazon_Sqs_Response($httpResponse);

$this->checkForErrors($response);

return $response;
}

/**
* Gets whether or not a visibility timeout is valid
*
* Visibility timeouts must be between 0 and 7200 seconds.
*
* @param integer $timeout the timeout value to check (in seconds).
*
* @return boolean true if the timeout is valid, otherwise false.
*/
protected function isValidVisibiltyTimeout($timeout)
{
$valid = true;

if($timeout < 0 || $timeout > 7200) {
$valid = false;
}

return $valid;
}

/**
* Adds required authentication and version parameters to an array of
* parameters
*
* The required parameters are:
* - AWSAccessKey
* - SignatureVersion
* - Timestamp
* - Version and
* - Signature
*
* If a required parameter is already set in the <tt>$parameters</tt> array,
* it is overwritten.
*
* @param array $parameters the array to which to add the required
* parameters.
*
* @return array
*/
protected function addRequiredParameters(array $parameters)
{
$parameters['AWSAccessKeyId'] = $this->getAccessKey();
$parameters['SignatureVersion'] = self::SQS_SIGNATURE_VERSION;
$parameters['Timestamp'] = gmdate('c');
$parameters['Version'] = self::SQS_API_VERSION;
$parameters['Signature'] = $this->signParameters($parameters);

return $parameters;
}

/**
* Computes the RFC 2104-compliant HMAC signature for request parameters
*
* This implements the Amazon Web Services signature, as per the following
* specification:
*
* 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
* excluding <tt>Signature</tt>, the value of which is being created),
* ignoring case.
*
* 2. Iterate over the sorted list and append the parameter name (in its
* original case) and then its value. Do not URL-encode the parameter
* values before constructing this string. Do not use any separator
* characters when appending strings.
*
* @param array $parameters the parameters for which to get the signature.
* @param string $secretKey the secret key to use to sign the parameters.
*
* @return string the signed data.
*/
protected function signParameters(array $paramaters)
{
$data = '';

uksort($paramaters, 'strcasecmp');
unset($paramaters['Signature']);

foreach($paramaters as $key => $value) {
$data .= $key . $value;
}

require_once 'Zend/Crypt/Hmac.php';
$hmac = Zend_Crypt_Hmac::compute($this->getSecretKey(), 'SHA1', $data, Zend_Crypt_Hmac::BINARY);

return base64_encode($hmac);
}

/**
* Checks for errors responses from Amazon
*
* @param Zend_Service_Amazon_Sqs_Response $response the response object to
* check.
*
* @return void
*
* @throws Zend_Service_Amazon_Sqs_Exception if one or more errors are
* returned from Amazon.
*/
private function checkForErrors(Zend_Service_Amazon_Sqs_Response $response)
{
$xpath = $response->getXPath();
$list = $xpath->query('//sqs:Error');
if ($list->length > 0) {
$node = $list->item(0);
$code = $xpath->evaluate('string(sqs:Code/text())', $node);
$message = $xpath->evaluate('string(sqs:Message/text())', $node);
throw new Zend_Service_Amazon_Sqs_Exception($message, 0, $code);
}

}
}
{code}
{card}

{card:label=Zend_Service_Amazon_Sqs_Exception}
{code:php}
<?php
/**
* Zend Framework
*
* 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_Service_Amazon
* @subpackage Sqs
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Exception.php 8064 2008-02-16 10:58:39Z thomas $
*/


/**
* Zend_Service_Exception
*/
require_once 'Zend/Service/Amazon/Exception.php';


/**
* @category Zend
* @package Zend_Service_Amazon
* @subpackage Sqs
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Service_Amazon_Sqs_Exception extends Zend_Service_Amazon_Exception
{
private $awsErrorCode = '';

public function __construct($message, $code, $awsErrorCode = '')
{
parent::__construct($message, $code);
$this->awsErrorCode = $awsErrorCode;
}

public function getErrorCode()
{
return $this->awsErrorCode;
}
}

{code}
{card}

{card:label=Zend_Service_Amazon_Sqs_Response}
{code:php}
<?php
/**
* Zend Framework
*
* 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_Service_Amazon
* @subpackage Sqs
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Exception.php 8064 2008-02-16 10:58:39Z thomas $
*/

require_once 'Zend/Http/Response.php';

/**
* @category Zend
* @package Zend_Service_Amazon
* @subpackage Sqs
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Service_Amazon_Sqs_Response {
/**
* XML namespace used for SQS responses.
*/
const XML_NAMESPACE = 'http://queue.amazonaws.com/doc/2008-01-01/';

/**
* The original HTTP response
*
* This contains the response body and headers.
*
* @var Zend_Http_Response
*/
private $_httpResponse = null;

/**
* The response document object
*
* @var DOMDocument
*/
private $_document = null;

/**
* The response XPath
*
* @var DOMXPath
*/
private $_xpath = null;

/**
* Last error code
*
* @var integer
*/
private $_errorCode = 0;

/**
* Last error message
*
* @var string
*/
private $_errorMessage = '';

/**
* Creates a new high-level SQS response object
*
* @param Zend_Http_Response $httpResponse the HTTP response.
*/
public function __construct(Zend_Http_Response $httpResponse)
{
$this->_httpResponse = $httpResponse;
}

/**
* Gets the XPath object for this response
*
* @return DOMXPath the XPath object for response.
*/
public function getXPath()
{
if ($this->_xpath === null) {
$document = $this->getDocument();
if ($document === false) {
$this->_xpath = false;
} else {
$this->_xpath = new DOMXPath($document);
$this->_xpath->registerNamespace('sqs',
self::XML_NAMESPACE);
}
}

return $this->_xpath;
}

/**
* Gets the document object for this response
*
* @return DOMDocument the DOM Document for this response.
*/
public function getDocument()
{
try {
$body = $this->_httpResponse->getBody();
} catch (Zend_Http_Exception $e) {
$body = false;
}

if ($this->_document === null) {
if ($body !== false) {
// turn off libxml error handling
$errors = libxml_use_internal_errors();

$this->_document = new DOMDocument();
if (!$this->_document->loadXML($body)) {
$this->_document = false;
}

// reset libxml error handling
libxml_clear_errors();
libxml_use_internal_errors($errors);
} else {
$this->_document = false;
}
}

return $this->_document;
}
}
{code}
{card}

{card:label=Zend_Service_Amazon_Sqs_QueueManager}
{code:php}
<?php
/**
* Zend Framework
*
* 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_Service_Amazon
* @subpackage Sqs
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Exception.php 8064 2008-02-16 10:58:39Z thomas $
*/

require_once 'Zend/Http/Response.php';

/**
* @category Zend
* @package Zend_Service_Amazon
* @subpackage Sqs
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Service_Amazon_Sqs_Response {
/**
* XML namespace used for SQS responses.
*/
const XML_NAMESPACE = 'http://queue.amazonaws.com/doc/2008-01-01/';

/**
* The original HTTP response
*
* This contains the response body and headers.
*
* @var Zend_Http_Response
*/
private $_httpResponse = null;

/**
* The response document object
*
* @var DOMDocument
*/
private $_document = null;

/**
* The response XPath
*
* @var DOMXPath
*/
private $_xpath = null;

/**
* Last error code
*
* @var integer
*/
private $_errorCode = 0;

/**
* Last error message
*
* @var string
*/
private $_errorMessage = '';

/**
* Creates a new high-level SQS response object
*
* @param Zend_Http_Response $httpResponse the HTTP response.
*/
public function __construct(Zend_Http_Response $httpResponse)
{
$this->_httpResponse = $httpResponse;
}

/**
* Gets the XPath object for this response
*
* @return DOMXPath the XPath object for response.
*/
public function getXPath()
{
if ($this->_xpath === null) {
$document = $this->getDocument();
if ($document === false) {
$this->_xpath = false;
} else {
$this->_xpath = new DOMXPath($document);
$this->_xpath->registerNamespace('sqs',
self::XML_NAMESPACE);
}
}

return $this->_xpath;
}

/**
* Gets the document object for this response
*
* @return DOMDocument the DOM Document for this response.
*/
public function getDocument()
{
try {
$body = $this->_httpResponse->getBody();
} catch (Zend_Http_Exception $e) {
$body = false;
}

if ($this->_document === null) {
if ($body !== false) {
// turn off libxml error handling
$errors = libxml_use_internal_errors();

$this->_document = new DOMDocument();
if (!$this->_document->loadXML($body)) {
$this->_document = false;
}

// reset libxml error handling
libxml_clear_errors();
libxml_use_internal_errors($errors);
} else {
$this->_document = false;
}
}

return $this->_document;
}
}
{code}
{card}

{card:label=Zend_Service_Amazon_Sqs_Queue}
{code:php}
<?php
/**
* Zend Framework
*
* 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_Service_Amazon
* @subpackage Sqs
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Exception.php 8064 2008-02-16 10:58:39Z thomas $
*/

/* Zend_Service_Amazon_Sqs */
require_once 'Zend/Service/Amazon/Sqs.php';

/* Zend_Service_Amazon_Sqs_Exception */
require_once 'Zend/Service/Amazon/Sqs/Exception.php';

/**
* @category Zend
* @package Zend_Service_Amazon
* @subpackage Sqs
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Service_Amazon_Sqs_Queue extends Zend_Service_Amazon_Sqs
{
/**
* The url of the queue we wanting to listen to or send message to
*
* @var string
*/
protected $queueUrl = null;

/**
* Creates a PHP SQS queue object
*
* Queue objects are created with the full URL because Amazon reserves the
* right to change the URL scheme for queues created in the future.
*
* @param string $queueUrl the URL of this queue.
*
* @param string $accessKey either a string containing the SQS access key for an account.
*
* @param string $secretKey the secret access key for the SQS account.
*
*/
public function __construct($queueUrl, $accessKey = null, $secretKey = null)
{
// Make sure the queue url contains the sqs end point;
if(false === strpos($queueUrl, self::SQS_ENDPOINT)) {
$queueUrl = 'http://' . self::SQS_ENDPOINT . '/' . $queueUrl;
}
$this->queueUrl = $queueUrl;

// start the parent class up.
parent::__construct($accessKey, $secretKey);
}

/**
* Gets a string representation of this queue
*
* Specifically, this returns the queue URL of this queue.
*
* @return string the URL of this queue.
*/
public function __toString()
{
return $this->queueUrl;
}

/**
* Sends a message to this queue
*
* @param string $message the message to put in this queue.
*
* @return string the message id of the message.
*
* @throws Zend_Service_Amazon_Sqs_Exception
*/
public function send($message)
{
$params = array();

$params['Action'] = 'SendMessage';
$params['MessageBody'] = $message;

try {
$response = $this->sendRequest($params, $this->queueUrl);
} catch (Zend_Service_Amazon_Sqs_Exception $e) {
switch ($e->getErrorCode()) {
case 'InvalidMessageContents':
throw new Zend_Service_Amazon_Sqs_Exception('The ' .
'message contains characters outside the allowed set.', 0,
$message);

case 'MessageTooLong':
throw new Zend_Service_Amazon_Sqs_Exception('The ' .
'message size can not exceed 8192 bytes.', 0, $message);

case 'AWS.SimpleQueueService.NonExistentQueue':
throw new Zend_Service_Amazon_Sqs_Exception('The ' .
'queue "' . $this . '" does not exist.', 0,
$this->queueUrl);

default:
throw $e;
}
}

$xpath = $response->getXPath();
$expectedMd5 = md5($message);

$id = $xpath->evaluate('string(//sqs:MessageId/text())');
$md5 = $xpath->evaluate('string(//sqs:MD5OfMessageBody/text())');

if ($md5 !== $expectedMd5) {
throw new Zend_Service_Amazon_Sqs_Exception('Message ' .
'body was not received by Amazon correctly. Expected ' .
'MD5 was: "' . $expectedMd5 . '", but received MD5 was: "' .
$md5 .'".', 0, $id);
}

return $id;
}

/**
* Retrieves one or more messages from this queue
*
* Retrieved messages are made invisible to subsequent requests for the
* duration of the visibility timeout. To permanently remove a message from
* this queue, first retrieve the message and them delete it using the
* {@link Zend_Service_Amazon_Sqs_Queue::delete()} method.
*
* @var integer $count optional. The number of messages to retrieve from
* the queue. If not specified, one message is
* retrieved. At most, ten messages may be
* retrieved.
* @var integer $timeout optional. The number of seconds that retrieved
* messages should be hidden from view in the queue.
* If not specified, the default visibility timeout
* of this queue is used.
* @throws Zend_Service_Amazon_Sqs_Exception
* @return array an array containing one or more retrieved messages. Each
* message in the returned array is represented as an
* associative array with the following keys:
* - <tt>id</tt> - the message id.
* - <tt>body</tt> - the body of the message.
* - <tt>handle</tt> - the receipt handle of the message.
*/
public function receive($count, $timeout = null)
{
if($timeout !== null && !$this->isValidVisibiltyTimeout($timeout)) {
throw new Zend_Service_Amazon_Sqs_Exception('The ' .
'specified timeout falls outside the allowable range (0-7200)',
0, $timeout);
}

// make sure the count is valid for amazon. valid race is 1-10.
$count = max($count, 1);
$count = min($count, 10);

$params = array();

$params['Action'] = 'ReceiveMessage';
$params['MaxNumberOfMessages'] = $count;

if ($timeout) {
$params['VisibilityTimeout'] = $timeout;
}

try {
$response = $this->sendRequest($params, $this->queueUrl);
} catch (Zend_Service_Amazon_Sqs_Exception $e) {
switch ($e->getError()) {
case 'AWS.SimpleQueueService.NonExistentQueue':
throw new Zend_Service_Amazon_Sqs_Exception('The ' .
'queue "' . $this . '" does not exist.', 0,
$this->queueUrl);
default:
throw $e;
}
}

// get messages from response
$messages = array();
$xpath = $response->getXPath();
$nodes = $xpath->query('//sqs:Message');

foreach ($nodes as $node) {
$id = $xpath->evaluate('string(sqs:MessageId/text())', $node);
$handle = $xpath->evaluate('string(sqs:ReceiptHandle/text())', $node);
$body = $xpath->evaluate('string(sqs:Body/text())', $node);

$message = array();
$message['id'] = $id;
$message['body'] = $body;
$message['handle'] = $handle;

$messages[] = $message;
}

return $messages;
}

/**
* Deletes a message from this queue
*
* @param string $handle the receipt handle of the message to delete.
*
* @return void
*
* @throws Zend_Service_Amazon_Sqs_Exception
*
*/
public function delete($handle)
{
$params = array();

$params['Action'] = 'DeleteMessage';
$params['ReceiptHandle'] = $handle;

try {
$this->sendRequest($params, $this->queueUrl);
} catch (Zend_Service_Amazon_Sqs_Exception $zsase) {
print $zsase->getErrorCode();
throw $zsase;
}
}

/**
* Gets an associative array of one or more attributes of this queue
*
* Currently, Amazon SQS only allows retrieving the values of the
* following attributes:
*
* - <tt>ApproximateNumberOfMessages</tt> - an approximation of the number
* of messages in this queue.
* - <tt>VisibilityTimeout</tt> - the default time period for
* which messages are made
* invisible when retrieved from
* this queue.
*
* Additionally, the special attribute <tt>All</tt> may be used to retrieve
* all available attributes.
*
* @param string $name optional. The name of the attribute value to get or
* <tt>All</tt> to get all available attributes. If
* not specified, 'All' is used.
*
* @return array an associative array of available attributes. The attribute
* name is the array key and the attribute value is the
* array value.
*
* @throws Zend_Service_Amazon_Sqs_Exception
*
*/
public function getAttributes($name = 'All')
{
$params = array();

$params['Action'] = 'GetQueueAttributes';
$params['AttributeName'] = $name;

try {
$response = $this->sendRequest($params, $this->queueUrl);
} catch (Zend_Service_Amazon_Sqs_Exception $e) {
switch ($e->getError()) {
case 'InvalidAttributeName':
throw new Zend_Service_Amazon_Sqs_Exception('The ' .
'attribute name "' . $name . '" is not a valid attribute ' .
'name.', 0, $name);

case 'AWS.SimpleQueueService.NonExistentQueue':
throw new Zend_Service_Amazon_Sqs_Exception('The ' .

'queue "' . $this . '" does not exist.', 0,
$this->queueUrl);

default:
throw $e;
}
}

$attributes = array();
$xpath = $response->getXPath();
$nodes = $xpath->query('//sqs:Attribute');

foreach ($nodes as $node) {
$name = $xpath->evaluate('string(sqs:Name/text())', $node);
$value = $xpath->evaluate('string(sqs:Value/text())', $node);

$attributes[$name] = $value;
}

return $attributes;
}

/**
* Sets an attribute of this queue
*
* Currently, Amazon SQS only allows setting the <tt>VisibilityTimeout</tt>
* attribute. This attribute sets the default time period for which
* messages are made invisible when retrieved from this queue.
*
* @param string $name the attribute name.
* @param mixed $value the attribute value.
*
* @return void
*
* @throws Zend_Service_Amazon_Sqs_Exception.
*/
public function setAttribute($name, $value)
{
$params = array();

$params['Action'] = 'SetQueueAttributes';
$params['Attribute.Name'] = $name;
$params['Attribute.Value'] = $value;

try {
$this->sendRequest($params, $this->queueUrl);
} catch (Services_Amazon_SQS_ErrorException $e) {
switch ($e->getError()) {
case 'InvalidAttributeName':
throw new Zend_Service_Amazon_Sqs_Exception('The ' .
'attribute name "' . $name . '" is not a valid attribute ' .
'name.', 0, $name);

case 'AWS.SimpleQueueService.NonExistentQueue':
throw new Zend_Service_Amazon_Sqs_Exception('The ' .
'queue "' . $this . '" does not exist.', 0,
$this->queueUrl);

default:
throw $e;
}
}
}

}
{code}
{card}

{deck}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>