Source for file Queue.php
Documentation is available at Queue.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* Contains a class representing a queue in the Amazon Simple Queue
* Copyright 2008 Mike Brittain, Amazon.com, silverorange
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Portions of this code were taken from the Amazon SQS PHP5 Library which
* is distributed under the Apache 2.0 license
* (http://aws.amazon.com/apache2.0).
* @package Services_Amazon_SQS
* @author Mike Brittain <mike@mikebrittain.com>
* @author Michael Gauthier <mike@silverorange.com>
* @copyright 2008 Mike Brittain, 2008 Amazon.com, 2008 silverorange
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @version CVS: $Id: Queue.php 283528 2009-07-05 18:53:10Z gauthierm $
* @link http://pear.php.net/package/Services_Amazon_SQS
* @link http://aws.amazon.com/sqs/
* @link http://s3.amazonaws.com/awsdocs/SQS/20080101/sqs-dg-20080101.pdf
require_once 'Services/Amazon/SQS/Exceptions.php';
* Amazon SQS client base class.
require_once 'Services/Amazon/SQS.php';
* A queue in the Amazon SQS
* This class allows sending objects to and receiving objects from a queue on
* the Amazon SQS. Use the queue URL provided by Amazon through the list or
* creating methods when instantiating this class.
* @package Services_Amazon_SQS
* @author Mike Brittain <mike@mikebrittain.com>
* @author Michael Gauthier <mike@silverorange.com>
* @copyright 2008 Mike Brittain, 2008 Amazon.com, 2008-2009 silverorange
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link http://pear.php.net/package/Services_Amazon_SQS
* @link http://aws.amazon.com/sqs/
* @link http://s3.amazonaws.com/awsdocs/SQS/20080101/sqs-dg-20080101.pdf
// {{{ private properties
* The URL of this queue provided by Amazon when the queue was created
* 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. Always
* use the full queue URL.
* @param string $queueUrl the URL of
* @param Services_Amazon_SQS_Account|string$accessKey either a
* {@link Services_Amazon_SQS_Account} object or a string containing
* the SQS access key for an account.
* @param string $secretAccessKey if the second
* parameter is an account object, this parameter is ignored.
* Otherwise, this parameter is required and is the secret access
* key for the SQS account.
* @param HTTP_Request2 $request optional. The
* HTTP request object to use. If not specified, a HTTP request
* object is created automatically.
public function __construct($queueUrl, $accessKey, $secretAccessKey = '',
HTTP_Request2 $request = null
parent ::__construct($accessKey, $secretAccessKey, $request);
$this->_queueUrl = $queueUrl;
* Gets a string representation of this queue
* Specifically, this returns the queue URL of this queue.
* @return string the URL of this queue.
* 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 Services_Amazon_SQS_InvalidMessageException if the message
* contains characters outside the allowed set or if the message
* size is greater than 8192 bytes.
* @throws Services_Amazon_SQS_ChecksumException if the received checksum
* from Amazon does not match the calculated checksum of the
* @throws Services_Amazon_SQS_InvalidQueueException if this queue does
* not exist for the Amazon SQS account.
* @throws Services_Amazon_SQS_ErrorException if one or more errors are
* @throws Services_Amazon_SQS_HttpException if the HTTP request fails.
public function send($message)
$params['Action'] = 'SendMessage';
$params['MessageBody'] = $message;
$response = $this->sendRequest($params, $this->_queueUrl);
switch ($e->getError ()) {
case 'InvalidMessageContents':
'The message contains characters outside the allowed set.',
'The message size can not exceed 8192 bytes.',
case 'InvalidParameterValue':
$tooLongMessage = 'Value for parameter MessageBody is '
. 'invalid. Reason: Message body must be shorter than '
if ($e->getMessage () === $tooLongMessage) {
'The message size can not exceed 8192 bytes.',
case 'AWS.SimpleQueueService.NonExistentQueue':
'The queue "' . $this . '" does not exist.',
$xpath = $response->getXPath ();
$messageNodes = $xpath->query ('//sqs:Message');
$expectedMd5 = md5($message);
$id = $xpath->evaluate ('string(//sqs:MessageId/text())');
$md5 = $xpath->evaluate ('string(//sqs:MD5OfMessageBody/text())');
if ($md5 !== $expectedMd5) {
'body was not received by Amazon correctly. Expected ' .
'MD5 was: "' . $expectedMd5 . '", but received MD5 was: "' .
* 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 Services_Amazon_SQS_Queue::delete()} method.
* @param 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
* @param 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
* @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:
* - <kbd>id</kbd> - the message id.
* - <kbd>body</kbd> - the body of the message.
* - <kbd>handle</kbd> - the receipt handle of the message.
* @throws Services_Amazon_SQS_InvalidTimeoutException if the provided
* <kbd>$timeout</kbd> is not in the valid range.
* @throws Services_Amazon_SQS_InvalidQueueException if this queue does
* not exist for the Amazon SQS account.
* @throws Services_Amazon_SQS_ErrorException if one or more errors are
* @throws Services_Amazon_SQS_HttpException if the HTTP request fails.
* @see Services_Amazon_SQS_Queue::changeMessageVisibility()
public function receive($count = 1 , $timeout = null )
'specified timeout falls outside the allowable range (0-7200)',
// normalize count if it's outside of Amazon's constraints
$count = min($count, 10 );
$params['Action'] = 'ReceiveMessage';
$params['MaxNumberOfMessages'] = $count;
$params['VisibilityTimeout'] = $timeout;
$response = $this->sendRequest($params, $this->_queueUrl);
switch ($e->getError ()) {
case 'AWS.SimpleQueueService.NonExistentQueue':
'queue "' . $this . '" does not exist.', 0 ,
// get messages from response
$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['body'] = $body;
$message['handle'] = $handle;
* Deletes a message from this queue
* @param string $handle the receipt handle of the message to delete.
* @throws Services_Amazon_SQS_InvalidQueueException if this queue does
* not exist for the Amazon SQS account.
* @throws Services_Amazon_SQS_ErrorException if one or more errors are
* @throws Services_Amazon_SQS_HttpException if the HTTP request fails.
public function delete($handle)
$params['Action'] = 'DeleteMessage';
$params['ReceiptHandle'] = $handle;
switch ($e->getError ()) {
case 'AWS.SimpleQueueService.NonExistentQueue':
'queue "' . $this . '" does not exist.', 0 ,
// {{{ changeMessageVisibility()
* Changes the visibility timeout for a message in this queue
* Once a message is received, it is invisible to the queue for the
* duration of the visibility timeout. After receiving the message, the
* visibility timeout may be increased if the queue operation will take
* longer than the default visibility timeout.
* Message visibility may be changed multiple times, but a single received
* message can not have a total visibility timeout period exceeding 12
* // receive a message with a 10 second visibility timeout
* $message = $queue->receive(1, 10);
* // check if processing the message will take longer than ten seconds
* if ($message['body'] == 'this will take a long time') {
* // if so, add five minutes to the visibility timeout
* $queue->changeMessageVisibility($message['handle'], 300);
* // now process the message
* @param string $handle the receipt handle of the message to update.
* @param integer $timeout the new visibility timeout for the message.
* @throws Services_Amazon_SQS_InvalidTimeoutException if the provided
* <kbd>$timeout</kbd> is not in the valid range for the given
* @throws Services_Amazon_SQS_InvalidQueueException if this queue does
* not exist for the Amazon SQS account.
* @throws Services_Amazon_SQS_ErrorException if one or more errors are
* @throws Services_Amazon_SQS_HttpException if the HTTP request fails.
$params['Action'] = 'ChangeMessageVisibility';
$params['ReceiptHandle'] = $handle;
$params['VisibilityTimeout'] = $timeout;
switch ($e->getError ()) {
case 'AWS.SimpleQueueService.NonExistentQueue':
'queue "' . $this . '" does not exist.', 0 ,
case 'InvalidParameterValue':
$exp = '/^Value .*? for parameter VisibilityTimeout is ' .
'invalid\. Reason: VisibilityTimeout must be an integer ' .
'between 0 and 43200\.$/';
'The timeout "' . $timeout . '" is not valid for ' .
'the specified message.',
* Gets an associative array of one or more attributes of this queue
* Currently, Amazon SQS only allows retrieving the values of the
* - <kbd>ApproximateNumberOfMessages</kbd> - an approximation of the number
* - <kbd>CreatedTimestamp</kbd> - the date this queue was
* - <kbd>LastModifiedTimestamp</kbd> - the date this queue was
* - <kbd>VisibilityTimeout</kbd> - the default time period for
* which messages are made
* invisible when retrieved from
* Timestamp values are formatted as Unix timestamps.
* Additionally, the special attribute <kbd>All</kbd> may be used to
* retrieve all available attributes.
* @param string|array$name optional. The name or names of the attribute
* values to get or <kbd>All</kbd> to get all
* available attributes. If not specified,
* <i><kbd>All</kdb></i> is used. Multiple
* specific attributes may be retrieved using an
* array of attribute names.
* @return array an associative array of available attributes. The attribute
* name is the array key and the attribute value is the
* @throws Services_Amazon_SQS_InvalidAttributeException if an invalid
* attribute name is requested.
* @throws Services_Amazon_SQS_InvalidQueueException if this queue does
* not exist for the Amazon SQS account.
* @throws Services_Amazon_SQS_ErrorException if one or more errors are
* @throws Services_Amazon_SQS_HttpException if the HTTP request fails.
$params = array ('Action' => 'GetQueueAttributes');
$params['AttributeName'] = reset($name);
foreach ($name as $attributeName) {
$params['AttributeName.' . $count] = $attributeName;
$response = $this->sendRequest($params, $this->_queueUrl);
switch ($e->getError ()) {
case 'InvalidAttributeName':
'attribute name "' . $name . '" is not a valid attribute ' .
case 'AWS.SimpleQueueService.NonExistentQueue':
'queue "' . $this . '" does not exist.', 0 ,
$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;
* Sets an attribute of this queue
* Currently, Amazon SQS only allows setting the
* <kbd>VisibilityTimeout</kbd> attribute. This attribute sets the default
* time period for which messages are made invisible when retrieved from
* @param string $name the attribute name.
* @param mixed $value the attribute value.
* @throws Services_Amazon_SQS_InvalidAttributeException if an invalid
* @throws Services_Amazon_SQS_InvalidQueueException if this queue does
* not exist for the Amazon SQS account.
* @throws Services_Amazon_SQS_ErrorException if one or more errors are
* @throws Services_Amazon_SQS_HttpException if the HTTP request fails.
$params['Action'] = 'SetQueueAttributes';
$params['Attribute.Name'] = $name;
$params['Attribute.Value'] = $value;
$response = $this->sendRequest($params, $this->_queueUrl);
switch ($e->getError ()) {
case 'InvalidAttributeName':
'attribute name "' . $name . '" is not a valid attribute ' .
case 'AWS.SimpleQueueService.NonExistentQueue':
'queue "' . $this . '" does not exist.', 0 ,
* Adds a permission to this queue for a specific principal
* Permissions that may be granted on this queue are:
* - <kbd>*</kbd> (all permissions)
* - <kbd>SendMessage</kbd>
* - <kbd>ReceiveMessage</kbd>
* - <kbd>DeleteMessage</kbd>
* - <kbd>ChangeMessageVisibility</kbd>
* - <kbd>GetQueueAttributes</kbd>
* 'account' => '123456789012',
* 'permission => 'ReceiveMessage'
* 'account' => '123456789012',
* 'permission => 'GetQueueAttributes'
* @param string $label a unique identifier for this permission. This
* label can be used to revoke the permission at
* @param array $principals an array of principals receiving the
* permission. Each array element is a
* separate array containing the following keys:
* - <kbd>account</kbd> - the id of the AWS
* - <kbd>permission</kbd> - the permission to
* @throws InvalidArgumentException if no principals are specified, or if
* the specified principals do not contain the <kbd>account</kbd>
* and <kbd>permission</kbd> fields.
* @throws Services_Amazon_SQS_InvalidPermissionLabelException if the
* specified permission label is not valid.
* @throws Services_Amazon_SQS_InvalidQueueException if this queue does
* not exist for the Amazon SQS account.
* @throws Services_Amazon_SQS_ErrorException if one or more errors are
* @throws Services_Amazon_SQS_HttpException if the HTTP request fails.
* @see Services_Amazon_SQS_Queue::removePermission()
if (!$this->isValidPermissionLabel ($label)) {
'The permission label "' . $label . '" is not a valid ' .
'permission label. Permission labels must be 1-80 ' .
'characters long and must consist only of alphanumeric ' .
'characters, dashes or underscores.',
if (count ($principals) === 0 ) {
throw new InvalidArgumentException (
'At least one principal must be specified.'
foreach ($principals as $principal) {
throw new InvalidArgumentException (
'Each principal must be specified as an associative ' .
'array with the following keys: "account", and ' .
$params['Action'] = 'AddPermission';
$params['Label'] = $label;
if (count($principals) === 1 ) {
$principal = reset($principals);
$params['AWSAccountId'] = $principal['account'];
$params['ActionName'] = $principal['permission'];
foreach ($principals as $principal) {
$params['AWSAccountId.' . $count] = $principal['account'];
$params['ActionName.' . $count] = $principal['permission'];
switch ($e->getError ()) {
case 'AWS.SimpleQueueService.NonExistentQueue':
'queue "' . $this . '" does not exist.', 0 ,
case 'InvalidParameterValue':
$exp = '/^Value .*? for parameter Label is invalid\. ' .
'Reason: Already exists\.\.$/';
'Permission label "' . $label . '" is already used ' .
'for another permission. A different label must be ' .
'used for this permission.',
// {{{ removePermission()
* Removes a permission from this queue by the permission's label
* $queue->removePermission('billing-read-only');
* @param string $label the unique identifier of the permission to remove.
* This should be the same label used when the
* @throws Services_Amazon_SQS_InvalidPermissionLabelException if the
* specified permission label is not valid.
* @throws Services_Amazon_SQS_InvalidQueueException if this queue does
* not exist for the Amazon SQS account.
* @throws Services_Amazon_SQS_ErrorException if one or more errors are
* @throws Services_Amazon_SQS_HttpException if the HTTP request fails.
* @see Services_Amazon_SQS_Queue::addPermission()
'The permission label "' . $label . '" is not a valid ' .
'permission label. Permission labels must be 1-80 ' .
'characters long and must consist only of alphanumeric ' .
'characters, dashes or underscores.',
$params['Action'] = 'RemovePermission';
$params['Label'] = $label;
switch ($e->getError ()) {
case 'AWS.SimpleQueueService.NonExistentQueue':
'queue "' . $this . '" does not exist.', 0 ,
case 'InvalidParameterValue':
$exp = '/^Value .*? for parameter Label is invalid\. ' .
'Reason: Does not exist\.\.$/';
'Permission label "' . $label . '" does not exist ' .
'for this queue and cannot be removed.',
Documentation generated on Tue, 24 Nov 2009 17:00:04 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.
|