File: service-sqs.rst

package info (click to toggle)
aws-sdk-for-php 2.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 14,448 kB
  • ctags: 10,879
  • sloc: php: 157,235; python: 233; makefile: 184; xml: 28; sh: 5
file content (97 lines) | stat: -rw-r--r-- 2,943 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
.. service:: Sqs

Creating a queue
----------------

Now, let's create a queue. You can create a standard queue by just providing a name. Make sure to get the queue's URL
from the result, since the queue URL is the unique identifier used to specify the queue in order to send and receive
messages.

.. code-block:: php

    $result = $client->createQueue(array('QueueName' => 'my-queue'));
    $queueUrl = $result->get('QueueUrl');

You can also set attributes on your queue when you create it.

.. code-block:: php

    $result = $client->createQueue(array(
        'QueueName'  => 'my-queue',
        'Attributes' => array(
            'DelaySeconds'       => 5,
            'MaximumMessageSize' => 4096, // 4 KB
        ),
    ));
    $queueUrl = $result->get('QueueUrl');

Or you can also set queue attributes later.

.. code-block:: php

    $result = $client->setQueueAttributes(array(
        'QueueUrl'   => $queueUrl,
        'Attributes' => array(
            'VisibilityTimeout' => 2 * 60 * 60, // 2 min
        ),
    ));

Sending messages
----------------

Sending a message to a queue is straight forward with the ``SendMessage`` command.

.. code-block:: php

    $client->sendMessage(array(
        'QueueUrl'    => $queueUrl,
        'MessageBody' => 'An awesome message!',
    ));

You can overwrite the queue's default delay for a message when you send it.

.. code-block:: php

    $client->sendMessage(array(
        'QueueUrl'     => $queueUrl,
        'MessageBody'  => 'An awesome message!',
        'DelaySeconds' => 30,
    ));

Receiving messages
------------------

Receiving messages is done with the ``ReceiveMessage`` command.

.. code-block:: php

    $result = $client->receiveMessage(array(
        'QueueUrl' => $queueUrl,
    ));

    foreach ($result->getPath('Messages/*/Body') as $messageBody) {
        // Do something with the message
        echo $messageBody;
    }

By default, only one message will be returned. If you want to get more messages, make sure to use the
``MaxNumberOfMessages`` parameter and specify a number of messages (1 to 10). Remember that you are not guaranteed to
receive that many messages, but you can receive up to that amount depending on how many are actually in the queue at
the time of your request.

SQS also supports `"long polling"
<http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html>`_, meaning that you
can instruct SQS to hold the connection open with the SDK for up to 20 seconds in order to wait for a message to arrive
in the queue. To configure this behavior, you must use the ``WaitTimeSeconds`` parameter.

.. code-block:: php

    $result = $client->receiveMessage(array(
        'QueueUrl'        => $queueUrl,
        'WaitTimeSeconds' => 10,
    ));

.. note:: You can also configure long-polling at the queue level by setting the ``ReceiveMessageWaitTimeSeconds`` queue
          attribute.

.. apiref:: Sqs