Zend_Service_Amazon_Sqs.xml 11 KB

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