Zend_Service_Amazon_Sqs.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.service.amazon.sqs">
  4. <title>Zend_Service_Amazon_Sqs</title>
  5. <sect2 id="zend.service.amazon.sqs.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <ulink url="http://aws.amazon.com/sqs/">Amazon Simple Queue Service
  9. (Amazon SQS)</ulink> offers a reliable, highly scalable, hosted
  10. queue for storing messages as they travel between computers. By
  11. using Amazon SQS, developers can simply move data between
  12. distributed components of their applications that perform different
  13. tasks, without losing messages or requiring each component to be
  14. always available. Amazon SQS makes it easy to build an automated
  15. workflow, working in close conjunction with the Amazon Elastic
  16. Compute Cloud (Amazon EC2) and the other <acronym>AWS</acronym> infrastructure web
  17. services.
  18. </para>
  19. <para>
  20. Amazon SQS works by exposing Amazon's web-scale messaging
  21. infrastructure as a web service. Any computer on the Internet can
  22. add or read messages without any installed software or special
  23. firewall configurations. Components of applications using Amazon SQS
  24. can run independently, and do not need to be on the same network,
  25. developed with the same technologies, or running at the same time.
  26. </para>
  27. </sect2>
  28. <sect2 id="zend.service.amazon.sqs.registering">
  29. <title>Registering with Amazon SQS</title>
  30. <para>
  31. Before you can get started with
  32. <classname>Zend_Service_Amazon_Sqs</classname>, you must first
  33. register for an account. Please see the <ulink
  34. url="http://aws.amazon.com/sqs/faqs/">SQS FAQ</ulink> page on
  35. the Amazon website for more information.
  36. </para>
  37. <para>
  38. After registering, you will receive an application key and a secret key.
  39. You will need both to access the SQS service.
  40. </para>
  41. </sect2>
  42. <sect2 id="zend.service.amazon.sqs.apiDocumentation">
  43. <title>API Documentation</title>
  44. <para>
  45. The <classname>Zend_Service_Amazon_Sqs</classname> class provides
  46. the <acronym>PHP</acronym> wrapper to the Amazon SQS REST interface. Please consult the
  47. <ulink
  48. url="http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=31">Amazon
  49. SQS documentation</ulink> for detailed description of the
  50. service. You will need to be familiar with basic concepts in order
  51. to use this service.
  52. </para>
  53. </sect2>
  54. <sect2 id="zend.service.amazon.sqs.features">
  55. <title>Features</title>
  56. <para>
  57. <classname>Zend_Service_Amazon_Sqs</classname> provides the
  58. following functionality:
  59. </para>
  60. <itemizedlist>
  61. <listitem>
  62. <para>
  63. A single point for configuring your amazon.sqs
  64. authentication credentials that can be used across the
  65. amazon.sqs namespaces.
  66. </para>
  67. </listitem>
  68. <listitem>
  69. <para>
  70. A proxy object that is more convenient to use than an <acronym>HTTP</acronym>
  71. client alone, mostly removing the need to manually construct
  72. <acronym>HTTP</acronym> POST requests to access the REST service.
  73. </para>
  74. </listitem>
  75. <listitem>
  76. <para>
  77. A response wrapper that parses each response body and throws
  78. an exception if an error occurred, alleviating the need to
  79. repeatedly check the success of many commands.
  80. </para>
  81. </listitem>
  82. <listitem>
  83. <para>
  84. Additional convenience methods for some of the more common
  85. operations.
  86. </para>
  87. </listitem>
  88. </itemizedlist>
  89. </sect2>
  90. <sect2 id="zend.service.amazon.sqs.storing-your-first">
  91. <title>Getting Started</title>
  92. <para>
  93. Once you have registered with Amazon SQS, you're ready to create
  94. your queue and store some messages on SQS. Each queue can contain
  95. unlimited amount of messages, identified by name.
  96. </para>
  97. <para>
  98. The following example demonstrates creating a queue, storing and
  99. retrieving messages.
  100. </para>
  101. <example id="zend.service.amazon.sqs.storing-your-first.example">
  102. <title>Zend_Service_Amazon_Sqs Usage Example</title>
  103. <programlisting language="php"><![CDATA[
  104. $sqs = new Zend_Service_Amazon_Sqs($my_aws_key, $my_aws_secret_key);
  105. $queue_url = $sqs->create('test');
  106. $message = 'this is a test';
  107. $message_id = $sqs->send($queue_url, $message);
  108. foreach ($sqs->receive($queue_url) as $message) {
  109. echo $message['body'].'<br/>';
  110. }
  111. ]]></programlisting>
  112. </example>
  113. <para>
  114. Since the <classname>Zend_Service_Amazon_Sqs</classname> service
  115. requires authentication, you should pass your credentials (AWS key
  116. and secret key) to the constructor. If you only use one account,
  117. you can set default credentials for the service:
  118. </para>
  119. <programlisting language="php"><![CDATA[
  120. Zend_Service_Amazon_Sqs::setKeys($my_aws_key, $my_aws_secret_key);
  121. $sqs = new Zend_Service_Amazon_Sqs();
  122. ]]></programlisting>
  123. </sect2>
  124. <sect2 id="zend.service.amazon.sqs.queues">
  125. <title>Queue operations</title>
  126. <para>
  127. All messages SQS are stored in queues. A queue has to be created
  128. before any message operations. Queue names must be unique under your
  129. access key and secret key.
  130. </para>
  131. <para>
  132. Queue names can contain lowercase letters, digits, periods (.),
  133. underscores (_), and dashes (-). No other symbols allowed. Queue
  134. names can be a maximum of 80 characters.
  135. </para>
  136. <itemizedlist>
  137. <listitem>
  138. <para>
  139. <methodname>create()</methodname> creates a new queue.
  140. </para>
  141. </listitem>
  142. <listitem>
  143. <para>
  144. <methodname>delete()</methodname> removes all messages in
  145. the queue.
  146. </para>
  147. <example id="zend.service.amazon.sqs.queues.removalExample">
  148. <title>Zend_Service_Amazon_Sqs Queue Removal Example</title>
  149. <programlisting language="php"><![CDATA[
  150. $sqs = new Zend_Service_Amazon_Sqs($my_aws_key, $my_aws_secret_key);
  151. $queue_url = $sqs->create('test_1');
  152. $sqs->delete($queue_url);
  153. ]]></programlisting>
  154. </example>
  155. </listitem>
  156. <listitem>
  157. <para>
  158. <methodname>count()</methodname> gets the approximate number
  159. of messages in the queue.
  160. </para>
  161. <example id="zend.service.amazon.sqs.queues.countExample">
  162. <title>Zend_Service_Amazon_Sqs Queue Count Example</title>
  163. <programlisting language="php"><![CDATA[
  164. $sqs = new Zend_Service_Amazon_Sqs($my_aws_key, $my_aws_secret_key);
  165. $queue_url = $sqs->create('test_1');
  166. $sqs->send($queue_url, 'this is a test');
  167. $count = $sqs->count($queue_url); // Returns '1'
  168. ]]></programlisting>
  169. </example>
  170. </listitem>
  171. <listitem>
  172. <para>
  173. <methodname>getQueues()</methodname> returns the list of the
  174. names of all queues belonging to the user.
  175. </para>
  176. <example id="zend.service.amazon.sqs.queues.listExample">
  177. <title>Zend_Service_Amazon_Sqs Queue Listing Example</title>
  178. <programlisting language="php"><![CDATA[
  179. $sqs = new Zend_Service_Amazon_Sqs($my_aws_key, $my_aws_secret_key);
  180. $list = $sqs->getQueues();
  181. foreach($list as $queue) {
  182. echo "I have queue $queue\n";
  183. }
  184. ]]></programlisting>
  185. </example>
  186. </listitem>
  187. </itemizedlist>
  188. </sect2>
  189. <sect2 id="zend.service.amazon.sqs.messages">
  190. <title>Message operations</title>
  191. <para>
  192. After a queue is created, simple messages can be sent into the queue
  193. then received at a later point in time. Messages can be up to 8KB in
  194. length. If longer messages are needed please see <ulink
  195. url="http://framework.zend.com/manual/en/zend.service.amazon.s3.html">S3</ulink>.
  196. There is no limit to the number of messages a queue can contain.
  197. </para>
  198. <itemizedlist>
  199. <listitem>
  200. <para>
  201. <methodname>sent($queue_url, $message)</methodname> send the
  202. <varname>$message</varname> to the <varname>$queue_url</varname> SQS
  203. queue <acronym>URL</acronym>.
  204. </para>
  205. <example id="zend.service.amazon.sqs.messages.sendExample">
  206. <title>Zend_Service_Amazon_Sqs Message Send Example</title>
  207. <programlisting language="php"><![CDATA[
  208. $sqs = new Zend_Service_Amazon_Sqs($my_aws_key, $my_aws_secret_key);
  209. $queue_url = $sqs->create('test_queue');
  210. $sqs->send($queue_url, 'this is a test message');
  211. ]]></programlisting>
  212. </example>
  213. </listitem>
  214. <listitem>
  215. <para>
  216. <methodname>receive($queue_url)</methodname> retrieves
  217. messages from the queue.
  218. </para>
  219. <example id="zend.service.amazon.sqs.messages.receiveExample">
  220. <title>Zend_Service_Amazon_Sqs Message Receive Example</title>
  221. <programlisting language="php"><![CDATA[
  222. $sqs = new Zend_Service_Amazon_Sqs($my_aws_key, $my_aws_secret_key);
  223. $queue_url = $sqs->create('test_queue');
  224. $sqs->send($queue_url, 'this is a test message');
  225. foreach ($sqs->receive($queue_url) as $message) {
  226. echo "got message ".$message['body'].'<br/>';
  227. }
  228. ]]></programlisting>
  229. </example>
  230. </listitem>
  231. <listitem>
  232. <para>
  233. <methodname>deleteMessage($queue_url, $handle)</methodname>
  234. deletes a message from a queue. A message must first be
  235. received using the <methodname>receive()</methodname> method
  236. before it can be deleted.
  237. </para>
  238. <example id="zend.service.amazon.sqs.messages.deleteExample">
  239. <title>Zend_Service_Amazon_Sqs Message Delete Example</title>
  240. <programlisting language="php"><![CDATA[
  241. $sqs = new Zend_Service_Amazon_Sqs($my_aws_key, $my_aws_secret_key);
  242. $queue_url = $sqs->create('test_queue');
  243. $sqs->send($queue_url, 'this is a test message');
  244. foreach ($sqs->receive($queue_url) as $message) {
  245. echo "got message ".$message['body'].'<br/>';
  246. if ($sqs->deleteMessage($queue_url, $message['handle'])) {
  247. echo "Message deleted";
  248. }
  249. else {
  250. echo "Message not deleted";
  251. }
  252. }
  253. ]]></programlisting>
  254. </example>
  255. </listitem>
  256. </itemizedlist>
  257. </sect2>
  258. </sect1>